python-bunch 0.1.6__tar.gz → 0.1.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {python_bunch-0.1.6/python_bunch.egg-info → python_bunch-0.1.7}/PKG-INFO +1 -1
- {python_bunch-0.1.6 → python_bunch-0.1.7}/bunch/bunch.py +16 -2
- {python_bunch-0.1.6 → python_bunch-0.1.7}/bunch/immutable_bunch.py +17 -3
- {python_bunch-0.1.6 → python_bunch-0.1.7}/pyproject.toml +1 -1
- {python_bunch-0.1.6 → python_bunch-0.1.7/python_bunch.egg-info}/PKG-INFO +1 -1
- {python_bunch-0.1.6 → python_bunch-0.1.7}/tests/test_bunch.py +45 -0
- {python_bunch-0.1.6 → python_bunch-0.1.7}/tests/test_immutable_bunch.py +45 -0
- {python_bunch-0.1.6 → python_bunch-0.1.7}/LICENSE +0 -0
- {python_bunch-0.1.6 → python_bunch-0.1.7}/README.md +0 -0
- {python_bunch-0.1.6 → python_bunch-0.1.7}/bunch/__init__.py +0 -0
- {python_bunch-0.1.6 → python_bunch-0.1.7}/python_bunch.egg-info/SOURCES.txt +0 -0
- {python_bunch-0.1.6 → python_bunch-0.1.7}/python_bunch.egg-info/dependency_links.txt +0 -0
- {python_bunch-0.1.6 → python_bunch-0.1.7}/python_bunch.egg-info/top_level.txt +0 -0
- {python_bunch-0.1.6 → python_bunch-0.1.7}/setup.cfg +0 -0
@@ -66,5 +66,19 @@ class Bunch:
|
|
66
66
|
return self.__dict__.items()
|
67
67
|
|
68
68
|
@staticmethod
|
69
|
-
def from_dict(dictionary: dict) ->
|
70
|
-
|
69
|
+
def from_dict(dictionary: dict, recursive: bool = True) -> 'Bunch':
|
70
|
+
ret = Bunch()
|
71
|
+
if recursive:
|
72
|
+
for key, value in dictionary.items():
|
73
|
+
if isinstance(value, dict):
|
74
|
+
ret[key] = Bunch.from_dict(value, recursive=True)
|
75
|
+
elif isinstance(value, list):
|
76
|
+
ret[key] = [
|
77
|
+
Bunch.from_dict(item, recursive=True) if isinstance(item, dict) else item
|
78
|
+
for item in value
|
79
|
+
]
|
80
|
+
else:
|
81
|
+
ret[key] = value
|
82
|
+
else:
|
83
|
+
ret.update(dictionary)
|
84
|
+
return ret
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import json
|
2
2
|
from typing import Any
|
3
|
-
|
3
|
+
from bunch.bunch import Bunch
|
4
4
|
|
5
5
|
class ImmutableBunchException(Exception):
|
6
6
|
def __init__(self, message: str) -> None:
|
@@ -71,5 +71,19 @@ class ImmutableBunch:
|
|
71
71
|
return self.__dict__.items()
|
72
72
|
|
73
73
|
@staticmethod
|
74
|
-
def from_dict(dictionary: dict) ->
|
75
|
-
|
74
|
+
def from_dict(dictionary: dict, recursive: bool = True) -> 'ImmutableBunch':
|
75
|
+
ret = dict()
|
76
|
+
if recursive:
|
77
|
+
for key, value in dictionary.items():
|
78
|
+
if isinstance(value, dict):
|
79
|
+
ret[key] = ImmutableBunch.from_dict(value, recursive=True)
|
80
|
+
elif isinstance(value, list):
|
81
|
+
ret[key] = [
|
82
|
+
ImmutableBunch.from_dict(item, recursive=True) if isinstance(item, dict) else item
|
83
|
+
for item in value
|
84
|
+
]
|
85
|
+
else:
|
86
|
+
ret[key] = value
|
87
|
+
else:
|
88
|
+
ret.update(dictionary)
|
89
|
+
return ImmutableBunch(**ret)
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
4
4
|
|
5
5
|
[project]
|
6
6
|
name = "python-bunch"
|
7
|
-
version = "0.1.
|
7
|
+
version = "0.1.7"
|
8
8
|
description = "A lightweight Python class that behaves like a dict but supports attribute-style access."
|
9
9
|
readme = "README.md"
|
10
10
|
requires-python = ">=3.7"
|
@@ -103,6 +103,51 @@ class TestBunch(unittest.TestCase):
|
|
103
103
|
self.assertEqual(b.fruit, 'apple')
|
104
104
|
self.assertEqual(b.color, 'red')
|
105
105
|
|
106
|
+
def test_from_dict_recursive_nested(self):
|
107
|
+
data = {
|
108
|
+
'user': {
|
109
|
+
'name': 'Alice',
|
110
|
+
'profile': {'age': 30, 'city': 'Paris'}
|
111
|
+
}
|
112
|
+
}
|
113
|
+
b = Bunch.from_dict(data, recursive=True)
|
114
|
+
|
115
|
+
self.assertIsInstance(b.user, Bunch)
|
116
|
+
self.assertIsInstance(b.user.profile, Bunch)
|
117
|
+
self.assertEqual(b.user.profile.city, 'Paris')
|
118
|
+
|
119
|
+
def test_from_dict_recursive_deeply_nested(self):
|
120
|
+
data = {'level1': {'level2': {'level3': {'value': 42}}}}
|
121
|
+
b = Bunch.from_dict(data, recursive=True)
|
122
|
+
|
123
|
+
self.assertEqual(b.level1.level2.level3.value, 42)
|
124
|
+
self.assertIsInstance(b.level1.level2, Bunch)
|
125
|
+
self.assertIsInstance(b.level1.level2.level3, Bunch)
|
126
|
+
|
127
|
+
def test_from_dict_with_list_of_dicts(self):
|
128
|
+
data = {
|
129
|
+
'users': [
|
130
|
+
{'name': 'Alice', 'age': 30},
|
131
|
+
{'name': 'Bob', 'age': 25}
|
132
|
+
]
|
133
|
+
}
|
134
|
+
b = Bunch.from_dict(data, recursive=True)
|
135
|
+
|
136
|
+
self.assertIsInstance(b.users[0], Bunch)
|
137
|
+
self.assertEqual(b.users[0].name, 'Alice')
|
138
|
+
self.assertEqual(b.users[1].age, 25)
|
139
|
+
|
140
|
+
def test_from_dict_recursive_false(self):
|
141
|
+
data = {
|
142
|
+
'user': {
|
143
|
+
'name': 'Alice',
|
144
|
+
'profile': {'age': 30}
|
145
|
+
}
|
146
|
+
}
|
147
|
+
b = Bunch.from_dict(data, recursive=False)
|
148
|
+
|
149
|
+
self.assertIsInstance(b.user, dict)
|
150
|
+
self.assertEqual(b.user['profile']['age'], 30)
|
106
151
|
|
107
152
|
if __name__ == '__main__':
|
108
153
|
unittest.main()
|
@@ -109,6 +109,51 @@ class TestBunch(unittest.TestCase):
|
|
109
109
|
self.assertEqual(ib.fruit, 'apple')
|
110
110
|
self.assertEqual(ib.color, 'red')
|
111
111
|
|
112
|
+
def test_from_dict_recursive_nested(self):
|
113
|
+
data = {
|
114
|
+
'user': {
|
115
|
+
'name': 'Alice',
|
116
|
+
'profile': {'age': 30, 'city': 'Paris'}
|
117
|
+
}
|
118
|
+
}
|
119
|
+
b = ImmutableBunch.from_dict(data, recursive=True)
|
120
|
+
|
121
|
+
self.assertIsInstance(b.user, ImmutableBunch)
|
122
|
+
self.assertIsInstance(b.user.profile, ImmutableBunch)
|
123
|
+
self.assertEqual(b.user.profile.city, 'Paris')
|
124
|
+
|
125
|
+
def test_from_dict_recursive_deeply_nested(self):
|
126
|
+
data = {'level1': {'level2': {'level3': {'value': 42}}}}
|
127
|
+
b = ImmutableBunch.from_dict(data, recursive=True)
|
128
|
+
|
129
|
+
self.assertEqual(b.level1.level2.level3.value, 42)
|
130
|
+
self.assertIsInstance(b.level1.level2, ImmutableBunch)
|
131
|
+
self.assertIsInstance(b.level1.level2.level3, ImmutableBunch)
|
132
|
+
|
133
|
+
def test_from_dict_with_list_of_dicts(self):
|
134
|
+
data = {
|
135
|
+
'users': [
|
136
|
+
{'name': 'Alice', 'age': 30},
|
137
|
+
{'name': 'Bob', 'age': 25}
|
138
|
+
]
|
139
|
+
}
|
140
|
+
b = ImmutableBunch.from_dict(data, recursive=True)
|
141
|
+
|
142
|
+
self.assertIsInstance(b.users[0], ImmutableBunch)
|
143
|
+
self.assertEqual(b.users[0].name, 'Alice')
|
144
|
+
self.assertEqual(b.users[1].age, 25)
|
145
|
+
|
146
|
+
def test_from_dict_recursive_false(self):
|
147
|
+
data = {
|
148
|
+
'user': {
|
149
|
+
'name': 'Alice',
|
150
|
+
'profile': {'age': 30}
|
151
|
+
}
|
152
|
+
}
|
153
|
+
b = ImmutableBunch.from_dict(data, recursive=False)
|
154
|
+
|
155
|
+
self.assertIsInstance(b.user, dict)
|
156
|
+
self.assertEqual(b.user['profile']['age'], 30)
|
112
157
|
|
113
158
|
if __name__ == '__main__':
|
114
159
|
unittest.main()
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|