PersistentObjects 0.1.3__tar.gz → 0.1.5__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.3
3
+ Version: 0.1.5
4
4
  Summary: JSON-backed attribute-persistent object with namespace support
5
5
  Author: Tornado300
6
6
  License-Expression: MIT
@@ -11,7 +11,7 @@ Description-Content-Type: text/markdown
11
11
  License-File: LICENSE
12
12
  Dynamic: license-file
13
13
 
14
- This is a very simple lib that provides the PersistantObject class, which saves all its attributes to the given json file.
14
+ This is a very simple lib that provides the PersistantObject class, which saves all its attributes to the given json file. Exceptions are attributes suffixed with '_'.
15
15
  Attributes are saved as soon as they are set.
16
16
 
17
17
  [!IMPORTANT]
@@ -31,6 +31,8 @@ pobject = PersistantObject("save.json")
31
31
  pobject.first_value = 10
32
32
  pobject.second_value = "foo"
33
33
 
34
- namespace = pobject.namespace("test_namespace")
34
+ namespace = pobject.namespace("test_namespace") # creates a new namespace (new subdict in json)
35
35
  namespace.third_value = [10, 5]
36
+
37
+ pobject.temp_ = True # suffixed with '_' so it will not be saved and behaves like a normal attribute.
36
38
  ```
@@ -0,0 +1,3 @@
1
+ from .core import PersistentObject
2
+
3
+ __all__ = ["PersistentObject"]
@@ -41,6 +41,11 @@ class _PersistentState:
41
41
  def __getattr__(self, name):
42
42
  if name.startswith("_"):
43
43
  raise AttributeError(name)
44
+ if name.endswith("_"):
45
+ try:
46
+ return super().__getattribute__(name[:-1])
47
+ except AttributeError:
48
+ raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'.\n Notice: Attribute '{name}' is not persistent!")
44
49
  try:
45
50
  return self._data[name]
46
51
  except KeyError:
@@ -51,17 +56,46 @@ class _PersistentState:
51
56
  super().__setattr__(name, value)
52
57
  return
53
58
 
59
+ if name.endswith("_"):
60
+ super().__setattr__(name[:-1], value)
61
+ return
62
+
54
63
  with self._lock:
55
64
  self._data[name] = value
56
65
  self._save()
57
66
 
58
67
 
59
68
  class _Namespace:
69
+ _instances = {}
70
+
71
+ def __new__(cls, root, key):
72
+ # Create unique identifier for this namespace
73
+ instance_key = (id(root), key)
74
+
75
+ if instance_key in cls._instances:
76
+ return cls._instances[instance_key]
77
+
78
+ instance = super().__new__(cls)
79
+ cls._instances[instance_key] = instance
80
+ return instance
81
+
60
82
  def __init__(self, root, key):
83
+ # Prevent re-initialization
84
+ if hasattr(self, "_initialized"):
85
+ return
86
+
87
+ self._initialized = True
61
88
  self._root = root
62
89
  self._key = key
63
90
 
64
91
  def __getattr__(self, name):
92
+ if name.startswith("_"):
93
+ raise AttributeError(name)
94
+ if name.endswith("_"):
95
+ try:
96
+ return super().__getattribute__(name[:-1])
97
+ except AttributeError:
98
+ raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'.\n Notice: Attribute '{name}' is not persistent!")
65
99
  try:
66
100
  return self._root._data[self._key][name]
67
101
  except KeyError:
@@ -72,6 +106,10 @@ class _Namespace:
72
106
  super().__setattr__(name, value)
73
107
  return
74
108
 
109
+ if name.endswith("_"):
110
+ super().__setattr__(name[:-1], value)
111
+ return
112
+
75
113
  with self._root._lock:
76
114
  bucket = self._root._data.setdefault(self._key, {})
77
115
  bucket[name] = value
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PersistentObjects
3
- Version: 0.1.3
3
+ Version: 0.1.5
4
4
  Summary: JSON-backed attribute-persistent object with namespace support
5
5
  Author: Tornado300
6
6
  License-Expression: MIT
@@ -11,7 +11,7 @@ Description-Content-Type: text/markdown
11
11
  License-File: LICENSE
12
12
  Dynamic: license-file
13
13
 
14
- This is a very simple lib that provides the PersistantObject class, which saves all its attributes to the given json file.
14
+ This is a very simple lib that provides the PersistantObject class, which saves all its attributes to the given json file. Exceptions are attributes suffixed with '_'.
15
15
  Attributes are saved as soon as they are set.
16
16
 
17
17
  [!IMPORTANT]
@@ -31,6 +31,8 @@ pobject = PersistantObject("save.json")
31
31
  pobject.first_value = 10
32
32
  pobject.second_value = "foo"
33
33
 
34
- namespace = pobject.namespace("test_namespace")
34
+ namespace = pobject.namespace("test_namespace") # creates a new namespace (new subdict in json)
35
35
  namespace.third_value = [10, 5]
36
+
37
+ pobject.temp_ = True # suffixed with '_' so it will not be saved and behaves like a normal attribute.
36
38
  ```
@@ -2,7 +2,7 @@ LICENSE
2
2
  README.md
3
3
  pyproject.toml
4
4
  PersistentObjects/__init__.py
5
- PersistentObjects/__main__.py
5
+ PersistentObjects/core.py
6
6
  PersistentObjects.egg-info/PKG-INFO
7
7
  PersistentObjects.egg-info/SOURCES.txt
8
8
  PersistentObjects.egg-info/dependency_links.txt
@@ -1,4 +1,4 @@
1
- This is a very simple lib that provides the PersistantObject class, which saves all its attributes to the given json file.
1
+ This is a very simple lib that provides the PersistantObject class, which saves all its attributes to the given json file. Exceptions are attributes suffixed with '_'.
2
2
  Attributes are saved as soon as they are set.
3
3
 
4
4
  [!IMPORTANT]
@@ -18,6 +18,8 @@ pobject = PersistantObject("save.json")
18
18
  pobject.first_value = 10
19
19
  pobject.second_value = "foo"
20
20
 
21
- namespace = pobject.namespace("test_namespace")
21
+ namespace = pobject.namespace("test_namespace") # creates a new namespace (new subdict in json)
22
22
  namespace.third_value = [10, 5]
23
+
24
+ pobject.temp_ = True # suffixed with '_' so it will not be saved and behaves like a normal attribute.
23
25
  ```
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "PersistentObjects"
7
- version = "0.1.3"
7
+ version = "0.1.5"
8
8
  description = "JSON-backed attribute-persistent object with namespace support"
9
9
  authors = [
10
10
  { name="Tornado300" }
@@ -1,3 +0,0 @@
1
- from PersistentObjects.__main__ import PersistentObject
2
-
3
- __all__ = ["PersistentObject"]