Nyx Node
Loading...
Searching...
No Matches
json_list.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 list object.
25 """
26
27 ####################################################################################################################
28
29 def __init__(self, ptr: int | None = None):
30 """!
31 @brief Allocates a new JSON list object or wraps one.
32
33 @param ptr Optional JSON list object pointer.
34 """
35
36 if ptr is None:
37
38 ptr = bind.lib.nyx_list_new()
39
40 elif bind.lib.nyx_object_get_type(ptr) != bind.NyxObjectType.LIST:
41
42 raise TypeError('Not a pointer to a Nyx list object')
43
44 super().__init__(ptr)
45
46 ####################################################################################################################
47
48 def clear(self) -> None:
49 """!
50 @brief Clears the content of this JSON list object.
51
52 @return None
53 """
54
55 bind.lib.nyx_list_clear(self.ptr)
56
57 ####################################################################################################################
58
59 def __delitem__(self, idx: int) -> None:
60 """!
61 @brief Deletes the entry at the provided index.
62
63 @param idx Index.
64 @return None
65 """
66
67 bind.lib.nyx_list_del(self.ptr, idx)
68
69 ####################################################################################################################
70
71 def __getitem__(self, idx: int) -> obj.NyxObject:
72 """!
73 @brief Gets the JSON object at the provided index.
74
75 @param idx Index.
76 @return The JSON object at the provided index.
77 """
78
79 ################################################################################################################
80
81 ptr = bind.lib.nyx_list_get(self.ptr, idx)
82
83 if not ptr:
84
85 raise IndexError(idx)
86
87 ################################################################################################################
88
89 return obj.NyxObject._wrap_borrowed_ptr(ptr)
90
91 ####################################################################################################################
92
93 def __setitem__(self, idx: int, value: obj.NyxObject) -> bool:
94 """!
95 @brief Sets a JSON object at the provided index.
96
97 @param idx Index.
98 @param value JSON object to be added.
99 @return @c True if the value was modified, @c False otherwise.
100 """
101
102 if not isinstance(value, obj.NyxObject):
103
104 raise TypeError('value must be a Nyx object')
105
106 return bool(bind.lib.nyx_list_set(self.ptr, idx, value.ptr))
107
108 ####################################################################################################################
109
110 def append(self, value: obj.NyxObject) -> bool:
111 """!
112 @brief Appends a JSON object in this JSON list object.
113
114 @param value JSON object to be added.
115 @return @c True if the value was modified, @c False otherwise.
116 """
117
118 return self.__setitem__(-1, value)
119
120 ####################################################################################################################
121
122 def __len__(self) -> int:
123 """!
124 @brief Gets the number of items in this JSON list object.
125
126 @return The number of items.
127 """
128
129 return int(bind.lib.nyx_list_size(self.ptr))
130
131 ####################################################################################################################
132
133 def _iterate(self) -> typing.Iterator[typing.Tuple[int, int]]:
134
135 ################################################################################################################
136
137 list_ptr = ctypes.cast(self.ptr, bind.nyx_list_p)
138
139 iterator = bind.nyx_list_iter_t(
140 0x00000000000000000000,
141 list_ptr.contents.head,
142 )
143
144 ################################################################################################################
145
146 idx = bind.c_size_t()
147 val = bind.c_void_p()
148
149 ################################################################################################################
150
151 while bind.lib.nyx_list_iterate(
152 ctypes.byref(iterator),
153 ctypes.byref(idx),
154 ctypes.byref(val),
155 ):
156
157 if idx.value is None\
158 or \
159 val.value is None:
160
161 raise RuntimeError('Invalid Nyx list iterator result')
162
163 yield idx.value, val.value
164
165 ####################################################################################################################
166
167 def __iter__(self) -> typing.Iterator[obj.NyxObject]:
168 """!
169 @brief Iterates over the values of this JSON list object.
170
171 @return An iterator over the values.
172 """
173
174 for _, ptr in self._iterate():
175
176 yield obj.NyxObject._wrap_borrowed_ptr(ptr)
177
178 ####################################################################################################################
179
180 def indices(self) -> typing.Iterator[int]:
181 """!
182 @brief Iterates over the indices of this JSON list object.
183
184 @return An iterator over the indices.
185 """
186
187 for idx, _ in self._iterate():
188
189 yield idx
190
191 ####################################################################################################################
192
193 def values(self) -> typing.Iterator[obj.NyxObject]:
194 """!
195 @brief Iterates over the values of this JSON list object.
196
197 @return An iterator over the values.
198 """
199
200 for _, ptr in self._iterate():
201
202 yield obj.NyxObject._wrap_borrowed_ptr(ptr)
203
204 ####################################################################################################################
205
206 def items(self) -> typing.Iterator[typing.Tuple[int, obj.NyxObject]]:
207 """!
208 @brief Iterates over the index/value pairs of this JSON list object.
209
210 @return An iterator over the index/value pairs.
211 """
212
213 for idx, ptr in self._iterate():
214
215 yield idx, obj.NyxObject._wrap_borrowed_ptr(ptr)
216
217########################################################################################################################
218
219__all__ = ['NyxList']
220
221########################################################################################################################
JSON list object.
Definition json_list.py:22
typing.Iterator[int] indices(self)
Iterates over the indices of this JSON list object.
Definition json_list.py:180
int __len__(self)
Gets the number of items in this JSON list object.
Definition json_list.py:122
bool append(self, obj.NyxObject value)
Appends a JSON object in this JSON list object.
Definition json_list.py:110
None __delitem__(self, int idx)
Deletes the entry at the provided index.
Definition json_list.py:59
typing.Iterator[obj.NyxObject] values(self)
Iterates over the values of this JSON list object.
Definition json_list.py:193
bool __setitem__(self, int idx, obj.NyxObject value)
Sets a JSON object at the provided index.
Definition json_list.py:93
typing.Iterator[obj.NyxObject] __iter__(self)
Iterates over the values of this JSON list object.
Definition json_list.py:167
typing.Iterator[typing.Tuple[int, obj.NyxObject]] items(self)
Iterates over the index/value pairs of this JSON list object.
Definition json_list.py:206
__init__(self, int|None ptr=None)
Allocates a new JSON list object or wraps one.
Definition json_list.py:29
None clear(self)
Clears the content of this JSON list object.
Definition json_list.py:48
obj.NyxObject __getitem__(self, int idx)
Gets the JSON object at the provided index.
Definition json_list.py:71
typing.Iterator[typing.Tuple[int, int]] _iterate(self)
Definition json_list.py:133
Base class for JSON Nyx objects.
Definition obj.py:44