Nyx Node
Loading...
Searching...
No Matches
json_dict.py
1# -*- coding: utf-8 -*-
2########################################################################################################################
3# NyxNode
4# Author: Jérôme ODIER <jerome.odier@lpsc.in2p3.fr>
5# SPDX-License-Identifier: GPL-3.0+
6########################################################################################################################
7
8from __future__ import annotations
9
10########################################################################################################################
11
12import ctypes
13import typing
14
15########################################################################################################################
16
17from .. import bind
18from .. import obj
19
20########################################################################################################################
21
23 """!
24 @brief JSON dict object.
25 """
26
27 ####################################################################################################################
28
29 def __init__(self, ptr: int | None = None):
30 """!
31 @brief Allocates a new JSON dict object or wraps one.
32
33 @param ptr Optional JSON dict object pointer.
34 """
35
36 if ptr is None:
37
38 ptr = bind.lib.nyx_dict_new()
39
40 elif bind.lib.nyx_object_get_type(ptr) != bind.NyxObjectType.DICT:
41
42 raise TypeError('Not a pointer to a Nyx dict object')
43
44 super().__init__(ptr)
45
46 ####################################################################################################################
47
48 def clear(self) -> None:
49 """!
50 @brief Clears the content of this JSON dict object.
51 @return None
52 """
53
54 bind.lib.nyx_dict_clear(self.ptr)
55
56 ####################################################################################################################
57
58 def __delitem__(self, key: str) -> None:
59 """!
60 @brief Deletes the entry of the provided key.
61
62 @param key Key.
63 @return None
64 """
65
66 bind.lib.nyx_dict_del(self.ptr, bind.as_bytes(key, allow_none = False))
67
68 ####################################################################################################################
69
70 def __getitem__(self, key: str) -> obj.NyxObject:
71 """!
72 @brief Gets the JSON object of the provided key.
73
74 @param key Key.
75 @return The JSON object.
76 """
77
78 ################################################################################################################
79
80 ptr = bind.lib.nyx_dict_get(self.ptr, bind.as_bytes(key, allow_none = False))
81
82 if not ptr:
83
84 raise KeyError(key)
85
86 ################################################################################################################
87
88 return obj.NyxObject._wrap_borrowed_ptr(ptr)
89
90 ####################################################################################################################
91
92 def __setitem__(self, key: str, value: obj.NyxObject) -> bool:
93 """!
94 @brief Sets a JSON object in this JSON dict object.
95
96 @param key Key.
97 @param value JSON object to be added.
98 @return @c True if the value was modified, @c False otherwise.
99 """
100
101 if not isinstance(value, obj.NyxObject):
102
103 raise TypeError('value must be a Nyx object')
104
105 return bool(bind.lib.nyx_dict_set(self.ptr, bind.as_bytes(key, allow_none = False), value.ptr))
106
107 ####################################################################################################################
108
109 def __len__(self) -> int:
110 """!
111 @brief Gets the number of items in this JSON dict object.
112
113 @return The number of items.
114 """
115
116 return int(bind.lib.nyx_dict_size(self.ptr))
117
118 ####################################################################################################################
119
120 def _iterate(self) -> typing.Iterator[typing.Tuple[str, int]]:
121
122 ################################################################################################################
123
124 dict_ptr = ctypes.cast(self.ptr, bind.nyx_dict_p)
125
126 iterator = bind.nyx_dict_iter_t(
127 0x00000000000000000000,
128 dict_ptr.contents.head,
129 )
130
131 ################################################################################################################
132
133 key = bind.c_char_p()
134 val = bind.c_void_p()
135
136 ################################################################################################################
137
138 while bind.lib.nyx_dict_iterate(
139 ctypes.byref(iterator),
140 ctypes.byref(key),
141 ctypes.byref(val),
142 ):
143
144 if key.value is None\
145 or \
146 val.value is None:
147
148 raise RuntimeError('Invalid Nyx dict iterator result')
149
150 yield key.value.decode('utf-8'), val.value
151
152 ####################################################################################################################
153
154 def __iter__(self) -> typing.Iterator[str]:
155
156 for key, _ in self._iterate():
157
158 yield key
159
160 ####################################################################################################################
161
162 def keys(self) -> typing.Iterator[str]:
163 """!
164 @brief Iterates over the keys of this JSON dict object.
165
166 @return An iterator over the keys.
167 """
168
169 for key, _ in self._iterate():
170
171 yield key
172
173 ####################################################################################################################
174
175 def values(self) -> typing.Iterator[obj.NyxObject]:
176 """!
177 @brief Iterates over the values of this JSON dict object.
178
179 @return An iterator over the values.
180 """
181
182 for _, ptr in self._iterate():
183
184 yield obj.NyxObject._wrap_borrowed_ptr(ptr)
185
186 ####################################################################################################################
187
188 def items(self) -> typing.Iterator[typing.Tuple[str, obj.NyxObject]]:
189 """!
190 @brief Iterates over the key/value pairs of this JSON dict object.
191
192 @return An iterator over the key/value pairs.
193 """
194
195 for key, ptr in self._iterate():
196
197 yield key, obj.NyxObject._wrap_borrowed_ptr(ptr)
198
199########################################################################################################################
200
201__all__ = ['NyxDict']
202
203########################################################################################################################
JSON dict object.
Definition json_dict.py:22
None __delitem__(self, str key)
Deletes the entry of the provided key.
Definition json_dict.py:58
int __len__(self)
Gets the number of items in this JSON dict object.
Definition json_dict.py:109
typing.Iterator[typing.Tuple[str, obj.NyxObject]] items(self)
Iterates over the key/value pairs of this JSON dict object.
Definition json_dict.py:188
obj.NyxObject __getitem__(self, str key)
Gets the JSON object of the provided key.
Definition json_dict.py:70
__init__(self, int|None ptr=None)
Allocates a new JSON dict object or wraps one.
Definition json_dict.py:29
typing.Iterator[typing.Tuple[str, int]] _iterate(self)
Definition json_dict.py:120
None clear(self)
Clears the content of this JSON dict object.
Definition json_dict.py:48
bool __setitem__(self, str key, obj.NyxObject value)
Sets a JSON object in this JSON dict object.
Definition json_dict.py:92
typing.Iterator[str] keys(self)
Iterates over the keys of this JSON dict object.
Definition json_dict.py:162
typing.Iterator[obj.NyxObject] values(self)
Iterates over the values of this JSON dict object.
Definition json_dict.py:175
Base class for JSON Nyx objects.
Definition obj.py:44