Skip to content

Commit 8935e90

Browse files
committed
TR updates, first round
1 parent 3cb5121 commit 8935e90

File tree

5 files changed

+25
-63
lines changed

5 files changed

+25
-63
lines changed

python-dict-attribute/config_v2.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,30 @@ def __init__(self, name):
33
self.name = name
44

55
def set_option(self, key, value):
6-
setattr(self, key, value)
6+
self.__dict__[key] = value
77

88
def get_option(self, key):
9-
return getattr(self, key, None)
9+
return self.__dict__.get(key, None)
1010

1111
def remove_option(self, key):
12-
if hasattr(self, key):
13-
delattr(self, key)
12+
if key in self.__dict__:
13+
del self.__dict__[key]
14+
# self.__dict__.pop(key)
1415
print(f"'{key}' removed!")
1516
else:
1617
print(f"'{key}' does not exist.")
1718

1819
def clear(self):
19-
for key in list(self.__dict__.keys()):
20-
delattr(self, key)
20+
self.__dict__.clear()
2121
print("All options removed!")
22+
23+
24+
conf = Config("GUI App")
25+
conf.set_option("theme", "dark")
26+
conf.set_option("size", "200x400")
27+
print(conf.__dict__)
28+
conf.remove_option("size")
29+
print(conf.__dict__)
30+
# conf.remove_option("autosave") # Raises KeyError
31+
conf.clear()
32+
print(conf.__dict__)

python-dict-attribute/demo.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ def method(self):
99

1010

1111
print(DemoClass.__dict__)
12+
demo_object = DemoClass()
13+
print(demo_object.__dict__)
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ def as_tuple(self):
2222
return tuple(self.__dict__.values())
2323

2424

25-
person = Person("John", "Doe", 30)
26-
print(repr(person))
27-
print(person)
28-
print(person.as_dict())
29-
print(person.as_tuple())
25+
john = Person("John", "Doe", 30)
26+
print(repr(john))
27+
Person("John", "Doe", 30)
28+
print(john)
29+
print(john.as_dict())
30+
print(john.as_tuple())

python-dict-attribute/person_v2.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

python-dict-attribute/plain_record.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)