-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_tuple.py
More file actions
324 lines (264 loc) · 9.85 KB
/
lib_tuple.py
File metadata and controls
324 lines (264 loc) · 9.85 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# -*- coding: utf-8 -*-
from __future__ import division
TYPE_CHECKING = False
if TYPE_CHECKING:
from typing import Callable
from .lib_object import BaseManager
class Tuple:
"""
Tuple 提供了对元组的内置操作
"""
_manager = BaseManager()
def __init__(self, manager): # type: (BaseManager) -> None
"""初始化并返回一个新的 Tuple
Args:
manager (BaseManager):
用于管理引用对象的对象管理器
"""
self._manager = manager
def new(self, *elements): # type: (...) -> int
"""
new 创建并返回一个新的元组
Returns:
int: 新创建的元组的指针
"""
return self._manager.ref(elements)
def cast(self, ptr): # type: (int) -> int
"""
cast 将 ptr 指向的对象强制转换为元组
Args:
ptr (int): 目标对象的指针
Returns:
int: 强制转换后所得元组的指针
"""
return self._manager.ref(tuple(self._manager.deref(ptr)))
def length(self, ptr): # type: (int) -> int
"""length 返回元组的长度
Args:
ptr (int): 目标元组的指针
Raises:
Exception:
如果目标对象不是元组,则抛出相应的错误
Returns:
int: 目标元组的长度
"""
obj = self._manager.deref(ptr)
if not isinstance(obj, tuple):
raise Exception("tuple.length: Target object is not a tuple")
return len(obj)
def format(self, ptr): # type: (int) -> str
"""format 将元组格式化为其字符串表示
Args:
ptr (int): 目标元组的指针
Raises:
Exception:
如果目标对象不是元组,则抛出相应的错误
Returns:
str: 目标元组的字符串表示
"""
obj = self._manager.deref(ptr)
if not isinstance(obj, tuple):
raise Exception("tuple.format: Target object is not a tuple")
return obj.__repr__()
def get(self, ptr, index): # type: (int, int) -> int | float | bool | str
"""
get 从给定的元组中获取在指定索引的元素
Args:
ptr (int): 目标元组的指针
index (int): 给定的索引值
Raises:
Exception:
如果目标对象不是元组,
则抛出相应的错误
Returns:
int | float | bool | str:
位于目标索引的元素
"""
obj = self._manager.deref(ptr)
if not isinstance(obj, tuple):
raise Exception("tuple.get: Target object is not a tuple")
if index < 0 or index >= len(obj):
raise Exception(
"tuple.get: Index out of range [{}] with length {}".format(
index, len(obj)
)
)
return obj[index]
def ptr_get(self, ptr, index): # type: (int, int) -> int
"""
ptr_get 从给定的元组中获取在指定索引的元素。
它与 get 的不同之处在于它完全基于指针进行操作
Args:
ptr (int): 目标元组的指针
index (int): 给定的索引值
Raises:
Exception:
如果目标对象不是元组,
则抛出相应的错误
Returns:
int:
位于目标索引的元素(的指针)
"""
obj = self._manager.deref(ptr)
if not isinstance(obj, tuple):
raise Exception("tuple.ptr_get: Target object is not a tuple")
if index < 0 or index >= len(obj):
raise Exception(
"tuple.ptr_get: Index out of range [{}] with length {}".format(
index, len(obj)
)
)
return self._manager.ref(obj[index])
def sub(self, ptr, start, end): # type: (int, int, int) -> int
"""sub 返回元组的子元组
Args:
ptr (int): 目标元组的指针
start (int): 子元组的起始索引
end (int): 子元组的结束索引
Raises:
Exception:
如果目标对象不是元组,
或给出的起始或结束索引超出元组的范围,
或结束索引小于起始索引,
则抛出相应的错误
Returns:
int: 子元组的指针
"""
obj = self._manager.deref(ptr)
if not isinstance(obj, tuple):
raise Exception("tuple.sub: Target object is not a tuple")
if start < 0 or start > len(obj):
raise Exception(
"tuple.sub: Start index out of range [{}] with length {}".format(
start, len(obj)
)
)
if end < 0 or end > len(obj):
raise Exception(
"tuple.sub: End index out of range [{}] with length {}".format(
end, len(obj)
)
)
if end < start:
raise Exception(
"tuple.sub: The end index can't be less than the start index (start={}, end={})".format(
start, end
)
)
return self._manager.ref(obj[start:end])
def max(self, ptr): # type: (int) -> int | bool | float | str
"""max 返回元组中最大的元素
Args:
ptr (int): 目标元组的指针
Raises:
Exception:
如果目标对象不是元组,
则抛出相应的错误
Returns:
int | bool | float | str:
元组中最大的元素
"""
obj = self._manager.deref(ptr)
if not isinstance(obj, tuple):
raise Exception("tuple.max: Target object is not a tuple")
return max(obj)
def min(self, ptr): # type: (int) -> int | bool | float | str
"""min 返回元组中最小的元素
Args:
ptr (int): 目标元组的指针
Raises:
Exception:
如果目标对象不是元组,
则抛出相应的错误
Returns:
int | bool | float | str:
元组中最小的元素
"""
obj = self._manager.deref(ptr)
if not isinstance(obj, tuple):
raise Exception("tuple.min: Target object is not a tuple")
return min(obj)
def sum(self, ptr): # type: (int) -> int | float
"""sum 返回元组中所有元素的和
Args:
ptr (int): 目标元组的指针
Raises:
Exception:
如果目标对象不是元组,
或求和结果不是整数或浮点数,
则抛出相应的错误
Returns:
int | float:
元组中所有元素的和
"""
obj = self._manager.deref(ptr)
if not isinstance(obj, tuple):
raise Exception("tuple.sum: Target object is not a tuple")
result = sum(obj)
if isinstance(result, bool) or not isinstance(result, (int, float)):
raise Exception("tuple.sum: Only support tuple that contains int or float")
return result
def compare_in(self, ptr, raw): # type: (int, int | bool | float | str) -> bool
"""compare_in 检查给定的元组是否包含指定的元素
Args:
ptr (int):
目标元组的指针
raw (int | bool | float | str):
欲被检查的元素
Raises:
Exception:
如果目标对象不是元组,
则抛出相应的错误
Returns:
bool: 目标元组是否包含给定的元素
"""
obj = self._manager.deref(ptr)
if not isinstance(obj, tuple):
raise Exception("tuple.in: Target object is not a tuple")
return raw in obj
def ptr_compare_in(self, tuple_ptr, value_ptr): # type: (int, int) -> bool
"""
ptr_compare_in 检查给定的元组是否包含指定的元素。
它与 compare_in 的不同之处在于它完全基于指针进行操作
Args:
tuple_ptr (int):
目标元组的指针
value_ptr (int):
欲被检查的元素(的指针)
Raises:
Exception:
如果目标对象不是元组,
则抛出相应的错误
Returns:
bool: 目标元组是否包含给定的元素
"""
obj = self._manager.deref(tuple_ptr)
if not isinstance(obj, tuple):
raise Exception("tuple.ptr_in: Target object is not a tuple")
return self._manager.deref(value_ptr) in obj
def build_func(
self,
origin, # type: dict[str, Callable[..., int | bool | float | str]]
): # type: (...) -> None
"""
build_func 构建 tuple 模块的内置函数,
并将构建结果写入到传递的 origin 字典中
Args:
origin (dict[str, Callable[..., int | bool | float | str]]):
用于存放所有内置函数的字典
"""
funcs = {} # type: dict[str, Callable[..., int | bool | float | str]]
funcs["tuple.new"] = self.new
funcs["tuple.cast"] = self.cast
funcs["tuple.length"] = self.length
funcs["tuple.format"] = self.format
funcs["tuple.get"] = self.get
funcs["tuple.ptr_get"] = self.ptr_get
funcs["tuple.sub"] = self.sub
funcs["tuple.max"] = self.max
funcs["tuple.min"] = self.min
funcs["tuple.sum"] = self.sum
funcs["tuple.in"] = self.compare_in
funcs["tuple.ptr_in"] = self.ptr_compare_in
for key, value in funcs.items():
origin[key] = value