forked from tuvo1106/python_design_patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic.py
More file actions
31 lines (24 loc) · 813 Bytes
/
dynamic.py
File metadata and controls
31 lines (24 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class FileWithLogging:
def __init__(self, file):
self.file = file
def writelines(self, strings):
self.file.writelines(strings)
print(f'wrote {len(strings)} lines')
def __iter__(self):
return self.file.__iter__()
def __next__(self):
return self.file.__next__()
def __getattr__(self, item):
return getattr(self.__dict__['file'], item)
def __setattr__(self, key, value):
if key == 'file':
self.__dict__[key] = value
else:
setattr(self.__dict__['file'], key)
def __delattr__(self, item):
delattr(self.__dict__['file'], item)
if __name__ == "__main__":
file = FileWithLogging(open('hello.txt', 'w'))
file.writelines(['hello', 'world'])
file.write('testing')
file.close()