python-bunch 0.1.7__tar.gz → 0.1.9__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-bunch
3
- Version: 0.1.7
3
+ Version: 0.1.9
4
4
  Summary: A lightweight Python class that behaves like a dict but supports attribute-style access.
5
5
  Author-email: Omer Menashe <unspecified@mail.com>
6
6
  License: MIT
@@ -16,7 +16,8 @@ Dynamic: license-file
16
16
 
17
17
  ## Bunch
18
18
 
19
- A simple Python class that allows dictionary-style and attribute-style access to data interchangeably. Think of it as a lightweight object wrapper for dictionaries — great for config objects, JSON responses, or anything else you'd normally throw in a dict.
19
+ A simple Python class that allows dictionary-style and attribute-style access to data interchangeably.
20
+ Think of it as a lightweight object wrapper for dictionaries — great for config objects, JSON responses, or anything else you'd normally throw in a dict.
20
21
 
21
22
  ### <ins> Features </ins>
22
23
 
@@ -36,18 +37,28 @@ You can install this package via PIP: _pip install python-bunch_
36
37
  # - Mutable Bunch -
37
38
  from bunch.bunch import Bunch
38
39
 
39
- my_bunch = Bunch({'name': 'Jane', 'age': 30})
40
+ my_bunch = Bunch(name='Jane', age=30)
40
41
 
41
42
  print(my_bunch.name) # Output: Jane
42
43
  print(my_bunch['age']) # Output: 30
43
44
 
44
- # - Immutable Bunch -v
45
+ # From Dictionary
46
+ my_dict = {'name': 'Bob', 'age': 25}
47
+ my_bunch_from_dict = Bunch.from_dict(my_dict)
48
+ print(my_bunch_from_dict) # Output: {'name': 'Bob', 'age': 25}
49
+
50
+ # - Immutable Bunch -
45
51
  from bunch.immutable_bunch import ImmutableBunch
46
52
 
47
- my_immutable_bunch = ImmutableBunch({'name': 'John', 'age': 25})
53
+ my_immutable_bunch = ImmutableBunch(name='John', age=35)
48
54
  print(my_immutable_bunch.name) # Output: John
49
55
  print(my_immutable_bunch['age']) # Output: 35
50
56
 
51
57
  # Attempting to modify an ImmutableBunch will raise an Exception
52
58
  my_immutable_bunch.name = 'Alice' # Raises ImmutableBunchException
59
+
60
+ # From Dictionary
61
+ my_dict = {'name': 'Alice', 'age': 40}
62
+ my_bunch_from_dict = Bunch.from_dict(my_dict, recursive=True)
63
+ print(my_bunch_from_dict) # Output: {'name': 'Alice', 'age': 40}
53
64
  ```
@@ -1,6 +1,7 @@
1
1
  ## Bunch
2
2
 
3
- A simple Python class that allows dictionary-style and attribute-style access to data interchangeably. Think of it as a lightweight object wrapper for dictionaries — great for config objects, JSON responses, or anything else you'd normally throw in a dict.
3
+ A simple Python class that allows dictionary-style and attribute-style access to data interchangeably.
4
+ Think of it as a lightweight object wrapper for dictionaries — great for config objects, JSON responses, or anything else you'd normally throw in a dict.
4
5
 
5
6
  ### <ins> Features </ins>
6
7
 
@@ -20,18 +21,28 @@ You can install this package via PIP: _pip install python-bunch_
20
21
  # - Mutable Bunch -
21
22
  from bunch.bunch import Bunch
22
23
 
23
- my_bunch = Bunch({'name': 'Jane', 'age': 30})
24
+ my_bunch = Bunch(name='Jane', age=30)
24
25
 
25
26
  print(my_bunch.name) # Output: Jane
26
27
  print(my_bunch['age']) # Output: 30
27
28
 
28
- # - Immutable Bunch -v
29
+ # From Dictionary
30
+ my_dict = {'name': 'Bob', 'age': 25}
31
+ my_bunch_from_dict = Bunch.from_dict(my_dict)
32
+ print(my_bunch_from_dict) # Output: {'name': 'Bob', 'age': 25}
33
+
34
+ # - Immutable Bunch -
29
35
  from bunch.immutable_bunch import ImmutableBunch
30
36
 
31
- my_immutable_bunch = ImmutableBunch({'name': 'John', 'age': 25})
37
+ my_immutable_bunch = ImmutableBunch(name='John', age=35)
32
38
  print(my_immutable_bunch.name) # Output: John
33
39
  print(my_immutable_bunch['age']) # Output: 35
34
40
 
35
41
  # Attempting to modify an ImmutableBunch will raise an Exception
36
42
  my_immutable_bunch.name = 'Alice' # Raises ImmutableBunchException
43
+
44
+ # From Dictionary
45
+ my_dict = {'name': 'Alice', 'age': 40}
46
+ my_bunch_from_dict = Bunch.from_dict(my_dict, recursive=True)
47
+ print(my_bunch_from_dict) # Output: {'name': 'Alice', 'age': 40}
37
48
  ```
@@ -22,7 +22,7 @@ class Bunch:
22
22
  return key in self.__dict__
23
23
 
24
24
  def __str__(self) -> str:
25
- return json.dumps(self.__dict__, sort_keys=False)
25
+ return json.dumps(self.to_dict(), sort_keys=False)
26
26
 
27
27
  def __repr__(self) -> str:
28
28
  return self.__str__()
@@ -82,3 +82,17 @@ class Bunch:
82
82
  else:
83
83
  ret.update(dictionary)
84
84
  return ret
85
+
86
+ def to_dict(self) -> dict:
87
+ def convert(value):
88
+ if isinstance(value, Bunch):
89
+ return value.to_dict()
90
+ elif isinstance(value, list):
91
+ return [convert(item) for item in value]
92
+ elif isinstance(value, dict):
93
+ return {k: convert(v) for k, v in value.items()}
94
+ else:
95
+ return value
96
+
97
+ return {key: convert(value) for key, value in self.__dict__.items()}
98
+
@@ -1,7 +1,9 @@
1
1
  import json
2
2
  from typing import Any
3
+
3
4
  from bunch.bunch import Bunch
4
5
 
6
+
5
7
  class ImmutableBunchException(Exception):
6
8
  def __init__(self, message: str) -> None:
7
9
  super().__init__(message)
@@ -27,7 +29,7 @@ class ImmutableBunch:
27
29
  return key in self.__dict__
28
30
 
29
31
  def __str__(self) -> str:
30
- return json.dumps(self.__dict__, sort_keys=False)
32
+ return json.dumps(self.to_dict(), sort_keys=False)
31
33
 
32
34
  def __repr__(self) -> str:
33
35
  return self.__str__()
@@ -87,3 +89,16 @@ class ImmutableBunch:
87
89
  else:
88
90
  ret.update(dictionary)
89
91
  return ImmutableBunch(**ret)
92
+
93
+ def to_dict(self) -> dict:
94
+ def convert(value):
95
+ if isinstance(value, ImmutableBunch):
96
+ return value.to_dict()
97
+ elif isinstance(value, list):
98
+ return [convert(item) for item in value]
99
+ elif isinstance(value, dict):
100
+ return {k: convert(v) for k, v in value.items()}
101
+ else:
102
+ return value
103
+
104
+ return {key: convert(value) for key, value in self.__dict__.items()}
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "python-bunch"
7
- version = "0.1.7"
7
+ version = "0.1.9"
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"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-bunch
3
- Version: 0.1.7
3
+ Version: 0.1.9
4
4
  Summary: A lightweight Python class that behaves like a dict but supports attribute-style access.
5
5
  Author-email: Omer Menashe <unspecified@mail.com>
6
6
  License: MIT
@@ -16,7 +16,8 @@ Dynamic: license-file
16
16
 
17
17
  ## Bunch
18
18
 
19
- A simple Python class that allows dictionary-style and attribute-style access to data interchangeably. Think of it as a lightweight object wrapper for dictionaries — great for config objects, JSON responses, or anything else you'd normally throw in a dict.
19
+ A simple Python class that allows dictionary-style and attribute-style access to data interchangeably.
20
+ Think of it as a lightweight object wrapper for dictionaries — great for config objects, JSON responses, or anything else you'd normally throw in a dict.
20
21
 
21
22
  ### <ins> Features </ins>
22
23
 
@@ -36,18 +37,28 @@ You can install this package via PIP: _pip install python-bunch_
36
37
  # - Mutable Bunch -
37
38
  from bunch.bunch import Bunch
38
39
 
39
- my_bunch = Bunch({'name': 'Jane', 'age': 30})
40
+ my_bunch = Bunch(name='Jane', age=30)
40
41
 
41
42
  print(my_bunch.name) # Output: Jane
42
43
  print(my_bunch['age']) # Output: 30
43
44
 
44
- # - Immutable Bunch -v
45
+ # From Dictionary
46
+ my_dict = {'name': 'Bob', 'age': 25}
47
+ my_bunch_from_dict = Bunch.from_dict(my_dict)
48
+ print(my_bunch_from_dict) # Output: {'name': 'Bob', 'age': 25}
49
+
50
+ # - Immutable Bunch -
45
51
  from bunch.immutable_bunch import ImmutableBunch
46
52
 
47
- my_immutable_bunch = ImmutableBunch({'name': 'John', 'age': 25})
53
+ my_immutable_bunch = ImmutableBunch(name='John', age=35)
48
54
  print(my_immutable_bunch.name) # Output: John
49
55
  print(my_immutable_bunch['age']) # Output: 35
50
56
 
51
57
  # Attempting to modify an ImmutableBunch will raise an Exception
52
58
  my_immutable_bunch.name = 'Alice' # Raises ImmutableBunchException
59
+
60
+ # From Dictionary
61
+ my_dict = {'name': 'Alice', 'age': 40}
62
+ my_bunch_from_dict = Bunch.from_dict(my_dict, recursive=True)
63
+ print(my_bunch_from_dict) # Output: {'name': 'Alice', 'age': 40}
53
64
  ```
File without changes
File without changes