forked from xpodev/extype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
61 lines (38 loc) · 1.15 KB
/
test.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
from extype.core import *
from extype import extension, extend_type_with, Protocol
def _():
...
function = type(_)
implement_protocol_on_type(function, Protocol.Number)
class FunctionExtension:
@extension
def __matmul__(self, other):
def _(*args, **kwargs):
return self(other(*args, **kwargs))
return _
@extension
def __lshift__(self, other):
assert isinstance(other, (tuple, dict))
def wrapper(*args, **kwargs):
if isinstance(other, tuple):
return self(*other, *args, **kwargs)
return self(*args, **other, **kwargs)
return wrapper
class IntExtension(int):
@extension
@property
def as_bytes(self):
return self.to_bytes(4, "little")
def foo(*args, **kwargs):
print(*args, **kwargs)
def bar(x, y):
return x + y
extend_type_with(function, FunctionExtension)
extend_type_with(int, IntExtension)
print(foo @ bar)
(foo @ bar)(1, 2)
print(bar << ())
print((bar << (3, 4))())
print_hello = foo << ("Hello",) # I would've used print directly, but we can't yet extend builtin functions
print_hello("World!")
print((2).as_bytes)