Skip to content

Commit 1061e23

Browse files
committed
main branch merged rel branch
2 parents e89d438 + a95fa6f commit 1061e23

File tree

4 files changed

+50
-23
lines changed

4 files changed

+50
-23
lines changed

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,37 @@ Python interactive shell demo use case:
3636
>>> DotDict = dotdict.DotDict
3737
>>>
3838
>>> sample = DotDict()
39+
>>> sample
40+
DotDict(**{})
3941
>>> print(sample)
4042
.{}
4143
>>>
4244
>>> sample.attr1 = 1
4345
>>> sample.attr2 = 1.1
4446
>>> sample.attr3 = True
4547
>>> sample.attr4 = "Hello"
46-
>>> print(sample)
47-
.{attr1: 1, attr2: 1.1, attr3: True, attr4: Hello}
48-
>>> print(sample.attr4)
49-
Hello
50-
>>>
5148
>>> sample.attr5 = {"hello": "World"}
49+
>>> sample
50+
DotDict(**{'attr1': 1, 'attr2': 1.1, 'attr3': True, 'attr4': 'Hello', 'attr5': DotDict(**{'hello': 'World'})})
5251
>>> print(sample)
53-
.{attr1: 1, attr2: 1.1, attr3: True, attr4: Hello, attr5: .{hello: World}}
52+
.{attr1: 1, attr2: 1.1, attr3: True, attr4: 'Hello', attr5: .{hello: 'World'}}
5453
>>> print(sample.attr5.hello)
5554
World
5655
>>>
5756
>>> sample2 = sample.to_dict____()
5857
>>> print(sample2)
5958
{'attr1': 1, 'attr2': 1.1, 'attr3': True, 'attr4': 'Hello', 'attr5': {'hello': 'World'}}
6059
>>>
60+
>>> sample3 = eval(repr(sample))
61+
>>> sample3
62+
DotDict(**{'attr1': 1, 'attr2': 1.1, 'attr3': True, 'attr4': 'Hello', 'attr5': DotDict(**{'hello': 'World'})})
63+
>>> print(sample3)
64+
.{attr1: 1, attr2: 1.1, attr3: True, attr4: 'Hello', attr5: .{hello: 'World'}}
65+
>>> print(sample3 == sample)
66+
True
67+
>>> len(sample3)
68+
5
69+
>>>
6170
```
6271

6372
## `TimedInput`

lyc_pyutils/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,33 @@
1515
from lyc_pyutils.libs import timedinput
1616
from lyc_pyutils.libs import clamps
1717

18+
dotdict = dotdict
1819
DotDict = dotdict.DotDict
1920

21+
timedinput = timedinput
2022
TimedInput = timedinput.TimedInput
2123

24+
functhread = functhread
2225
FuncThread = functhread.FuncThread
2326

27+
randbool = randbool
2428
rand_bool = randbool.rand_bool
2529

30+
jsonrw = jsonrw
2631
load_json = jsonrw.load_json
2732
save_json = jsonrw.save_json
2833
load_json_str = jsonrw.load_json_str
2934
save_json_str = jsonrw.save_json_str
3035

36+
batchlog = batchlog
3137
logstr = batchlog.logstr
3238
logln = batchlog.logln
3339
flushlogs = batchlog.flushlogs
3440

41+
textrw = textrw
3542
load_text = textrw.load_text
3643
save_text = textrw.save_text
3744

45+
clamps = clamps
3846
clamp_float = clamps.clamp_float
3947
clamp_int = clamps.clamp_int

lyc_pyutils/libs/dotdict.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def from_dict____(cls, dict_):
6464
Returns:
6565
result: the resulting DotDict
6666
"""
67-
# print(f"fromdict____ dic: {dic}") # Debug
67+
# print(f"from_dict____ dict_: {dict_}") # Debug
6868
result = DotDict()
6969

7070
for key in dict_:
@@ -115,33 +115,33 @@ def _mand_init(self, *args, **kwargs):
115115

116116
# Set the inherited keys from the custom class level
117117
if self_type is not DotDict and issubclass(self_type, DotDict):
118-
class_dict = self_type.__dict__
118+
class_dir = dir(self_type)
119119

120-
for key in class_dict:
121-
key = str(key)
122-
# print(f"_dot_dict_init class_dict {key}: {class_dict[key]}") # Debug
120+
for name in class_dir:
121+
name = str(name)
122+
# print(f"_mand_init class attribute {name}: {getattr(self_type, name)}") # Debug
123123

124-
if not self_type.is_exc_key____(key):
125-
val = class_dict[key]
126-
self.set_attr____(key, val)
124+
if not self_type.is_exc_key____(name):
125+
val = getattr(self_type, name)
126+
self.set_attr____(name, val)
127127
# end for
128128
# end if
129129

130130
# Set keys with the key names from the variable arguments
131131
for arg in args:
132132
arg = str(arg)
133-
# print(f"_dot_dict_init *args arg: {arg}") # Debug
133+
# print(f"_mand_init *args arg: {arg}") # Debug
134134

135-
if not self_type.is_exc_key____(key):
135+
if not self_type.is_exc_key____(name):
136136
self.set_attr____(arg, None)
137137
# end for
138138

139139
# Set pairs with the key names and values from the keyword arguments
140140
for kw in kwargs:
141141
kw = str(kw)
142-
# print(f"_dot_dict_init **kwargs kw: {kw} arg: {kwargs[kw]}") # Debug
142+
# print(f"_mand_init **kwargs kw: {kw} arg: {kwargs[kw]}") # Debug
143143

144-
if not self_type.is_exc_key____(key):
144+
if not self_type.is_exc_key____(name):
145145
arg = kwargs[kw]
146146
self.set_attr____(kw, arg)
147147
# end for
@@ -753,14 +753,24 @@ def str____(self):
753753
result = ".{}"
754754
return result
755755

756-
result = ".{"
756+
segs = []
757757

758758
for key in self:
759-
result += f"{key.__str__()}: {self[key].__str__()}, "
759+
val = self[key]
760+
761+
if isinstance(val, DotDict):
762+
seg = f"{key.__str__()}: {val.__str__()}, "
763+
else:
764+
seg = f"{key.__str__()}: {val.__repr__()}, "
765+
# end if
766+
767+
segs.append(seg)
768+
# end for
760769

761-
result = result[:-2] # Remove the trailing comma and space
762-
result += "}"
770+
contents = "".join(segs)
771+
contents = contents[:-2] # Remove the trailing comma and space
763772

773+
result = f".{{{contents}}}"
764774
return result
765775

766776
def to_dict____(self):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
def main():
2323
_setup(
2424
name="lyc-pyutils",
25-
version="2.0.1",
25+
version="2.0.2",
2626
author="Yucheng Liu",
2727
license="Copyright (C) 2022 Yucheng Liu. GNU LGPL3 license.",
2828
description="LYC's personal Python utilities.",

0 commit comments

Comments
 (0)