-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathsnippets.py
88 lines (69 loc) · 2.78 KB
/
snippets.py
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
# -*- coding: utf-8 -*-
# :Project: metapensiero.pj -- code to aid transformation, it gets converted
# :Created: mar 01 mar 2016 01:42:26 CET
# :Author: Alberto Berti <[email protected]>
# :License: GNU General Public License version 3 or later
#
def _in(left, right):
from __globals__ import Array, typeof
if isinstance(right, Array) or typeof(right) == 'string':
return right.indexOf(left) > -1
else:
return left in right
def in_es6(left, right):
from __globals__ import Array, typeof, Map, Set, WeakMap, WeakSet
if isinstance(right, Array) or typeof(right) == 'string':
return right.indexOf(left) > -1
elif isinstance(right, (Map, Set, WeakMap, WeakSet)):
return right.has(left)
else:
return left in right
def set_decorators(cls, props):
from __globals__ import Function, Map, WeakMap, Object
for p in dict(props):
decos = props[p]
def reducer(val, deco):
return deco(val, cls, p)
deco = decos.reduce(reducer, cls.prototype[p])
if not isinstance(deco, (Function, Map, WeakMap)) and \
isinstance(deco, Object) and (('value' in deco) or
('get' in deco)):
del cls.prototype[p]
Object.defineProperty(cls.prototype, p, deco)
else:
cls.prototype[p] = deco
def set_class_decorators(cls, decos):
def reducer(val, deco):
return deco(val, cls)
return decos.reduce(reducer, cls)
def set_properties(cls, props):
from __globals__ import Function, Map, WeakMap, Object
for p in dict(props):
value = props[p]
if not isinstance(value, (Map, WeakMap)) and isinstance(value, Object) \
and 'get' in value and isinstance(value.get, Function):
# the following condition raises a TypeError in dukpy, why?
# ('set' in value and isinstance(value.set, Function)):
desc = value
else:
desc = {
'value': value,
'enumerable': False,
'configurable': True,
'writable': True
}
Object.defineProperty(cls.prototype, p, desc)
def _assert(comp, msg):
from __globals__ import Error, Object, typeof
def PJAssertionError(self, message):
self.name = 'PJAssertionError'
self.message = message or 'Custom error PJAssertionError'
if typeof(Error.captureStackTrace) == 'function':
Error.captureStackTrace(self, self.constructor)
else:
self.stack = Error(message).stack
PJAssertionError.prototype = Object.create(Error.prototype)
PJAssertionError.prototype.constructor = PJAssertionError
msg = msg or 'Assertion failed.'
if not comp:
raise PJAssertionError(msg)