forked from automerge/automerge-py
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
124 lines (83 loc) · 2.44 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
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
#!/usr/bin/env python3
import automerge
import json
def dump(doc: automerge.Document):
if isinstance(doc, automerge.Mapping):
res = {}
for name, value in automerge.entries(doc):
if isinstance(value, automerge.Document):
value = dump(value)
elif isinstance(value, automerge.Counter):
value = value.get()
elif isinstance(value, automerge.Text):
value = str(value)
res[name] = value
else: # sequence
res = []
for value in doc:
if isinstance(value, automerge.Document):
value = dump(value)
elif isinstance(value, automerge.Counter):
value = value.get()
elif isinstance(value, automerge.Text):
value = str(value)
res.append(value)
return res
def dd(doc):
print(json.dumps(dump(doc), indent=4))
doc = automerge.init()
with automerge.transaction(doc) as d:
d.hello = {"hello": [{"a":1, "2":3}]}
# d.riea = [1,2,3,4,5, "riea"]
d.riea = [1,2,3,4,5]
d.riea[:1] = [1,2,34]
d.riea[::3] = [1,2,34]
# print(doc.riea[::3])
# for k in doc:
# print(k)
dd(doc)
with automerge.transaction(doc) as d:
d.hello.hello[0] = False # = {"hello": [{"a":1, "2":3}]}
# d.riea.insert(1, 3)
dd(doc)
data = automerge.save(doc)
doc2 = automerge.load(data)
with automerge.transaction(doc) as d:
d.hello.hello[4:] = [1,3,4]
with automerge.transaction(doc2) as d:
d.hello.hello[:0] = [1]
dd(doc)
dd(doc2)
automerge.merge(doc, doc2)
dd(doc)
change = automerge.get_last_local_change(doc).bytes()
print(change)
automerge.apply_changes(doc2, [change])
dd(doc2)
doc3 = automerge.fork(doc)
with automerge.transaction(doc3) as d:
d.text = automerge.Text("hello")
d.counter = automerge.Counter(123)
doc_a = automerge.fork(doc3)
doc_b = automerge.fork(doc3)
with automerge.transaction(doc_a) as d:
d.text[:0] = "Somebody says: "
d.counter.increment(100)
with automerge.transaction(doc_b) as d:
d.text[10000:] = ", World! :)"
d.counter.increment(10)
dd(doc_a)
dd(doc_b)
automerge.merge(doc_a, doc_b)
dd(doc_a)
from typing import Dict, List
class Idea:
name: str
priority: float
class MyDoc:
ideas: List[Idea]
ideas_doc = automerge.init(MyDoc)
with automerge.transaction(ideas_doc) as d:
d.ideas = [{"name": "a idea", "priority": 1.0}]
d.ideas[0].name = "other name"
dd(ideas_doc)