ambry 0.1.2__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.
- ambry-0.1.2/LICENSE +21 -0
- ambry-0.1.2/PKG-INFO +32 -0
- ambry-0.1.2/README.md +18 -0
- ambry-0.1.2/pyproject.toml +24 -0
- ambry-0.1.2/setup.cfg +4 -0
- ambry-0.1.2/src/ambry/__init__.py +240 -0
- ambry-0.1.2/src/ambry.egg-info/PKG-INFO +32 -0
- ambry-0.1.2/src/ambry.egg-info/SOURCES.txt +8 -0
- ambry-0.1.2/src/ambry.egg-info/dependency_links.txt +1 -0
- ambry-0.1.2/src/ambry.egg-info/top_level.txt +1 -0
ambry-0.1.2/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CocosPYPI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
ambry-0.1.2/PKG-INFO
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ambry
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: A memory-efficient, iterable storage container with a limit.
|
|
5
|
+
Author-email: Your Name <you@example.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/yourusername/ambry
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# ambry
|
|
16
|
+
|
|
17
|
+
A memory-efficient iterable storage container with a limit.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
pip install ambry
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
from ambry import ambry
|
|
26
|
+
|
|
27
|
+
c = ambry(5, ["a", "b", "c"])
|
|
28
|
+
print(c.contains) # ['a', 'b', 'c']
|
|
29
|
+
|
|
30
|
+
## Security warning!
|
|
31
|
+
|
|
32
|
+
`use_pickle=True` enables pickle serialization. Never unpickle data from untrusted sources — it can execute arbitrary code [citation:15].
|
ambry-0.1.2/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# ambry
|
|
2
|
+
|
|
3
|
+
A memory-efficient iterable storage container with a limit.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
pip install ambry
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
from ambry import ambry
|
|
12
|
+
|
|
13
|
+
c = ambry(5, ["a", "b", "c"])
|
|
14
|
+
print(c.contains) # ['a', 'b', 'c']
|
|
15
|
+
|
|
16
|
+
## Security warning!
|
|
17
|
+
|
|
18
|
+
`use_pickle=True` enables pickle serialization. Never unpickle data from untrusted sources — it can execute arbitrary code [citation:15].
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "ambry"
|
|
7
|
+
version = "0.1.2"
|
|
8
|
+
description = "A memory-efficient, iterable storage container with a limit."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
authors = [
|
|
13
|
+
{name = "Your Name", email = "you@example.com"}
|
|
14
|
+
]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[project.urls]
|
|
21
|
+
Homepage = "https://github.com/yourusername/ambry"
|
|
22
|
+
|
|
23
|
+
[tool.setuptools]
|
|
24
|
+
package-dir = {"" = "src"}
|
ambry-0.1.2/setup.cfg
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
"""ambry — a memory-efficient, iterable storage container with a limit."""
|
|
2
|
+
|
|
3
|
+
import struct
|
|
4
|
+
import pickle
|
|
5
|
+
from collections.abc import Iterable
|
|
6
|
+
|
|
7
|
+
__version__ = "0.1.2"
|
|
8
|
+
__all__ = ["ambry", "DoorException"]
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class DoorException(Exception):
|
|
12
|
+
"""Raised when the ambry is closed or destroyed."""
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ambry:
|
|
17
|
+
"""
|
|
18
|
+
A memory-efficient iterable storage container with a limit.
|
|
19
|
+
|
|
20
|
+
Stores elements serialized in a single bytearray.
|
|
21
|
+
Supports str, bytes, bool, int, float, tuple, set, frozenset,
|
|
22
|
+
list, and any other Iterable (converted to list).
|
|
23
|
+
|
|
24
|
+
dict is NOT supported (even with use_pickle=True).
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
limit: Maximum number of elements (default 32).
|
|
28
|
+
items: Optional iterable of initial elements.
|
|
29
|
+
use_pickle: If True, serialize any object via pickle (default False).
|
|
30
|
+
WARNING: pickle is insecure with untrusted data.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
__slots__ = ("_limit", "_data", "_published", "_alive", "_count", "_use_pickle")
|
|
34
|
+
|
|
35
|
+
def __init__(self, limit: int = 32, items=None, use_pickle: bool = False):
|
|
36
|
+
if not isinstance(limit, int) or isinstance(limit, bool):
|
|
37
|
+
raise TypeError("limit must be an integer")
|
|
38
|
+
if limit < 0:
|
|
39
|
+
raise ValueError("limit must be non-negative")
|
|
40
|
+
|
|
41
|
+
self._limit = limit
|
|
42
|
+
self._data = bytearray()
|
|
43
|
+
self._published = True
|
|
44
|
+
self._alive = True
|
|
45
|
+
self._count = 0
|
|
46
|
+
self._use_pickle = use_pickle
|
|
47
|
+
|
|
48
|
+
if items is not None:
|
|
49
|
+
try:
|
|
50
|
+
iterator = iter(items)
|
|
51
|
+
except TypeError:
|
|
52
|
+
raise TypeError("items must be an iterable")
|
|
53
|
+
for element in iterator:
|
|
54
|
+
self.att(element)
|
|
55
|
+
|
|
56
|
+
# --- Internal ---
|
|
57
|
+
def _check(self):
|
|
58
|
+
if not self._alive:
|
|
59
|
+
raise DoorException("Ambry has been destroyed")
|
|
60
|
+
if not self._published:
|
|
61
|
+
raise DoorException("Ambry is closed")
|
|
62
|
+
|
|
63
|
+
def _serialize(self, element) -> bytes:
|
|
64
|
+
if isinstance(element, dict):
|
|
65
|
+
raise TypeError("dict is not supported by ambry")
|
|
66
|
+
|
|
67
|
+
if self._use_pickle:
|
|
68
|
+
raw = pickle.dumps(element)
|
|
69
|
+
return b"P" + struct.pack(">I", len(raw)) + raw
|
|
70
|
+
|
|
71
|
+
if isinstance(element, str):
|
|
72
|
+
raw = element.encode("utf-8")
|
|
73
|
+
return b"S" + struct.pack(">I", len(raw)) + raw
|
|
74
|
+
if isinstance(element, bytes):
|
|
75
|
+
return b"Y" + struct.pack(">I", len(element)) + element
|
|
76
|
+
if isinstance(element, bool):
|
|
77
|
+
return b"B" + bytes([element])
|
|
78
|
+
if isinstance(element, int):
|
|
79
|
+
return b"I" + struct.pack(">q", element)
|
|
80
|
+
if isinstance(element, float):
|
|
81
|
+
return b"F" + struct.pack(">d", element)
|
|
82
|
+
if isinstance(element, tuple):
|
|
83
|
+
items = list(element)
|
|
84
|
+
raw = b"".join(self._serialize(x) for x in items)
|
|
85
|
+
return b"T" + struct.pack(">I", len(items)) + raw
|
|
86
|
+
if isinstance(element, frozenset):
|
|
87
|
+
items = list(element)
|
|
88
|
+
raw = b"".join(self._serialize(x) for x in items)
|
|
89
|
+
return b"O" + struct.pack(">I", len(items)) + raw
|
|
90
|
+
if isinstance(element, set):
|
|
91
|
+
items = list(element)
|
|
92
|
+
raw = b"".join(self._serialize(x) for x in items)
|
|
93
|
+
return b"E" + struct.pack(">I", len(items)) + raw
|
|
94
|
+
if isinstance(element, list):
|
|
95
|
+
items = list(element)
|
|
96
|
+
raw = b"".join(self._serialize(x) for x in items)
|
|
97
|
+
return b"L" + struct.pack(">I", len(items)) + raw
|
|
98
|
+
if isinstance(element, Iterable):
|
|
99
|
+
items = list(element)
|
|
100
|
+
raw = b"".join(self._serialize(x) for x in items)
|
|
101
|
+
return b"G" + struct.pack(">I", len(items)) + raw
|
|
102
|
+
raise TypeError(f"Unsupported type: {type(element).__name__}. Use use_pickle=True for arbitrary objects.")
|
|
103
|
+
|
|
104
|
+
def _deserialize(self, buf: memoryview, offset: int):
|
|
105
|
+
if offset >= len(buf):
|
|
106
|
+
raise ValueError(f"Buffer underflow at offset {offset}")
|
|
107
|
+
tag = buf[offset:offset + 1]
|
|
108
|
+
offset += 1
|
|
109
|
+
|
|
110
|
+
if tag == b"P":
|
|
111
|
+
length = struct.unpack_from(">I", buf, offset)[0]
|
|
112
|
+
offset += 4
|
|
113
|
+
raw = bytes(buf[offset:offset + length])
|
|
114
|
+
return pickle.loads(raw), offset + length
|
|
115
|
+
if tag == b"S":
|
|
116
|
+
length = struct.unpack_from(">I", buf, offset)[0]
|
|
117
|
+
offset += 4
|
|
118
|
+
value = bytes(buf[offset:offset + length]).decode("utf-8")
|
|
119
|
+
return value, offset + length
|
|
120
|
+
if tag == b"Y":
|
|
121
|
+
length = struct.unpack_from(">I", buf, offset)[0]
|
|
122
|
+
offset += 4
|
|
123
|
+
value = bytes(buf[offset:offset + length])
|
|
124
|
+
return value, offset + length
|
|
125
|
+
if tag == b"B":
|
|
126
|
+
return bool(buf[offset]), offset + 1
|
|
127
|
+
if tag == b"I":
|
|
128
|
+
value = struct.unpack_from(">q", buf, offset)[0]
|
|
129
|
+
return value, offset + 8
|
|
130
|
+
if tag == b"F":
|
|
131
|
+
value = struct.unpack_from(">d", buf, offset)[0]
|
|
132
|
+
return value, offset + 8
|
|
133
|
+
|
|
134
|
+
if tag in (b"O", b"T", b"E", b"L", b"G"):
|
|
135
|
+
count = struct.unpack_from(">I", buf, offset)[0]
|
|
136
|
+
offset += 4
|
|
137
|
+
items = []
|
|
138
|
+
for _ in range(count):
|
|
139
|
+
item, offset = self._deserialize(buf, offset)
|
|
140
|
+
items.append(item)
|
|
141
|
+
if tag == b"O":
|
|
142
|
+
return frozenset(items), offset
|
|
143
|
+
if tag == b"T":
|
|
144
|
+
return tuple(items), offset
|
|
145
|
+
if tag == b"E":
|
|
146
|
+
return set(items), offset
|
|
147
|
+
if tag == b"L":
|
|
148
|
+
return items, offset
|
|
149
|
+
return items, offset
|
|
150
|
+
|
|
151
|
+
raise ValueError(f"Unknown tag: {tag!r}")
|
|
152
|
+
|
|
153
|
+
def _all_items(self):
|
|
154
|
+
offset = 0
|
|
155
|
+
buf = memoryview(self._data)
|
|
156
|
+
for _ in range(self._count):
|
|
157
|
+
item, offset = self._deserialize(buf, offset)
|
|
158
|
+
yield item
|
|
159
|
+
|
|
160
|
+
# --- Public API ---
|
|
161
|
+
def publish(self):
|
|
162
|
+
self._published = True
|
|
163
|
+
|
|
164
|
+
def private(self):
|
|
165
|
+
self._published = False
|
|
166
|
+
|
|
167
|
+
@property
|
|
168
|
+
def cangrab(self) -> bool:
|
|
169
|
+
return self._published
|
|
170
|
+
|
|
171
|
+
@property
|
|
172
|
+
def contains(self):
|
|
173
|
+
self._check()
|
|
174
|
+
return list(self._all_items())
|
|
175
|
+
|
|
176
|
+
def att(self, element):
|
|
177
|
+
self._check()
|
|
178
|
+
if self._count + 1 > self._limit:
|
|
179
|
+
return
|
|
180
|
+
self._data += self._serialize(element)
|
|
181
|
+
self._count += 1
|
|
182
|
+
|
|
183
|
+
def disatt(self, index: int):
|
|
184
|
+
self._check()
|
|
185
|
+
if not (0 <= index < self._count):
|
|
186
|
+
raise ValueError(f"Index {index} out of range")
|
|
187
|
+
items = self.contains
|
|
188
|
+
del items[index]
|
|
189
|
+
self._data = bytearray()
|
|
190
|
+
self._count = 0
|
|
191
|
+
for item in items:
|
|
192
|
+
self._data += self._serialize(item)
|
|
193
|
+
self._count += 1
|
|
194
|
+
|
|
195
|
+
def disact(self, arg: list = None):
|
|
196
|
+
if not self._alive:
|
|
197
|
+
raise DoorException("Ambry already destroyed")
|
|
198
|
+
if arg is not None:
|
|
199
|
+
if not isinstance(arg, list):
|
|
200
|
+
raise TypeError("arg must be a list or None")
|
|
201
|
+
arg.extend(self._all_items())
|
|
202
|
+
self._alive = False
|
|
203
|
+
self._data = bytearray()
|
|
204
|
+
self._count = 0
|
|
205
|
+
|
|
206
|
+
# --- Class methods ---
|
|
207
|
+
@classmethod
|
|
208
|
+
def filter(cls, iterable, predicate, limit=32):
|
|
209
|
+
return cls(limit, (x for x in iterable if predicate(x)))
|
|
210
|
+
|
|
211
|
+
@classmethod
|
|
212
|
+
def merge(cls, *iterables, limit=32):
|
|
213
|
+
def chain():
|
|
214
|
+
for it in iterables:
|
|
215
|
+
yield from it
|
|
216
|
+
return cls(limit, chain())
|
|
217
|
+
|
|
218
|
+
@classmethod
|
|
219
|
+
def from_file(cls, path, fmt, limit=32):
|
|
220
|
+
s = struct.Struct(fmt)
|
|
221
|
+
with open(path, "rb") as f:
|
|
222
|
+
raw = f.read()
|
|
223
|
+
if len(raw) % s.size != 0:
|
|
224
|
+
raise ValueError("File size is not a multiple of struct size")
|
|
225
|
+
elements = (s.unpack_from(raw, i)[0] for i in range(0, len(raw), s.size))
|
|
226
|
+
return cls(limit, elements)
|
|
227
|
+
|
|
228
|
+
# --- Dunder ---
|
|
229
|
+
def __len__(self):
|
|
230
|
+
return self._count
|
|
231
|
+
|
|
232
|
+
def __iter__(self):
|
|
233
|
+
self._check()
|
|
234
|
+
return self._all_items()
|
|
235
|
+
|
|
236
|
+
def __repr__(self):
|
|
237
|
+
state = "open" if self._published else "closed"
|
|
238
|
+
if not self._alive:
|
|
239
|
+
state = "dead"
|
|
240
|
+
return f"<ambry {state}, {self._count}/{self._limit}>"
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ambry
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: A memory-efficient, iterable storage container with a limit.
|
|
5
|
+
Author-email: Your Name <you@example.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/yourusername/ambry
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# ambry
|
|
16
|
+
|
|
17
|
+
A memory-efficient iterable storage container with a limit.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
pip install ambry
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
from ambry import ambry
|
|
26
|
+
|
|
27
|
+
c = ambry(5, ["a", "b", "c"])
|
|
28
|
+
print(c.contains) # ['a', 'b', 'c']
|
|
29
|
+
|
|
30
|
+
## Security warning!
|
|
31
|
+
|
|
32
|
+
`use_pickle=True` enables pickle serialization. Never unpickle data from untrusted sources — it can execute arbitrary code [citation:15].
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ambry
|