-
Notifications
You must be signed in to change notification settings - Fork 434
Expand file tree
/
Copy pathdictionary.py
More file actions
74 lines (60 loc) · 2.19 KB
/
dictionary.py
File metadata and controls
74 lines (60 loc) · 2.19 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
import cython
from cython.cimports.av.error import err_check
@cython.cclass
class Dictionary:
def __cinit__(self, *args, **kwargs):
for arg in args:
self.update(arg)
if kwargs:
self.update(kwargs)
def __dealloc__(self):
if self.ptr != cython.NULL:
lib.av_dict_free(cython.address(self.ptr))
def __getitem__(self, key: cython.str):
element: cython.pointer[lib.AVDictionaryEntry] = lib.av_dict_get(
self.ptr, key, cython.NULL, 0
)
if element == cython.NULL:
raise KeyError(key)
return element.value
def __setitem__(self, key: cython.str, value: cython.str):
err_check(lib.av_dict_set(cython.address(self.ptr), key, value, 0))
def __delitem__(self, key: cython.str):
err_check(lib.av_dict_set(cython.address(self.ptr), key, cython.NULL, 0))
def __len__(self):
return err_check(lib.av_dict_count(self.ptr))
def __iter__(self):
element: cython.pointer[lib.AVDictionaryEntry] = cython.NULL
while True:
element = lib.av_dict_get(self.ptr, "", element, lib.AV_DICT_IGNORE_SUFFIX)
if element == cython.NULL:
break
yield element.key
def __repr__(self):
return f"av.Dictionary({dict(self)!r})"
def copy(self):
other: Dictionary = Dictionary()
lib.av_dict_copy(cython.address(other.ptr), self.ptr, 0)
return other
def pop(self, key: str):
value = self[key]
del self[key]
return value
def update(self, other=(), /, **kwds):
if isinstance(other, Dictionary):
lib.av_dict_copy(
cython.address(self.ptr), cython.cast(Dictionary, other).ptr, 0
)
elif hasattr(other, "keys"):
for key in other.keys():
self[key] = other[key]
else:
for key, value in other:
self[key] = value
for key, value in kwds.items():
self[key] = value
@cython.cfunc
def wrap_dictionary(input_: cython.pointer[lib.AVDictionary]) -> Dictionary:
output: Dictionary = Dictionary()
output.ptr = input_
return output