rapydscript-ns 0.9.2 → 0.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -0
- package/PYTHON_GAPS.md +352 -0
- package/README.md +176 -32
- package/TODO.md +1 -128
- package/bin/rapydscript +70 -70
- package/language-service/index.js +242 -11
- package/memory/project_string_impl.md +43 -0
- package/package.json +1 -1
- package/release/baselib-plain-pretty.js +248 -38
- package/release/baselib-plain-ugly.js +8 -8
- package/release/compiler.js +778 -277
- package/release/signatures.json +30 -30
- package/src/ast.pyj +10 -1
- package/src/baselib-builtins.pyj +56 -2
- package/src/baselib-containers.pyj +25 -1
- package/src/baselib-errors.pyj +7 -3
- package/src/baselib-internal.pyj +51 -6
- package/src/baselib-str.pyj +18 -5
- package/src/lib/asyncio.pyj +534 -0
- package/src/lib/base64.pyj +399 -0
- package/src/lib/bisect.pyj +73 -0
- package/src/lib/collections.pyj +228 -4
- package/src/lib/csv.pyj +494 -0
- package/src/lib/heapq.pyj +98 -0
- package/src/lib/html.pyj +382 -0
- package/src/lib/http/__init__.pyj +98 -0
- package/src/lib/http/client.pyj +304 -0
- package/src/lib/http/cookies.pyj +236 -0
- package/src/lib/logging.pyj +672 -0
- package/src/lib/pprint.pyj +455 -0
- package/src/lib/pythonize.pyj +20 -20
- package/src/lib/statistics.pyj +0 -0
- package/src/lib/string.pyj +357 -0
- package/src/lib/textwrap.pyj +329 -0
- package/src/lib/urllib/__init__.pyj +14 -0
- package/src/lib/urllib/error.pyj +66 -0
- package/src/lib/urllib/parse.pyj +475 -0
- package/src/lib/urllib/request.pyj +86 -0
- package/src/monaco-language-service/analyzer.js +5 -2
- package/src/monaco-language-service/completions.js +26 -0
- package/src/monaco-language-service/diagnostics.js +203 -4
- package/src/monaco-language-service/scope.js +1 -0
- package/src/output/codegen.pyj +4 -1
- package/src/output/functions.pyj +152 -6
- package/src/output/loops.pyj +17 -2
- package/src/output/modules.pyj +1 -1
- package/src/output/operators.pyj +15 -0
- package/src/output/stream.pyj +0 -1
- package/src/parse.pyj +108 -24
- package/src/tokenizer.pyj +19 -3
- package/test/async_generators.pyj +144 -0
- package/test/asyncio.pyj +307 -0
- package/test/base64.pyj +202 -0
- package/test/baselib.pyj +23 -0
- package/test/bisect.pyj +178 -0
- package/test/chainmap.pyj +185 -0
- package/test/csv.pyj +405 -0
- package/test/float_special.pyj +64 -0
- package/test/heapq.pyj +174 -0
- package/test/html.pyj +212 -0
- package/test/http.pyj +259 -0
- package/test/imports.pyj +79 -72
- package/test/logging.pyj +356 -0
- package/test/long.pyj +130 -0
- package/test/parenthesized_with.pyj +141 -0
- package/test/pprint.pyj +232 -0
- package/test/python_compat.pyj +3 -5
- package/test/python_modulo.pyj +76 -0
- package/test/python_modulo_off.pyj +21 -0
- package/test/statistics.pyj +224 -0
- package/test/str.pyj +14 -0
- package/test/string.pyj +245 -0
- package/test/textwrap.pyj +172 -0
- package/test/type_display.pyj +48 -0
- package/test/type_enforcement.pyj +164 -0
- package/test/unit/index.js +94 -6
- package/test/unit/language-service-completions.js +121 -0
- package/test/unit/language-service-scope.js +32 -0
- package/test/unit/language-service.js +190 -5
- package/test/unit/run-language-service.js +17 -3
- package/test/unit/web-repl.js +2401 -13
- package/test/urllib.pyj +193 -0
- package/tools/compile.js +1 -1
- package/tools/embedded_compiler.js +7 -7
- package/tools/export.js +4 -2
- package/web-repl/main.js +1 -1
- package/web-repl/rapydscript.js +7 -5
- package/test/omit_function_metadata.pyj +0 -20
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
###########################################################
|
|
2
|
+
# RapydScript Standard Library
|
|
3
|
+
# License: Apache License 2.0
|
|
4
|
+
# This library is covered under Apache license, so that
|
|
5
|
+
# you can distribute it with your RapydScript applications.
|
|
6
|
+
###########################################################
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# Implementation of Python's 'pprint' module.
|
|
10
|
+
#
|
|
11
|
+
# Pretty-prints arbitrary RapydScript / Python data structures.
|
|
12
|
+
#
|
|
13
|
+
# A value's repr() is used when it fits within `width`; otherwise containers
|
|
14
|
+
# are broken across multiple lines with Python-style indentation.
|
|
15
|
+
#
|
|
16
|
+
# Public API:
|
|
17
|
+
# pformat(object, ...) -> str
|
|
18
|
+
# pprint(object, ...)
|
|
19
|
+
# pp(object, ...) same as pprint but defaults sort_dicts=False
|
|
20
|
+
# saferepr(object) repr() with recursive references marked
|
|
21
|
+
# isreadable(object) whether pformat output can be parsed back
|
|
22
|
+
# isrecursive(object) whether the object has recursive references
|
|
23
|
+
# class PrettyPrinter
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# ── private helpers ─────────────────────────────────────────────────────────
|
|
27
|
+
|
|
28
|
+
v"""
|
|
29
|
+
function _pp_is_plain_dict_like(obj) {
|
|
30
|
+
if (obj === null || obj === undefined) return false;
|
|
31
|
+
if (typeof obj !== 'object') return false;
|
|
32
|
+
if (Array.isArray(obj)) return false;
|
|
33
|
+
if (obj.jsmap && typeof obj.jsmap.forEach === 'function') return false;
|
|
34
|
+
if (obj.jsset && typeof obj.jsset.values === 'function') return false;
|
|
35
|
+
var proto = Object.getPrototypeOf(obj);
|
|
36
|
+
return proto === null || proto === Object.prototype;
|
|
37
|
+
}
|
|
38
|
+
var _pp_id_counter = 0;
|
|
39
|
+
function _pp_id(obj) {
|
|
40
|
+
if (obj === null || obj === undefined) return 0;
|
|
41
|
+
if (typeof obj !== 'object') return 0;
|
|
42
|
+
if (!obj.__pp_id__) {
|
|
43
|
+
Object.defineProperty(obj, '__pp_id__',
|
|
44
|
+
{ value: ++_pp_id_counter, configurable: true, writable: true });
|
|
45
|
+
}
|
|
46
|
+
return obj.__pp_id__;
|
|
47
|
+
}
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _try_repr(x):
|
|
52
|
+
try:
|
|
53
|
+
return repr(x)
|
|
54
|
+
except:
|
|
55
|
+
return str(x)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _cmp_pair(a, b):
|
|
59
|
+
try:
|
|
60
|
+
if a[0] < b[0]:
|
|
61
|
+
return -1
|
|
62
|
+
if a[0] > b[0]:
|
|
63
|
+
return 1
|
|
64
|
+
return 0
|
|
65
|
+
except:
|
|
66
|
+
return 0
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _cmp_value(a, b):
|
|
70
|
+
try:
|
|
71
|
+
if a < b:
|
|
72
|
+
return -1
|
|
73
|
+
if a > b:
|
|
74
|
+
return 1
|
|
75
|
+
return 0
|
|
76
|
+
except:
|
|
77
|
+
return 0
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _is_dict(obj):
|
|
81
|
+
if isinstance(obj, dict):
|
|
82
|
+
return True
|
|
83
|
+
return v'_pp_is_plain_dict_like(obj)'
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _get_dict_pairs(obj, sort_dicts):
|
|
87
|
+
pairs = v'[]'
|
|
88
|
+
if isinstance(obj, dict):
|
|
89
|
+
iterator = obj.items()
|
|
90
|
+
r = iterator.next()
|
|
91
|
+
while not r.done:
|
|
92
|
+
pairs.push(v'[r.value[0], r.value[1]]')
|
|
93
|
+
r = iterator.next()
|
|
94
|
+
else:
|
|
95
|
+
v"""
|
|
96
|
+
var _keys = Object.keys(obj);
|
|
97
|
+
for (var _i = 0; _i < _keys.length; _i++) {
|
|
98
|
+
pairs.push([_keys[_i], obj[_keys[_i]]]);
|
|
99
|
+
}
|
|
100
|
+
"""
|
|
101
|
+
if sort_dicts:
|
|
102
|
+
pairs.sort(_cmp_pair)
|
|
103
|
+
return pairs
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _get_set_items(obj):
|
|
107
|
+
items = v'[]'
|
|
108
|
+
iterator = obj.jsset.values()
|
|
109
|
+
r = iterator.next()
|
|
110
|
+
while not r.done:
|
|
111
|
+
items.push(r.value)
|
|
112
|
+
r = iterator.next()
|
|
113
|
+
items.sort(_cmp_value)
|
|
114
|
+
return items
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _recursion_marker(obj):
|
|
118
|
+
name = 'object'
|
|
119
|
+
try:
|
|
120
|
+
if obj.constructor and obj.constructor.name:
|
|
121
|
+
name = obj.constructor.name
|
|
122
|
+
elif isinstance(obj, dict):
|
|
123
|
+
name = 'dict'
|
|
124
|
+
elif isinstance(obj, set):
|
|
125
|
+
name = 'set'
|
|
126
|
+
elif Array.isArray(obj):
|
|
127
|
+
name = 'list'
|
|
128
|
+
except:
|
|
129
|
+
pass
|
|
130
|
+
return '<Recursion on ' + name + ' with id=' + str(v'_pp_id(obj)') + '>'
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _safe_repr(obj, context, maxlevels, level, sort_dicts):
|
|
134
|
+
"""Return [repr_string, readable, recursive].
|
|
135
|
+
|
|
136
|
+
readable -- True if the output is parseable back by eval
|
|
137
|
+
recursive -- True if obj (or any sub-object) recursively contains itself
|
|
138
|
+
"""
|
|
139
|
+
if obj is None:
|
|
140
|
+
return v'["None", true, false]'
|
|
141
|
+
if obj is True:
|
|
142
|
+
return v'["True", true, false]'
|
|
143
|
+
if obj is False:
|
|
144
|
+
return v'["False", true, false]'
|
|
145
|
+
t = jstype(obj)
|
|
146
|
+
if t is 'number' or t is 'string' or t is 'bigint':
|
|
147
|
+
return v'[_try_repr(obj), true, false]'
|
|
148
|
+
|
|
149
|
+
obj_id = v'_pp_id(obj)'
|
|
150
|
+
|
|
151
|
+
if Array.isArray(obj):
|
|
152
|
+
if obj.length is 0:
|
|
153
|
+
return v'["[]", true, false]'
|
|
154
|
+
if maxlevels is not None and level >= maxlevels:
|
|
155
|
+
return v'["[...]", false, false]'
|
|
156
|
+
if context.has(obj):
|
|
157
|
+
return v'[_recursion_marker(obj), false, true]'
|
|
158
|
+
context.set(obj, 1)
|
|
159
|
+
components = v'[]'
|
|
160
|
+
readable = True
|
|
161
|
+
recursive = False
|
|
162
|
+
for v'var i = 0; i < obj.length; i++':
|
|
163
|
+
r = _safe_repr(obj[i], context, maxlevels, level + 1, sort_dicts)
|
|
164
|
+
components.push(r[0])
|
|
165
|
+
if not r[1]:
|
|
166
|
+
readable = False
|
|
167
|
+
if r[2]:
|
|
168
|
+
recursive = True
|
|
169
|
+
context.delete(obj)
|
|
170
|
+
return v'["[" + components.join(", ") + "]", readable, recursive]'
|
|
171
|
+
|
|
172
|
+
if isinstance(obj, set):
|
|
173
|
+
if obj.size is 0:
|
|
174
|
+
return v'["set()", false, false]'
|
|
175
|
+
if maxlevels is not None and level >= maxlevels:
|
|
176
|
+
return v'["{...}", false, false]'
|
|
177
|
+
if context.has(obj):
|
|
178
|
+
return v'[_recursion_marker(obj), false, true]'
|
|
179
|
+
context.set(obj, 1)
|
|
180
|
+
items = _get_set_items(obj)
|
|
181
|
+
components = v'[]'
|
|
182
|
+
readable = True
|
|
183
|
+
recursive = False
|
|
184
|
+
for v'var j = 0; j < items.length; j++':
|
|
185
|
+
r = _safe_repr(items[j], context, maxlevels, level + 1, sort_dicts)
|
|
186
|
+
components.push(r[0])
|
|
187
|
+
if not r[1]:
|
|
188
|
+
readable = False
|
|
189
|
+
if r[2]:
|
|
190
|
+
recursive = True
|
|
191
|
+
context.delete(obj)
|
|
192
|
+
return v'["{" + components.join(", ") + "}", readable, recursive]'
|
|
193
|
+
|
|
194
|
+
if isinstance(obj, frozenset):
|
|
195
|
+
if obj.size is 0:
|
|
196
|
+
return v'["frozenset()", true, false]'
|
|
197
|
+
if maxlevels is not None and level >= maxlevels:
|
|
198
|
+
return v'["frozenset({...})", false, false]'
|
|
199
|
+
items = _get_set_items(obj)
|
|
200
|
+
components = v'[]'
|
|
201
|
+
readable = True
|
|
202
|
+
recursive = False
|
|
203
|
+
for v'var k = 0; k < items.length; k++':
|
|
204
|
+
r = _safe_repr(items[k], context, maxlevels, level + 1, sort_dicts)
|
|
205
|
+
components.push(r[0])
|
|
206
|
+
if not r[1]:
|
|
207
|
+
readable = False
|
|
208
|
+
if r[2]:
|
|
209
|
+
recursive = True
|
|
210
|
+
return v'["frozenset({" + components.join(", ") + "})", readable, recursive]'
|
|
211
|
+
|
|
212
|
+
if _is_dict(obj):
|
|
213
|
+
pairs = _get_dict_pairs(obj, sort_dicts)
|
|
214
|
+
if pairs.length is 0:
|
|
215
|
+
return v'["{}", true, false]'
|
|
216
|
+
if maxlevels is not None and level >= maxlevels:
|
|
217
|
+
return v'["{...}", false, false]'
|
|
218
|
+
if context.has(obj):
|
|
219
|
+
return v'[_recursion_marker(obj), false, true]'
|
|
220
|
+
context.set(obj, 1)
|
|
221
|
+
components = v'[]'
|
|
222
|
+
readable = True
|
|
223
|
+
recursive = False
|
|
224
|
+
for v'var m = 0; m < pairs.length; m++':
|
|
225
|
+
kr = _safe_repr(pairs[m][0], context, maxlevels, level + 1, sort_dicts)
|
|
226
|
+
vr = _safe_repr(pairs[m][1], context, maxlevels, level + 1, sort_dicts)
|
|
227
|
+
components.push(kr[0] + ': ' + vr[0])
|
|
228
|
+
if not kr[1] or not vr[1]:
|
|
229
|
+
readable = False
|
|
230
|
+
if kr[2] or vr[2]:
|
|
231
|
+
recursive = True
|
|
232
|
+
context.delete(obj)
|
|
233
|
+
return v'["{" + components.join(", ") + "}", readable, recursive]'
|
|
234
|
+
|
|
235
|
+
# Class instance, function, etc. — fall back to repr().
|
|
236
|
+
rep = _try_repr(obj)
|
|
237
|
+
return v'[rep, false, false]'
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
# ── stream buffer used when no stream is given ──────────────────────────────
|
|
241
|
+
|
|
242
|
+
class _StreamBuffer:
|
|
243
|
+
def __init__(self):
|
|
244
|
+
self.parts = v'[]'
|
|
245
|
+
|
|
246
|
+
def write(self, s):
|
|
247
|
+
self.parts.push(s)
|
|
248
|
+
|
|
249
|
+
def getvalue(self):
|
|
250
|
+
return self.parts.join('')
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
# ── PrettyPrinter ───────────────────────────────────────────────────────────
|
|
254
|
+
|
|
255
|
+
class PrettyPrinter:
|
|
256
|
+
"""Handle pretty-printing operations onto a stream."""
|
|
257
|
+
|
|
258
|
+
def __init__(self, indent=1, width=80, depth=None, stream=None,
|
|
259
|
+
compact=False, sort_dicts=True, underscore_numbers=False):
|
|
260
|
+
if jstype(indent) is not 'number' or indent < 0:
|
|
261
|
+
raise ValueError('indent must be a non-negative integer')
|
|
262
|
+
if jstype(width) is not 'number' or width < 0:
|
|
263
|
+
raise ValueError('width must be a non-negative integer')
|
|
264
|
+
if depth is not None:
|
|
265
|
+
if jstype(depth) is not 'number' or depth <= 0:
|
|
266
|
+
raise ValueError('depth must be > 0')
|
|
267
|
+
self._depth = depth
|
|
268
|
+
self._indent_per_level = indent
|
|
269
|
+
self._width = width
|
|
270
|
+
self._compact = bool(compact)
|
|
271
|
+
self._sort_dicts = bool(sort_dicts)
|
|
272
|
+
self._underscore_numbers = bool(underscore_numbers)
|
|
273
|
+
self._stream = stream
|
|
274
|
+
|
|
275
|
+
def pprint(self, object):
|
|
276
|
+
if self._stream is not None:
|
|
277
|
+
self._format(object, self._stream, 0, 0, v'new Map()', 0)
|
|
278
|
+
self._stream.write('\n')
|
|
279
|
+
else:
|
|
280
|
+
print(self.pformat(object))
|
|
281
|
+
|
|
282
|
+
def pformat(self, object):
|
|
283
|
+
buf = _StreamBuffer()
|
|
284
|
+
self._format(object, buf, 0, 0, v'new Map()', 0)
|
|
285
|
+
return buf.getvalue()
|
|
286
|
+
|
|
287
|
+
def isreadable(self, object):
|
|
288
|
+
r = _safe_repr(object, v'new Map()', self._depth, 0, self._sort_dicts)
|
|
289
|
+
return r[1] and not r[2]
|
|
290
|
+
|
|
291
|
+
def isrecursive(self, object):
|
|
292
|
+
r = _safe_repr(object, v'new Map()', self._depth, 0, self._sort_dicts)
|
|
293
|
+
return r[2]
|
|
294
|
+
|
|
295
|
+
def _format(self, obj, stream, indent, allowance, context, level):
|
|
296
|
+
if obj is not None and jstype(obj) is 'object' and context.has(obj):
|
|
297
|
+
stream.write(_recursion_marker(obj))
|
|
298
|
+
return
|
|
299
|
+
|
|
300
|
+
rep = self._repr(obj, context, level)
|
|
301
|
+
max_width = self._width - indent - allowance
|
|
302
|
+
|
|
303
|
+
if rep.length <= max_width:
|
|
304
|
+
stream.write(rep)
|
|
305
|
+
return
|
|
306
|
+
|
|
307
|
+
# too wide -- try to expand the container across multiple lines
|
|
308
|
+
if Array.isArray(obj) and obj.length > 0:
|
|
309
|
+
if self._depth is not None and level >= self._depth:
|
|
310
|
+
stream.write(rep)
|
|
311
|
+
return
|
|
312
|
+
context.set(obj, 1)
|
|
313
|
+
stream.write('[')
|
|
314
|
+
self._format_items(obj, stream, indent, allowance + 1, context, level + 1)
|
|
315
|
+
stream.write(']')
|
|
316
|
+
context.delete(obj)
|
|
317
|
+
return
|
|
318
|
+
|
|
319
|
+
if isinstance(obj, set) and obj.size > 0:
|
|
320
|
+
if self._depth is not None and level >= self._depth:
|
|
321
|
+
stream.write(rep)
|
|
322
|
+
return
|
|
323
|
+
context.set(obj, 1)
|
|
324
|
+
items = _get_set_items(obj)
|
|
325
|
+
stream.write('{')
|
|
326
|
+
self._format_items(items, stream, indent, allowance + 1, context, level + 1)
|
|
327
|
+
stream.write('}')
|
|
328
|
+
context.delete(obj)
|
|
329
|
+
return
|
|
330
|
+
|
|
331
|
+
if isinstance(obj, frozenset) and obj.size > 0:
|
|
332
|
+
if self._depth is not None and level >= self._depth:
|
|
333
|
+
stream.write(rep)
|
|
334
|
+
return
|
|
335
|
+
items = _get_set_items(obj)
|
|
336
|
+
stream.write('frozenset({')
|
|
337
|
+
self._format_items(items, stream, indent + 10, allowance + 2, context, level + 1)
|
|
338
|
+
stream.write('})')
|
|
339
|
+
return
|
|
340
|
+
|
|
341
|
+
if _is_dict(obj):
|
|
342
|
+
pairs = _get_dict_pairs(obj, self._sort_dicts)
|
|
343
|
+
if pairs.length > 0:
|
|
344
|
+
if self._depth is not None and level >= self._depth:
|
|
345
|
+
stream.write(rep)
|
|
346
|
+
return
|
|
347
|
+
context.set(obj, 1)
|
|
348
|
+
stream.write('{')
|
|
349
|
+
if self._indent_per_level > 1:
|
|
350
|
+
stream.write(' '.repeat(self._indent_per_level - 1))
|
|
351
|
+
self._format_dict_items(pairs, stream, indent, allowance + 1, context, level + 1)
|
|
352
|
+
stream.write('}')
|
|
353
|
+
context.delete(obj)
|
|
354
|
+
return
|
|
355
|
+
|
|
356
|
+
stream.write(rep)
|
|
357
|
+
|
|
358
|
+
def _format_items(self, items, stream, indent, allowance, context, level):
|
|
359
|
+
indent = indent + self._indent_per_level
|
|
360
|
+
if self._indent_per_level > 1:
|
|
361
|
+
stream.write(' '.repeat(self._indent_per_level - 1))
|
|
362
|
+
delimnl = ',\n' + ' '.repeat(indent)
|
|
363
|
+
delim = ''
|
|
364
|
+
width = self._width - indent + 1
|
|
365
|
+
max_width = width
|
|
366
|
+
last_index = items.length - 1
|
|
367
|
+
for v'var i = 0; i < items.length; i++':
|
|
368
|
+
last = i is last_index
|
|
369
|
+
ent = items[i]
|
|
370
|
+
if self._compact:
|
|
371
|
+
rep = self._repr(ent, context, level)
|
|
372
|
+
w = rep.length + 2
|
|
373
|
+
if width < w:
|
|
374
|
+
width = max_width
|
|
375
|
+
if delim:
|
|
376
|
+
delim = delimnl
|
|
377
|
+
if width >= w:
|
|
378
|
+
width -= w
|
|
379
|
+
stream.write(delim)
|
|
380
|
+
delim = ', '
|
|
381
|
+
stream.write(rep)
|
|
382
|
+
continue
|
|
383
|
+
stream.write(delim)
|
|
384
|
+
delim = delimnl
|
|
385
|
+
self._format(ent, stream, indent,
|
|
386
|
+
allowance if last else 1,
|
|
387
|
+
context, level)
|
|
388
|
+
|
|
389
|
+
def _format_dict_items(self, items, stream, indent, allowance, context, level):
|
|
390
|
+
indent = indent + self._indent_per_level
|
|
391
|
+
delimnl = ',\n' + ' '.repeat(indent)
|
|
392
|
+
last_index = items.length - 1
|
|
393
|
+
for v'var i = 0; i < items.length; i++':
|
|
394
|
+
last = i is last_index
|
|
395
|
+
key = items[i][0]
|
|
396
|
+
ent = items[i][1]
|
|
397
|
+
rep = self._repr(key, context, level)
|
|
398
|
+
stream.write(rep)
|
|
399
|
+
stream.write(': ')
|
|
400
|
+
self._format(ent, stream, indent + rep.length + 2,
|
|
401
|
+
allowance if last else 1, context, level)
|
|
402
|
+
if not last:
|
|
403
|
+
stream.write(delimnl)
|
|
404
|
+
|
|
405
|
+
def _repr(self, obj, context, level):
|
|
406
|
+
r = _safe_repr(obj, context, self._depth, level, self._sort_dicts)
|
|
407
|
+
return r[0]
|
|
408
|
+
|
|
409
|
+
def format(self, object, context, maxlevels, level):
|
|
410
|
+
"""Hook for subclasses; returns (string, readable, recursive)."""
|
|
411
|
+
return _safe_repr(object, context, maxlevels, level, self._sort_dicts)
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
# ── module-level convenience functions ──────────────────────────────────────
|
|
415
|
+
|
|
416
|
+
def pformat(object, indent=1, width=80, depth=None, compact=False,
|
|
417
|
+
sort_dicts=True, underscore_numbers=False):
|
|
418
|
+
"""Pretty-print a value as a string."""
|
|
419
|
+
return PrettyPrinter(indent=indent, width=width, depth=depth,
|
|
420
|
+
compact=compact, sort_dicts=sort_dicts,
|
|
421
|
+
underscore_numbers=underscore_numbers).pformat(object)
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
def pprint(object, stream=None, indent=1, width=80, depth=None,
|
|
425
|
+
compact=False, sort_dicts=True, underscore_numbers=False):
|
|
426
|
+
"""Pretty-print a value to a stream, or to stdout if stream is None."""
|
|
427
|
+
PrettyPrinter(indent=indent, width=width, depth=depth, stream=stream,
|
|
428
|
+
compact=compact, sort_dicts=sort_dicts,
|
|
429
|
+
underscore_numbers=underscore_numbers).pprint(object)
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
def pp(object, stream=None, indent=1, width=80, depth=None,
|
|
433
|
+
compact=False, sort_dicts=False, underscore_numbers=False):
|
|
434
|
+
"""Pretty-print with sort_dicts=False by default."""
|
|
435
|
+
pprint(object, stream=stream, indent=indent, width=width, depth=depth,
|
|
436
|
+
compact=compact, sort_dicts=sort_dicts,
|
|
437
|
+
underscore_numbers=underscore_numbers)
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
def saferepr(object):
|
|
441
|
+
"""Like repr() but recursive references are marked as <Recursion ...>."""
|
|
442
|
+
r = _safe_repr(object, v'new Map()', None, 0, True)
|
|
443
|
+
return r[0]
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
def isreadable(object):
|
|
447
|
+
"""True if pformat(object) is readable by eval()."""
|
|
448
|
+
r = _safe_repr(object, v'new Map()', None, 0, True)
|
|
449
|
+
return r[1] and not r[2]
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
def isrecursive(object):
|
|
453
|
+
"""True if object contains any recursive references."""
|
|
454
|
+
r = _safe_repr(object, v'new Map()', None, 0, True)
|
|
455
|
+
return r[2]
|
package/src/lib/pythonize.pyj
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
# vim:fileencoding=utf-8
|
|
2
|
-
# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
|
|
3
|
-
# globals: ρσ_str
|
|
4
|
-
|
|
5
|
-
def strings():
|
|
6
|
-
string_funcs = set((
|
|
7
|
-
'capitalize strip lstrip rstrip islower isupper isspace lower upper swapcase title'
|
|
8
|
-
' center count endswith startswith find rfind index rindex format join ljust rjust'
|
|
9
|
-
' partition rpartition replace split rsplit splitlines zfill expandtabs').split(' '))
|
|
10
|
-
|
|
11
|
-
if not arguments.length:
|
|
12
|
-
exclude = {'split'
|
|
13
|
-
elif arguments[0]:
|
|
14
|
-
exclude = Array.prototype.slice.call(arguments)
|
|
15
|
-
else:
|
|
16
|
-
exclude = None
|
|
17
|
-
if exclude:
|
|
18
|
-
string_funcs = string_funcs.difference(set(exclude))
|
|
19
|
-
for name in string_funcs:
|
|
20
|
-
String.prototype[name] = ρσ_str.prototype[name]
|
|
1
|
+
# vim:fileencoding=utf-8
|
|
2
|
+
# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
|
|
3
|
+
# globals: ρσ_str
|
|
4
|
+
|
|
5
|
+
def strings():
|
|
6
|
+
string_funcs = set((
|
|
7
|
+
'capitalize strip lstrip rstrip islower isupper isspace lower upper swapcase title'
|
|
8
|
+
' center count endswith startswith find rfind index rindex format join ljust rjust'
|
|
9
|
+
' partition rpartition replace split rsplit splitlines zfill expandtabs').split(' '))
|
|
10
|
+
|
|
11
|
+
if not arguments.length:
|
|
12
|
+
exclude = {'split'}
|
|
13
|
+
elif arguments[0]:
|
|
14
|
+
exclude = Array.prototype.slice.call(arguments)
|
|
15
|
+
else:
|
|
16
|
+
exclude = None
|
|
17
|
+
if exclude:
|
|
18
|
+
string_funcs = string_funcs.difference(set(exclude))
|
|
19
|
+
for name in string_funcs:
|
|
20
|
+
String.prototype[name] = ρσ_str.prototype[name]
|
|
Binary file
|