-
Notifications
You must be signed in to change notification settings - Fork 0
/
grapejam.py
134 lines (131 loc) · 4.16 KB
/
grapejam.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
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
#GrapeJam
__all__ = ["dumps", "loads", "dump", "load"]
import json
from datetime import datetime
class GrapeJamError(Exception):
def __init__(self, message):
pass
def dumps(obj):
if isinstance(obj, bytes):
newlist = list(obj)
return f"?{json.dumps(newlist)[1:][:-1]}"
elif isinstance(obj, bytearray):
newlist = list(obj)
return f"/{json.dumps(newlist)}"
elif obj in [True, False]:
return f"!{json.dumps(obj)}"
elif isinstance(obj, int):
return f"@{str(obj)}"
elif isinstance(obj, str):
return f"#{str(obj)}"
elif isinstance(obj, list):
for i, item in enumerate(obj):
if isinstance(item, (list, tuple)):
obj[i] = [chr(7), dumps(item)]
return f"${json.dumps(obj)}"
elif isinstance(obj, tuple):
obj = list(obj)
for i, item in enumerate(obj):
if isinstance(item, set):
obj[i] = ["*py-set-obj", list(item)]
elif isinstance(item, tuple):
obj[i] = ["*py-tuple-obj", list(item)]
return f"%{json.dumps(obj)}"
elif isinstance(obj, float):
return f"^{json.dumps(obj)}"
elif isinstance(obj, dict):
return f"&{json.dumps(obj)}"
elif obj == None:
return "*N"
elif isinstance(obj, range):
return f"+{json.dumps(list(obj))}"
elif isinstance(obj, complex):
return f"-{str(obj)}"
elif isinstance(obj, set):
newlist = list(obj)
return f"~{json.dumps(newlist)}"
elif isinstance(obj, frozenset):
newlist = list(obj)
return f":{json.dumps(newlist)}"
elif isinstance(obj, datetime):
converted = obj.strftime("%Y\n%m\n%d\n%H\n%M\n%S\n%f").splitlines();
return f"_{json.dumps(converted)}"
else:
raise GrapeJamError("problem processing object")
def loads(string):
valid = '!@#$%^&*+-~:?/_'
if string[0] not in valid:
raise GrapeJamError("problem processing string")
string = list(string)
t = string.pop(0)
string = "".join(string)
if t == "!":
return bool(json.loads(string))
elif t == "@":
return int(string)
elif t == "#":
return string
elif t == "$":
parsed = json.loads(string)
for i, _obj in enumerate(parsed):
if isinstance(_obj, list):
if _obj[0] == chr(7):
parsed[i] = loads(_obj[1])
else:
pass
return parsed
elif t == "%":
parsed = json.loads(string)
for i, _obj in enumerate(parsed):
if isinstance(_obj, list):
if _obj[0] == "py-set-obj":
parsed[i] = set(_obj[1])
elif _obj[0] == "py-tuple-obj":
parsed[i] = tuple(_obj[1])
else:
pass
return tuple(parsed)
elif t == "^" or t == "&":
return json.loads(string)
elif t == "*":
if string == "*N":
return None
elif t == "+":
m = json.loads(string)
n = min(m)
o = max(m) + 1
return range(n, o)
elif t == "-":
return complex(string)
elif t == "~":
return set(json.loads(string))
elif t == ":":
return frozenset(json.loads(string))
elif t == "?":
return bytes(json.loads("[" + string + "]"))
elif t == "/":
return bytearray(json.loads(string))
elif t == "_":
a = json.loads(string)
a = "-".join(a)
return datetime.strptime(a, "%Y-%m-%d-%H-%M-%S-%f")
else:
raise GrapeJamError("problem processing string")
def dump(obj, file):
stringified = dumps(obj)
file.write(stringified)
def load(file):
s = file.read()
parsed = loads(s)
return parsed
def test():
a = dumps("hello")
b = dumps(["mouse", [2, 8, 4, "string"], None, ("tuple",)])
c = dumps(range(15))
d = loads(a)
e = loads(b)
f = loads(c)
for obj in a, b, c, "\n", d, e, f:
print(obj)
if __name__ == "__main__":
test()