Design Pattern Examples
Overview of object-oriented design patterns
composite_filedirentry.py
Go to the documentation of this file.
9
10from datetime import datetime
11from enum import Enum
12
13
14
16class FileDirTypes(Enum):
17
18 File = 0,
19
20
21 Directory = 1
22
23
24
29class FileDirEntryList(list):
30 pass
31
32
33
35
36
38
39
43 @property
44 def FileDirType(self) -> FileDirTypes:
45 return self._fileDirType
46
47
48 @property
49 def Name(self) -> str:
50 return self._name
51
52
54 @property
55 def Length(self) -> int:
56 return self._length
57
58
59 @property
60 def WhenModified(self) -> datetime:
61 return self._whenModified
62
63
66 @property
67 def Children(self) -> FileDirEntryList:
68 return None
69
70
71
72
83 def __init__(self, type = FileDirTypes.File, entryName = "", size = 0, modDate = datetime.now()):
84 self._fileDirType = type
85 self._name = entryName
86 self._length = size
87 self._whenModified = modDate
88
89
103
104
105
107
108
112
120 def __init__(self, entryName : str, size : int, modDate : datetime):
121 super().__init__(type=FileDirTypes.File, entryName=entryName, size=size, modDate=modDate)
122
123
124
126
127
128
135
136
138
139
144 @property
145 def Length(self):
146 if not self._lengthSet:
147 # Recurse over all children, accumulating their size.
148 for child in self._children:
149 self._length += child.Length
150 self._lengthSet = True
151 return self._length
152
153
158 @property
159 def Children(self) -> FileDirEntryList:
160 return self._children
161
162
163
172 def __init__(self, entryName: str, modDate: datetime, children : FileDirEntryList):
173 super().__init__(type=FileDirTypes.Directory, entryName=entryName, modDate=modDate)
174 self._children = children # type: FileDirEntryList
175 self._lengthSet = False # type: bool
176
177
Represents a Directory entry.
_children
Children of this directory.
def Length(self)
Property getter for the size of all children of this directory: value = o.Length.
FileDirEntryList Children(self)
Property getter for the children of this node: value = o.Children.
def __init__(self, str entryName, datetime modDate, FileDirEntryList children)
Construct a DirEntry instance.
_lengthSet
True if the length has already been computed for this directory; otherwise, False.
Base class representing a File or Directory entry.
datetime WhenModified(self)
Property getter for when this entry was last modified: value = o.WhenModified
_name
Name of this entry.
def __init__(self, type=FileDirTypes.File, entryName="", size=0, modDate=datetime.now())
Constructor.
_fileDirType
Value from the FileDirTypes enumeration indicating what type this entry is.
_whenModified
A datetime object indicating when this entry was last modified.
str Name(self)
Property getter for the name of this entry: value = o.Name
FileDirTypes FileDirType(self)
Property getter for the type of this entry as represented by a value from the FileDirTypes enumeratio...
_length
Size of this entry.
FileDirEntryList Children(self)
Property getter for the children of this entry: value = o.Children.
int Length(self)
Property getter for the length in bytes of this entry: value = o.Length.
Makes it easier to refer to a list of child nodes.
Represents the type of entries allowed in the hierarchy for the Composite design pattern example.
Represents a File entry.
def __init__(self, str entryName, int size, datetime modDate)
Constructor.
Structure representing a File (FileEntry) or Directory (DirEntry) entry. This is included as the firs...