python-bunch 0.1.0__py3-none-any.whl → 0.1.1__py3-none-any.whl

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.
bunch/bunch.py CHANGED
@@ -9,27 +9,53 @@ class Bunch:
9
9
  kwargs[arg] = None
10
10
  self.__dict__.update(kwargs)
11
11
 
12
- def __getitem__(self, key):
13
- return self.__dict__[key]
12
+ def __getitem__(self, key: Any) -> Any or None:
13
+ return self.__dict__.get(key, None)
14
14
 
15
- def __setitem__(self, key, value):
15
+ def __setitem__(self, key: Any, value: Any) -> None:
16
16
  self.__dict__[key] = value
17
17
 
18
- def __contains__(self, key):
18
+ def __delitem__(self, key: Any) -> None:
19
+ del self.__dict__[key]
20
+
21
+ def __contains__(self, key: Any) -> bool:
19
22
  return key in self.__dict__
20
23
 
21
- def __str__(self):
22
- return json.dumps(self.__dict__, indent=4, sort_keys=False)
24
+ def __str__(self) -> str:
25
+ return json.dumps(self.__dict__, sort_keys=False)
23
26
 
24
- def __repr__(self):
27
+ def __repr__(self) -> str:
25
28
  return self.__str__()
26
29
 
27
- def __delitem__(self, key):
28
- del self.__dict__[key]
30
+ def __getattr__(self, key: Any) -> Any or None:
31
+ if key in self.__dict__:
32
+ return self.__dict__[key]
33
+ return None
34
+
35
+ def __setattr__(self, name: str, value: Any) -> None:
36
+ self.__dict__[name] = value
37
+
38
+ def __delattr__(self, name) -> None:
39
+ del self.__dict__[name]
29
40
 
30
- def contains_value(self, value):
41
+ def contains_value(self, value: Any) -> bool:
31
42
  return value in self.__dict__.values()
32
43
 
44
+ def clear(self) -> None:
45
+ self.__dict__.clear()
46
+
47
+ def pop(self, key: Any, default: Any = None) -> Any or None:
48
+ return self.__dict__.pop(key, default)
49
+
50
+ def popitem(self) -> Any or None:
51
+ return self.__dict__.popitem()
52
+
53
+ def update(self, other: dict) -> None:
54
+ self.__dict__.update(other)
55
+
56
+ def setdefault(self, key: Any, default: Any = None) -> Any or None:
57
+ return self.__dict__.setdefault(key, default)
58
+
33
59
  def keys(self):
34
60
  return self.__dict__.keys()
35
61
 
@@ -0,0 +1,75 @@
1
+ import json
2
+ from typing import Any
3
+
4
+
5
+ class ImmutableBunchException(Exception):
6
+ def __init__(self, message: str) -> None:
7
+ super().__init__(message)
8
+
9
+
10
+ class ImmutableBunch:
11
+ def __init__(self, *args, **kwargs):
12
+ for arg in args:
13
+ if not isinstance(arg, dict):
14
+ kwargs[arg] = None
15
+ self.__dict__.update(kwargs)
16
+
17
+ def __getitem__(self, key: Any) -> Any or None:
18
+ return self.__dict__.get(key, None)
19
+
20
+ def __setitem__(self, key: Any, value: Any) -> None:
21
+ raise ImmutableBunchException('ImmutableBunch does not support item assignment')
22
+
23
+ def __delitem__(self, key: Any) -> None:
24
+ raise ImmutableBunchException('ImmutableBunch does not support item deletion')
25
+
26
+ def __contains__(self, key: Any) -> bool:
27
+ return key in self.__dict__
28
+
29
+ def __str__(self) -> str:
30
+ return json.dumps(self.__dict__, sort_keys=False)
31
+
32
+ def __repr__(self) -> str:
33
+ return self.__str__()
34
+
35
+ def __getattr__(self, key: Any) -> Any or None:
36
+ if key in self.__dict__:
37
+ return self.__dict__[key]
38
+ return None
39
+
40
+ def __setattr__(self, name: str, value: Any) -> None:
41
+ raise ImmutableBunchException('ImmutableBunch does not support attribute assignment')
42
+
43
+ def __delattr__(self, name) -> None:
44
+ raise ImmutableBunchException('ImmutableBunch does not support attribute deletion')
45
+
46
+ def contains_value(self, value: Any) -> bool:
47
+ return value in self.__dict__.values()
48
+
49
+ def clear(self) -> None:
50
+ raise ImmutableBunchException('ImmutableBunch does not support clearing')
51
+
52
+ def pop(self, key: Any, default: Any = None) -> Any or None:
53
+ raise ImmutableBunchException('ImmutableBunch does not support popping')
54
+
55
+ def popitem(self) -> Any or None:
56
+ raise ImmutableBunchException('ImmutableBunch does not support popitem')
57
+
58
+ def update(self, other: dict) -> None:
59
+ raise ImmutableBunchException('ImmutableBunch does not support update')
60
+
61
+ def setdefault(self, key: Any, default: Any = None) -> Any or None:
62
+ raise ImmutableBunchException('ImmutableBunch does not support setdefault')
63
+
64
+ def keys(self):
65
+ return self.__dict__.keys()
66
+
67
+ def values(self):
68
+ return self.__dict__.values()
69
+
70
+ def items(self):
71
+ return self.__dict__.items()
72
+
73
+ @staticmethod
74
+ def from_dict(dictionary: dict) -> Any:
75
+ return ImmutableBunch(**dictionary)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-bunch
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Summary: A lightweight Python class that behaves like a dict but supports attribute-style access.
5
5
  Author-email: Your Name <your.email@example.com>
6
6
  License: MIT
@@ -32,10 +32,21 @@ You can install this package via PIP: _pip install bunch_
32
32
  ### <ins> Usage </ins>
33
33
 
34
34
  ```python
35
- from bunch import Bunch
35
+ # - Mutable Bunch -
36
+ from bunch.bunch import Bunch
36
37
 
37
38
  my_bunch = Bunch({'name': 'Jane', 'age': 30})
38
39
 
39
40
  print(my_bunch.name) # Output: Jane
40
41
  print(my_bunch['age']) # Output: 30
42
+
43
+ # - Immutable Bunch -
44
+ from bunch.immutable_bunch import ImmutableBunch
45
+
46
+ my_immutable_bunch = ImmutableBunch({'name': 'John', 'age': 25})
47
+ print(my_immutable_bunch.name) # Output: John
48
+ print(my_immutable_bunch['age']) # Output: 35
49
+
50
+ # Attempting to modify an ImmutableBunch will raise an Exception
51
+ my_immutable_bunch.name = 'Alice' # Raises ImmutableBunchException
41
52
  ```
@@ -0,0 +1,8 @@
1
+ bunch/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ bunch/bunch.py,sha256=KThKq7aLFbig8EVnbXzZKcCaD4AVRFCF9XJUquWV5rw,1875
3
+ bunch/immutable_bunch.py,sha256=5tCZeMRSOAAv5bWhNKUKEWV8DE0LMTxS4OF1KmlbOCw,2447
4
+ python_bunch-0.1.1.dist-info/licenses/LICENSE,sha256=Hsoz6W43KBiNLAODKAcjo2witGQKjZ05gVU_mUD4eHs,1039
5
+ python_bunch-0.1.1.dist-info/METADATA,sha256=gHu1iroPrjCYe_lWlxo6btL9TdkFwMLpk4be3FCpfyA,1704
6
+ python_bunch-0.1.1.dist-info/WHEEL,sha256=lTU6B6eIfYoiQJTZNc-fyaR6BpL6ehTzU3xGYxn2n8k,91
7
+ python_bunch-0.1.1.dist-info/top_level.txt,sha256=A1w3sDjR8t1mZ4GDuhD9afxgybqy6nHJHPA4CbrgFRA,6
8
+ python_bunch-0.1.1.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (78.1.0)
2
+ Generator: setuptools (78.1.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,7 +0,0 @@
1
- bunch/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- bunch/bunch.py,sha256=OwDbw6bmpO4VU5taMl6gaqL6WRijMGMG18sQ5CmK7H4,1013
3
- python_bunch-0.1.0.dist-info/licenses/LICENSE,sha256=Hsoz6W43KBiNLAODKAcjo2witGQKjZ05gVU_mUD4eHs,1039
4
- python_bunch-0.1.0.dist-info/METADATA,sha256=4yNAae668lzJVoffU57_vCCwpKSpWjS9DfYRx3_aBNU,1312
5
- python_bunch-0.1.0.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
6
- python_bunch-0.1.0.dist-info/top_level.txt,sha256=A1w3sDjR8t1mZ4GDuhD9afxgybqy6nHJHPA4CbrgFRA,6
7
- python_bunch-0.1.0.dist-info/RECORD,,