forked from inaos/iron-array-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattrs.py
More file actions
82 lines (65 loc) · 2.3 KB
/
attrs.py
File metadata and controls
82 lines (65 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
###########################################################################################
# Copyright ironArray SL 2021.
#
# All rights reserved.
#
# This software is the confidential and proprietary information of ironArray SL
# ("Confidential Information"). You shall not disclose such Confidential Information
# and shall use it only in accordance with the terms of the license agreement.
###########################################################################################
from iarray import iarray_ext as ext
import iarray as ia
from collections.abc import MutableMapping
from msgpack import packb, unpackb
class Attributes(MutableMapping):
def __init__(self, a: ia.IArray):
self.iarr = a
def __getitem__(self, name):
"""Get the content of an attr.
Parameters
----------
name : str or byte string
The name of the attr to return.
Returns
-------
object :
The unpacked content of the attr.
"""
packed_content = ext.attr_getitem(self.iarr, name)
return unpackb(packed_content)
def __setitem__(self, name, content):
"""Add or update an attr.
Its content will be packed with msgpack and unpacked when getting it.
Parameters
----------
name : str or byte string
The name of the attribute.
content : object
The content of the attr. The object must be an object that can be serialized by `msgpack.packb`.
The getitem counterpart will `msgpack.unpackb` it automatically.
Returns
-------
None
"""
packed_content = packb(content)
ext.attr_setitem(self.iarr, name, packed_content)
def __delitem__(self, name):
"""Delete the attr given by :paramref:`name`.
Parameters
----------
name : str or byte string
The name of the attr.
"""
ext.attr_delitem(self.iarr, name)
def __iter__(self):
keys = ext.attr_get_names(self.iarr)
for name in keys:
yield name
def __len__(self):
return ext.attr_len(self.iarr)
def clear(self):
for attr in self.keys():
if attr == "zproxy_urlpath":
continue
else:
self.__delitem__(attr)