PersistentObjects 0.1.4__tar.gz → 0.1.6__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: PersistentObjects
3
- Version: 0.1.4
3
+ Version: 0.1.6
4
4
  Summary: JSON-backed attribute-persistent object with namespace support
5
5
  Author: Tornado300
6
6
  License-Expression: MIT
@@ -34,5 +34,8 @@ pobject.second_value = "foo"
34
34
  namespace = pobject.namespace("test_namespace") # creates a new namespace (new subdict in json)
35
35
  namespace.third_value = [10, 5]
36
36
 
37
+
38
+ print(pobject.test_namespace.third_value) # alternate way to access namespaces
39
+
37
40
  pobject.temp_ = True # suffixed with '_' so it will not be saved and behaves like a normal attribute.
38
41
  ```
@@ -1,5 +1,6 @@
1
1
  import json, os, threading
2
2
 
3
+
3
4
  class _PersistentState:
4
5
  _instances = {}
5
6
 
@@ -40,16 +41,24 @@ class _PersistentState:
40
41
 
41
42
  def __getattr__(self, name):
42
43
  if name.startswith("_"):
43
- raise AttributeError(name)
44
+ raise AttributeError(f"Cannot access private attribute '{name}'")
44
45
  if name.endswith("_"):
45
- try:
46
+ try:
46
47
  return super().__getattribute__(name[:-1])
47
48
  except AttributeError:
48
- raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'.\n Notice: Attribute '{name}' is not persistent!")
49
+ raise AttributeError(
50
+ f"'{self.__class__.__name__}' object has no attribute '{name}'.\n Notice: Attribute '{name}' is not persistent!"
51
+ )
49
52
  try:
50
- return self._data[name]
53
+ value = self._data[name]
54
+ # If it's a dict, return it as a namespace
55
+ if isinstance(value, dict):
56
+ return _Namespace(self, name)
57
+ return value
51
58
  except KeyError:
52
- raise AttributeError(name)
59
+ raise AttributeError(
60
+ f"'{type(self).__name__}' object has no attribute '{name}'"
61
+ )
53
62
 
54
63
  def __setattr__(self, name, value):
55
64
  if name.startswith("_"):
@@ -66,11 +75,38 @@ class _PersistentState:
66
75
 
67
76
 
68
77
  class _Namespace:
78
+ _instances = {}
79
+
80
+ def __new__(cls, root, key):
81
+ # Create unique identifier for this namespace
82
+ instance_key = (id(root), key)
83
+
84
+ if instance_key in cls._instances:
85
+ return cls._instances[instance_key]
86
+
87
+ instance = super().__new__(cls)
88
+ cls._instances[instance_key] = instance
89
+ return instance
90
+
69
91
  def __init__(self, root, key):
92
+ # Prevent re-initialization
93
+ if hasattr(self, "_initialized"):
94
+ return
95
+
96
+ self._initialized = True
70
97
  self._root = root
71
98
  self._key = key
72
99
 
73
100
  def __getattr__(self, name):
101
+ if name.startswith("_"):
102
+ raise AttributeError(name)
103
+ if name.endswith("_"):
104
+ try:
105
+ return super().__getattribute__(name[:-1])
106
+ except AttributeError:
107
+ raise AttributeError(
108
+ f"'{self.__class__.__name__}' object has no attribute '{name}'.\n Notice: Attribute '{name}' is not persistent!"
109
+ )
74
110
  try:
75
111
  return self._root._data[self._key][name]
76
112
  except KeyError:
@@ -81,6 +117,10 @@ class _Namespace:
81
117
  super().__setattr__(name, value)
82
118
  return
83
119
 
120
+ if name.endswith("_"):
121
+ super().__setattr__(name[:-1], value)
122
+ return
123
+
84
124
  with self._root._lock:
85
125
  bucket = self._root._data.setdefault(self._key, {})
86
126
  bucket[name] = value
@@ -92,6 +132,4 @@ class PersistentObject(_PersistentState):
92
132
  return _Namespace(self, name)
93
133
 
94
134
 
95
-
96
-
97
135
  __all__ = ["PersistentObject"]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PersistentObjects
3
- Version: 0.1.4
3
+ Version: 0.1.6
4
4
  Summary: JSON-backed attribute-persistent object with namespace support
5
5
  Author: Tornado300
6
6
  License-Expression: MIT
@@ -34,5 +34,8 @@ pobject.second_value = "foo"
34
34
  namespace = pobject.namespace("test_namespace") # creates a new namespace (new subdict in json)
35
35
  namespace.third_value = [10, 5]
36
36
 
37
+
38
+ print(pobject.test_namespace.third_value) # alternate way to access namespaces
39
+
37
40
  pobject.temp_ = True # suffixed with '_' so it will not be saved and behaves like a normal attribute.
38
41
  ```
@@ -21,5 +21,8 @@ pobject.second_value = "foo"
21
21
  namespace = pobject.namespace("test_namespace") # creates a new namespace (new subdict in json)
22
22
  namespace.third_value = [10, 5]
23
23
 
24
+
25
+ print(pobject.test_namespace.third_value) # alternate way to access namespaces
26
+
24
27
  pobject.temp_ = True # suffixed with '_' so it will not be saved and behaves like a normal attribute.
25
28
  ```
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "PersistentObjects"
7
- version = "0.1.4"
7
+ version = "0.1.6"
8
8
  description = "JSON-backed attribute-persistent object with namespace support"
9
9
  authors = [
10
10
  { name="Tornado300" }