Spaces:
Runtime error
Runtime error
Dmitry Kirsanov commited on
Commit ·
4d5bdd2
1
Parent(s): 7c8677a
more api development and tests
Browse files
fad.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
-
import re
|
| 2 |
import bidict
|
|
|
|
|
|
|
| 3 |
|
| 4 |
notetypes = bidict.bidict({
|
| 5 |
"document": 0,
|
|
@@ -16,8 +18,8 @@ notetypes = bidict.bidict({
|
|
| 16 |
"actor": 7,
|
| 17 |
"audience": 8,
|
| 18 |
|
| 19 |
-
"
|
| 20 |
-
"
|
| 21 |
|
| 22 |
"chunk": 11,
|
| 23 |
"topic": 12,
|
|
@@ -33,6 +35,7 @@ chunktypes = bidict.bidict({
|
|
| 33 |
"reference": 5
|
| 34 |
})
|
| 35 |
|
|
|
|
| 36 |
|
| 37 |
idlength = 8
|
| 38 |
|
|
@@ -46,107 +49,12 @@ class Fad:
|
|
| 46 |
|
| 47 |
highest_note_id = 0
|
| 48 |
|
| 49 |
-
|
| 50 |
-
class Note:
|
| 51 |
-
def __init__(self, parentfad, id, typeid, value=None) -> None:
|
| 52 |
-
self.id = id
|
| 53 |
-
self.timestamp = time.time()
|
| 54 |
-
self.typeid = typeid
|
| 55 |
-
#print(f"constructor: {self.type()} id {id} timestamp {self.timestamp}")
|
| 56 |
-
self.parentfad = parentfad
|
| 57 |
-
self.properties = {}
|
| 58 |
-
if value is not None:
|
| 59 |
-
self.properties["value"] = value
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
def type(self):
|
| 63 |
-
return notetypes.inverse[self.typeid][0]
|
| 64 |
-
|
| 65 |
-
def get(self, name=None):
|
| 66 |
-
propertyname = name if name is not None else "value"
|
| 67 |
-
if propertyname in self.properties.keys():
|
| 68 |
-
value = self.properties[propertyname] # "value" is default property
|
| 69 |
-
else:
|
| 70 |
-
value = None
|
| 71 |
-
if isinstance(value, int): # any int value is supposed to be a ref to id; if you need to store numbers, use floats
|
| 72 |
-
return self.parentfad.notes[value].get() # so we pass it to the target mark, recursively, but without name: target note gives us just its value
|
| 73 |
-
elif isinstance(value, list):
|
| 74 |
-
return [(self.parentfad.notes[v].get() if isinstance(v, int) else v) for v in value if v] # do the same for each element in list
|
| 75 |
-
else:
|
| 76 |
-
return value
|
| 77 |
-
|
| 78 |
-
def get_string(self, name=None):
|
| 79 |
-
value = self.get(name)
|
| 80 |
-
if value == None:
|
| 81 |
-
return ''
|
| 82 |
-
elif isinstance(value, list):
|
| 83 |
-
return ", ".join(value)
|
| 84 |
-
else:
|
| 85 |
-
return value
|
| 86 |
-
|
| 87 |
-
def set(self, value):
|
| 88 |
-
if isinstance(value, int): # any int value is supposed to be a ref to id
|
| 89 |
-
assert(value in self.parentfad.notes.keys()) # so we ensure this id exists
|
| 90 |
-
self.properties["value"] = value
|
| 91 |
-
self.timestamp = time.time()
|
| 92 |
-
#print(f"set value: {self.type()} id {id} timestamp {self.timestamp}")
|
| 93 |
-
|
| 94 |
-
def set_property(self, name, value):
|
| 95 |
-
if isinstance(value, int): # any int value is supposed to be a ref to id
|
| 96 |
-
assert(value in self.parentfad.notes.keys()) # so we ensure this id exists
|
| 97 |
-
self.properties[name] = value
|
| 98 |
-
self.timestamp = time.time()
|
| 99 |
-
#print(f"set name value: {self.type()} id {id} timestamp {self.timestamp}")
|
| 100 |
-
|
| 101 |
-
def add_property(self, name, value):
|
| 102 |
-
if isinstance(value, int): # any int value is supposed to be a ref to id
|
| 103 |
-
if value not in self.parentfad.notes.keys(): # so we ensure this id exists
|
| 104 |
-
raise(Exception(f"Note: add: trying to add an id ({value}) which does not point to a Note"))
|
| 105 |
-
if not name in self.properties.keys() or self.properties[name] is None: # no such property yet, or it's None: create list
|
| 106 |
-
self.properties[name] = [value] if value is not None else []
|
| 107 |
-
self.timestamp = time.time()
|
| 108 |
-
elif isinstance(self.properties[name], list): # already a list
|
| 109 |
-
self.properties[name].append(value)
|
| 110 |
-
self.timestamp = time.time()
|
| 111 |
-
else:
|
| 112 |
-
raise(Exception(f"Note: add: trying to add to list (properties[{name}]) but the existing value is not a list"))
|
| 113 |
-
#print(f"add: {self.type()} id {id} timestamp {self.timestamp}")
|
| 114 |
-
|
| 115 |
-
def get_refs(self):
|
| 116 |
-
refs = []
|
| 117 |
-
for key, note in self.parentfad.notes.items():
|
| 118 |
-
for pkey, pvalue in note.properties.items():
|
| 119 |
-
if isinstance(pvalue, int):
|
| 120 |
-
if pvalue == self.id:
|
| 121 |
-
refs.append(key)
|
| 122 |
-
return refs
|
| 123 |
-
|
| 124 |
-
def touch(self):
|
| 125 |
-
self.timestamp = time.time()
|
| 126 |
-
|
| 127 |
-
def needs_updating_from(self):
|
| 128 |
-
refs_to_update = []
|
| 129 |
-
for key, value in self.properties.items():
|
| 130 |
-
if isinstance(value, int):
|
| 131 |
-
if not(self.timestamp >= self.parentfad.notes[value].timestamp):
|
| 132 |
-
print(f"i refer to something newer than me! i need updating!\n me: {self.type()} at {self.timestamp}\n refer to: {self.parentfad.notes[value].type()} at {self.parentfad.notes[value].timestamp}")
|
| 133 |
-
refs_to_update.append(value)
|
| 134 |
-
return refs_to_update
|
| 135 |
-
|
| 136 |
-
def delete(self):
|
| 137 |
-
if self.get_refs():
|
| 138 |
-
raise(Exception(f"Note: cannot delete a note that has references to it: from {self.get_refs()}"))
|
| 139 |
-
else:
|
| 140 |
-
self.parentfad.notes.pop(self.id)
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
def newnote(self, type, value=None):
|
| 145 |
id = self.highest_note_id + 1
|
| 146 |
type_code = notetypes[type]
|
| 147 |
self.highest_note_id += 1
|
| 148 |
-
self.notes[id] =
|
| 149 |
-
return id
|
| 150 |
|
| 151 |
def add(self, type, value=None):
|
| 152 |
self.newnote(type, value)
|
|
@@ -156,21 +64,25 @@ class Fad:
|
|
| 156 |
self.newnote(type, value)
|
| 157 |
|
| 158 |
def set(self, type, value=None):
|
| 159 |
-
for
|
| 160 |
-
|
| 161 |
|
| 162 |
|
| 163 |
-
def
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
for id, note in self.notes.items():
|
| 166 |
if note.typeid == notetypes[valuetype]:
|
| 167 |
-
|
| 168 |
-
return ids
|
| 169 |
|
| 170 |
def delete_by_type(self, valuetype):
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
self.notes[id].delete()
|
| 174 |
|
| 175 |
def get_values_by_type(self, valuetype):
|
| 176 |
values = []
|
|
@@ -187,19 +99,22 @@ class Fad:
|
|
| 187 |
def get(self, src="document"):
|
| 188 |
return self.get_value_by_type(src)
|
| 189 |
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
-
def get_by_value(self, value, valuetype):
|
| 192 |
for id, note in self.notes.items():
|
| 193 |
if note.typeid == notetypes[valuetype] and note.get() == value:
|
| 194 |
-
return
|
| 195 |
return None
|
| 196 |
|
| 197 |
-
def get_by_type_and_value(self, type, value):
|
| 198 |
ret = []
|
| 199 |
-
for id,
|
| 200 |
-
if notetypes[type] ==
|
| 201 |
-
if value ==
|
| 202 |
-
ret.append(
|
| 203 |
return ret
|
| 204 |
|
| 205 |
|
|
@@ -229,20 +144,21 @@ class Fad:
|
|
| 229 |
def get_plaintext_with_markup(self, notetype, src="document", show_id=False, show_properties=True, properties_to_show=[]): # [] means show all
|
| 230 |
fad = self.get_value_by_type(src)
|
| 231 |
i = 0
|
| 232 |
-
|
| 233 |
while True:
|
| 234 |
-
text, markchar, id, newi = self.__next_mark(
|
| 235 |
-
if
|
| 236 |
-
break
|
| 237 |
-
if self.notes[id].typeid == notetypes[notetype]:
|
| 238 |
id_s = f' id="{id}"' if show_id else ''
|
| 239 |
properties_s = ''.join([(f' {name}="{str(self.notes[id].get_string(name))}"' if show_properties and self.notes[id].get(name) and (not(properties_to_show) or name in properties_to_show) else '') for name in self.notes[id].properties.keys()]) # show if a property's value is not [], None, or 0
|
| 240 |
if markchar == '⌧':
|
| 241 |
-
|
| 242 |
else:
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
i = newi
|
| 245 |
-
return self.__plaintext(newfad)
|
| 246 |
|
| 247 |
def get_spans_by_note_type(self, notetype, src="document"):
|
| 248 |
fad = self.get_value_by_type(src)
|
|
@@ -276,8 +192,15 @@ class Fad:
|
|
| 276 |
|
| 277 |
def get_spans_by_property_value(self, notetype, proprtyname, propertyvalue, src="document"): # when we only need to check one value
|
| 278 |
return self.get_spans_by_property_values(notetype, proprtyname, [propertyvalue], src)
|
| 279 |
-
|
| 280 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
|
| 282 |
|
| 283 |
def openmark(self, id):
|
|
@@ -304,28 +227,28 @@ class Fad:
|
|
| 304 |
else:
|
| 305 |
i = newi
|
| 306 |
|
| 307 |
-
def add_span_mark(self, notetype, start, end, src="document"):
|
| 308 |
fad = self.get_value_by_type(src)
|
| 309 |
-
id = self.newnote(notetype)
|
| 310 |
start_i = self.__goto_plaintext_i(fad, start)
|
| 311 |
end_i = self.__goto_plaintext_i(fad, end)
|
| 312 |
if start_i == None or end_i == None:
|
| 313 |
-
raise Exception("
|
| 314 |
-
|
|
|
|
| 315 |
self.set(src, newfad)
|
| 316 |
-
return
|
| 317 |
|
| 318 |
-
def add_point_mark(self, notetype, start, src="document"):
|
| 319 |
fad = self.get_value_by_type(src)
|
| 320 |
-
id = self.newnote(notetype)
|
| 321 |
start_i = self.__goto_plaintext_i(fad, start)
|
| 322 |
if start_i == None:
|
| 323 |
-
raise Exception("
|
| 324 |
-
|
|
|
|
| 325 |
self.set(src, newfad)
|
| 326 |
-
return
|
| 327 |
|
| 328 |
-
def get_note_for_mark_n(self, notetype, mark_n, startfrom=0, src="document"):
|
| 329 |
fad = self.get_value_by_type(src)
|
| 330 |
i = startfrom
|
| 331 |
n = 0
|
|
@@ -337,25 +260,26 @@ class Fad:
|
|
| 337 |
if self.notes[id].typeid == notetypes[notetype]:
|
| 338 |
n += 1
|
| 339 |
if n == mark_n:
|
| 340 |
-
return id
|
| 341 |
i = newi
|
| 342 |
|
| 343 |
|
| 344 |
def set_property_to_mark(self, notetype, mark_n, property, value, valuetype, startfrom=0, src="document"):
|
| 345 |
-
|
| 346 |
if valuetype != None: # we're setting value to ref to a note whose type is valuetype
|
| 347 |
-
|
| 348 |
-
|
| 349 |
else: # we're setting leaf string value to property
|
| 350 |
-
|
| 351 |
|
| 352 |
def add_property_to_mark(self, notetype, mark_n, property, value, valuetype, startfrom=0, src="document"):
|
| 353 |
-
|
| 354 |
if valuetype != None: # we're setting value to ref to a note whose type is valuetype
|
| 355 |
-
|
| 356 |
-
|
|
|
|
| 357 |
else: # we're setting leaf string value to property
|
| 358 |
-
|
| 359 |
|
| 360 |
|
| 361 |
|
|
@@ -451,6 +375,25 @@ if __name__ == "__main__":
|
|
| 451 |
|
| 452 |
assert(f.get_spans_by_property_value("span", "audiences", 'audience2') == ['A123456789', '56789B123456789C1234'])
|
| 453 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
for id, note in f.notes.items():
|
| 455 |
assert(note.needs_updating_from() == []) # in normal operation, there should be no contratemporal refs X -> something newer than X (it means X needs updating)
|
| 456 |
|
|
|
|
| 1 |
+
import re
|
| 2 |
import bidict
|
| 3 |
+
import inspect
|
| 4 |
+
from note import Note
|
| 5 |
|
| 6 |
notetypes = bidict.bidict({
|
| 7 |
"document": 0,
|
|
|
|
| 18 |
"actor": 7,
|
| 19 |
"audience": 8,
|
| 20 |
|
| 21 |
+
"what-this-is": 9,
|
| 22 |
+
"summary": 10,
|
| 23 |
|
| 24 |
"chunk": 11,
|
| 25 |
"topic": 12,
|
|
|
|
| 35 |
"reference": 5
|
| 36 |
})
|
| 37 |
|
| 38 |
+
supplementals = ["what-this-is","summary"]
|
| 39 |
|
| 40 |
idlength = 8
|
| 41 |
|
|
|
|
| 49 |
|
| 50 |
highest_note_id = 0
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
def newnote(self, type, value=None):
|
| 53 |
id = self.highest_note_id + 1
|
| 54 |
type_code = notetypes[type]
|
| 55 |
self.highest_note_id += 1
|
| 56 |
+
self.notes[id] = Note(self, id, type_code, value)
|
| 57 |
+
return self.notes[id]
|
| 58 |
|
| 59 |
def add(self, type, value=None):
|
| 60 |
self.newnote(type, value)
|
|
|
|
| 64 |
self.newnote(type, value)
|
| 65 |
|
| 66 |
def set(self, type, value=None):
|
| 67 |
+
for note in self.get_notes_by_type(type):
|
| 68 |
+
note.set(value)
|
| 69 |
|
| 70 |
|
| 71 |
+
def get_notes_by_type(self, valuetype):
|
| 72 |
+
notes = []
|
| 73 |
+
for id, note in self.notes.items():
|
| 74 |
+
if note.typeid == notetypes[valuetype]:
|
| 75 |
+
notes.append(note)
|
| 76 |
+
return notes
|
| 77 |
+
|
| 78 |
+
def get_note_by_type(self, valuetype): # if we know there's only one such note
|
| 79 |
for id, note in self.notes.items():
|
| 80 |
if note.typeid == notetypes[valuetype]:
|
| 81 |
+
return note
|
|
|
|
| 82 |
|
| 83 |
def delete_by_type(self, valuetype):
|
| 84 |
+
for note in self.get_notes_by_type(valuetype):
|
| 85 |
+
note.delete()
|
|
|
|
| 86 |
|
| 87 |
def get_values_by_type(self, valuetype):
|
| 88 |
values = []
|
|
|
|
| 99 |
def get(self, src="document"):
|
| 100 |
return self.get_value_by_type(src)
|
| 101 |
|
| 102 |
+
def get_note(self, src="document"):
|
| 103 |
+
return self.get_note_by_type(src)
|
| 104 |
+
|
| 105 |
|
| 106 |
+
def get_by_value(self, value, valuetype) -> Note:
|
| 107 |
for id, note in self.notes.items():
|
| 108 |
if note.typeid == notetypes[valuetype] and note.get() == value:
|
| 109 |
+
return note
|
| 110 |
return None
|
| 111 |
|
| 112 |
+
def get_by_type_and_value(self, type, value) -> list[Note]:
|
| 113 |
ret = []
|
| 114 |
+
for id, note in self.notes.items():
|
| 115 |
+
if notetypes[type] == note.typeid:
|
| 116 |
+
if value == note.get():
|
| 117 |
+
ret.append(note)
|
| 118 |
return ret
|
| 119 |
|
| 120 |
|
|
|
|
| 144 |
def get_plaintext_with_markup(self, notetype, src="document", show_id=False, show_properties=True, properties_to_show=[]): # [] means show all
|
| 145 |
fad = self.get_value_by_type(src)
|
| 146 |
i = 0
|
| 147 |
+
plaintext = ''
|
| 148 |
while True:
|
| 149 |
+
text, markchar, id, newi = self.__next_mark(fad, i)
|
| 150 |
+
if id and self.notes[id].typeid == notetypes[notetype]:
|
|
|
|
|
|
|
| 151 |
id_s = f' id="{id}"' if show_id else ''
|
| 152 |
properties_s = ''.join([(f' {name}="{str(self.notes[id].get_string(name))}"' if show_properties and self.notes[id].get(name) and (not(properties_to_show) or name in properties_to_show) else '') for name in self.notes[id].properties.keys()]) # show if a property's value is not [], None, or 0
|
| 153 |
if markchar == '⌧':
|
| 154 |
+
plaintext += text + f'<{notetype}{id_s}{properties_s}/>'
|
| 155 |
else:
|
| 156 |
+
plaintext += text + f'<{"/" if markchar == "⌫" else ""}{notetype}{id_s}{properties_s if markchar == "⌦" else ""}>'
|
| 157 |
+
else:
|
| 158 |
+
plaintext += text
|
| 159 |
+
if newi == None:
|
| 160 |
+
return plaintext
|
| 161 |
i = newi
|
|
|
|
| 162 |
|
| 163 |
def get_spans_by_note_type(self, notetype, src="document"):
|
| 164 |
fad = self.get_value_by_type(src)
|
|
|
|
| 192 |
|
| 193 |
def get_spans_by_property_value(self, notetype, proprtyname, propertyvalue, src="document"): # when we only need to check one value
|
| 194 |
return self.get_spans_by_property_values(notetype, proprtyname, [propertyvalue], src)
|
| 195 |
+
|
| 196 |
+
def is_newer(self, src, tgt):
|
| 197 |
+
s = self.get_note(src)
|
| 198 |
+
if s == None:
|
| 199 |
+
return False
|
| 200 |
+
t = s.get(tgt)
|
| 201 |
+
if t == None:
|
| 202 |
+
return True
|
| 203 |
+
return s.is_newer(tgt)
|
| 204 |
|
| 205 |
|
| 206 |
def openmark(self, id):
|
|
|
|
| 227 |
else:
|
| 228 |
i = newi
|
| 229 |
|
| 230 |
+
def add_span_mark(self, notetype, start, end, src="document") -> Note:
|
| 231 |
fad = self.get_value_by_type(src)
|
|
|
|
| 232 |
start_i = self.__goto_plaintext_i(fad, start)
|
| 233 |
end_i = self.__goto_plaintext_i(fad, end)
|
| 234 |
if start_i == None or end_i == None:
|
| 235 |
+
raise Exception(f"Fad: {inspect.stack()[0][3]}: start or end outside range")
|
| 236 |
+
newnote = self.newnote(notetype)
|
| 237 |
+
newfad = fad[:start_i] + self.openmark(newnote.id) + fad[start_i:end_i] + self.closemark(newnote.id) + fad[end_i:]
|
| 238 |
self.set(src, newfad)
|
| 239 |
+
return newnote
|
| 240 |
|
| 241 |
+
def add_point_mark(self, notetype, start, src="document") -> Note:
|
| 242 |
fad = self.get_value_by_type(src)
|
|
|
|
| 243 |
start_i = self.__goto_plaintext_i(fad, start)
|
| 244 |
if start_i == None:
|
| 245 |
+
raise Exception(f"Fad: {inspect.stack()[0][3]}: start outside range")
|
| 246 |
+
newnote = self.newnote(notetype)
|
| 247 |
+
newfad = fad[:start_i] + self.pointmark(newnote.id) + fad[start_i:]
|
| 248 |
self.set(src, newfad)
|
| 249 |
+
return newnote
|
| 250 |
|
| 251 |
+
def get_note_for_mark_n(self, notetype, mark_n, startfrom=0, src="document") -> Note:
|
| 252 |
fad = self.get_value_by_type(src)
|
| 253 |
i = startfrom
|
| 254 |
n = 0
|
|
|
|
| 260 |
if self.notes[id].typeid == notetypes[notetype]:
|
| 261 |
n += 1
|
| 262 |
if n == mark_n:
|
| 263 |
+
return self.notes[id]
|
| 264 |
i = newi
|
| 265 |
|
| 266 |
|
| 267 |
def set_property_to_mark(self, notetype, mark_n, property, value, valuetype, startfrom=0, src="document"):
|
| 268 |
+
note = self.get_note_for_mark_n(notetype, mark_n, startfrom, src)
|
| 269 |
if valuetype != None: # we're setting value to ref to a note whose type is valuetype
|
| 270 |
+
valuenote = self.get_by_value(value, valuetype)
|
| 271 |
+
note.set_property(property, valuenote.id if valuenote else None)
|
| 272 |
else: # we're setting leaf string value to property
|
| 273 |
+
note.set_property(property, value)
|
| 274 |
|
| 275 |
def add_property_to_mark(self, notetype, mark_n, property, value, valuetype, startfrom=0, src="document"):
|
| 276 |
+
note = self.get_note_for_mark_n(notetype, mark_n, startfrom, src)
|
| 277 |
if valuetype != None: # we're setting value to ref to a note whose type is valuetype
|
| 278 |
+
valuenote = self.get_by_value(value, valuetype)
|
| 279 |
+
if valuenote:
|
| 280 |
+
note.add_to_property(property, valuenote.id)
|
| 281 |
else: # we're setting leaf string value to property
|
| 282 |
+
note.add_to_property(property, value)
|
| 283 |
|
| 284 |
|
| 285 |
|
|
|
|
| 375 |
|
| 376 |
assert(f.get_spans_by_property_value("span", "audiences", 'audience2') == ['A123456789', '56789B123456789C1234'])
|
| 377 |
|
| 378 |
+
point2 = f.get_note_for_mark_n("point", 2)
|
| 379 |
+
point3 = f.get_note_for_mark_n("point", 3)
|
| 380 |
+
point2.set_property_newnote("concepts", "important concept 1", "concept")
|
| 381 |
+
|
| 382 |
+
#print(f.get_plaintext_with_markup("point", show_id=False, show_properties=True, properties_to_show=['concepts']))
|
| 383 |
+
assert(f.get_plaintext_with_markup("point", show_id=False, show_properties=True, properties_to_show=['concepts']) == '<point/>A<point concepts="important concept 1"/><point/>123456789B123456789C123456789')
|
| 384 |
+
|
| 385 |
+
point3.set_property_newnote("concepts", "important concept 2", "concept")
|
| 386 |
+
point2.add_to_property_newnote("concepts", "important concept 2", "concept")
|
| 387 |
+
|
| 388 |
+
assert(len(f.get_by_type_and_value("concept", "important concept 2")) == 1)
|
| 389 |
+
assert(f.get_plaintext_with_markup("point", show_id=False, show_properties=True, properties_to_show=['concepts']) == '<point/>A<point concepts="important concept 1, important concept 2"/><point concepts="important concept 2"/>123456789B123456789C123456789')
|
| 390 |
+
|
| 391 |
+
point2.set_property("concepts", None)
|
| 392 |
+
|
| 393 |
+
#print(f.get_plaintext_with_markup("point", show_id=False, show_properties=True, properties_to_show=['concepts']))
|
| 394 |
+
assert(f.get_plaintext_with_markup("point", show_id=False, show_properties=True, properties_to_show=['concepts']) == '<point/>A<point/><point concepts="important concept 2"/>123456789B123456789C123456789')
|
| 395 |
+
assert(f.get_plaintext_with_markup("point", show_id=False, show_properties=False) == '<point/>A<point/><point/>123456789B123456789C123456789')
|
| 396 |
+
|
| 397 |
for id, note in f.notes.items():
|
| 398 |
assert(note.needs_updating_from() == []) # in normal operation, there should be no contratemporal refs X -> something newer than X (it means X needs updating)
|
| 399 |
|
note.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
import inspect
|
| 3 |
+
|
| 4 |
+
class Note:
|
| 5 |
+
def __init__(self, parentfad, id, typeid, value=None) -> None:
|
| 6 |
+
self.id = id
|
| 7 |
+
self.timestamp = time.time()
|
| 8 |
+
self.typeid = typeid
|
| 9 |
+
#print(f"constructor: {self.type()} id {id} timestamp {self.timestamp}")
|
| 10 |
+
self.parentfad = parentfad
|
| 11 |
+
self.properties = {}
|
| 12 |
+
if value is not None:
|
| 13 |
+
self.properties["value"] = value
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def type(self):
|
| 17 |
+
return notetypes.inverse[self.typeid][0]
|
| 18 |
+
|
| 19 |
+
def get(self, name=None):
|
| 20 |
+
propertyname = name if name is not None else "value"
|
| 21 |
+
if propertyname in self.properties.keys():
|
| 22 |
+
value = self.properties[propertyname] # "value" is default property
|
| 23 |
+
else:
|
| 24 |
+
value = None
|
| 25 |
+
if isinstance(value, int): # any int value is supposed to be a ref to id; if you need to store numbers, use floats
|
| 26 |
+
return self.parentfad.notes[value].get() # so we pass it to the target mark, recursively, but without name: target note gives us just its value
|
| 27 |
+
elif isinstance(value, list):
|
| 28 |
+
return [(self.parentfad.notes[v].get() if isinstance(v, int) else v) for v in value if v] # do the same for each element in list
|
| 29 |
+
else:
|
| 30 |
+
return value
|
| 31 |
+
|
| 32 |
+
def get_string(self, name=None):
|
| 33 |
+
value = self.get(name)
|
| 34 |
+
if value == None:
|
| 35 |
+
return ''
|
| 36 |
+
elif isinstance(value, list):
|
| 37 |
+
return ", ".join(value)
|
| 38 |
+
else:
|
| 39 |
+
return value
|
| 40 |
+
|
| 41 |
+
def set(self, value):
|
| 42 |
+
if isinstance(value, int): # any int value is supposed to be a ref to id
|
| 43 |
+
assert(value in self.parentfad.notes.keys()) # so we ensure this id exists
|
| 44 |
+
self.properties["value"] = value
|
| 45 |
+
self.timestamp = time.time()
|
| 46 |
+
#print(f"set value: {self.type()} id {id} timestamp {self.timestamp}")
|
| 47 |
+
|
| 48 |
+
def set_property(self, name, value):
|
| 49 |
+
if isinstance(value, int): # any int value is supposed to be a ref to id
|
| 50 |
+
assert(value in self.parentfad.notes.keys()) # so we ensure this id exists
|
| 51 |
+
self.properties[name] = value
|
| 52 |
+
self.timestamp = time.time()
|
| 53 |
+
#print(f"set name value: {self.type()} id {id} timestamp {self.timestamp}")
|
| 54 |
+
|
| 55 |
+
def set_property_newnote(self, name, value, valuetype):
|
| 56 |
+
existing = self.parentfad.get_by_type_and_value(valuetype, value)
|
| 57 |
+
if existing:
|
| 58 |
+
note = existing[0] # we assume there's only one
|
| 59 |
+
else:
|
| 60 |
+
note = self.parentfad.newnote(valuetype, value)
|
| 61 |
+
self.properties[name] = note.id
|
| 62 |
+
self.timestamp = time.time()
|
| 63 |
+
#print(f"set name value: {self.type()} id {id} timestamp {self.timestamp}")
|
| 64 |
+
return note
|
| 65 |
+
|
| 66 |
+
def add_to_property(self, name, value):
|
| 67 |
+
if isinstance(value, int): # any int value is supposed to be a ref to id
|
| 68 |
+
if value not in self.parentfad.notes.keys(): # so we ensure this id exists
|
| 69 |
+
raise(Exception(f"Note: {inspect.stack()[0][3]}: trying to add an id ({value}) which does not point to a Note"))
|
| 70 |
+
elif value == None:
|
| 71 |
+
raise(Exception(f"Note: {inspect.stack()[0][3]}: trying to add a None value"))
|
| 72 |
+
if not name in self.properties.keys() or self.properties[name] is None: # no such property yet, or it's None: create list
|
| 73 |
+
self.properties[name] = [value]
|
| 74 |
+
elif isinstance(self.properties[name], list): # already a list
|
| 75 |
+
self.properties[name].append(value)
|
| 76 |
+
else: # not a list nor None, create list from the existing value and new one
|
| 77 |
+
self.properties[name] = [self.properties[name], value]
|
| 78 |
+
self.timestamp = time.time()
|
| 79 |
+
#print(f"add: {self.type()} id {id} timestamp {self.timestamp}")
|
| 80 |
+
|
| 81 |
+
def add_to_property_newnote(self, name, value, valuetype):
|
| 82 |
+
if value == None:
|
| 83 |
+
raise(Exception(f"Note: {inspect.stack()[0][3]}: trying to add a None value"))
|
| 84 |
+
existing = self.parentfad.get_by_type_and_value(valuetype, value)
|
| 85 |
+
if existing:
|
| 86 |
+
note = existing[0] # we assume there's only one
|
| 87 |
+
else:
|
| 88 |
+
note = self.parentfad.newnote(valuetype, value)
|
| 89 |
+
if not name in self.properties.keys() or self.properties[name] is None: # no such property yet, or it's None: create list
|
| 90 |
+
self.properties[name] = [note.id]
|
| 91 |
+
elif isinstance(self.properties[name], list): # already a list
|
| 92 |
+
self.properties[name].append(note.id)
|
| 93 |
+
else: # not a list nor None, create list from the existing value and new one
|
| 94 |
+
self.properties[name] = [self.properties[name], note.id]
|
| 95 |
+
self.timestamp = time.time()
|
| 96 |
+
return note
|
| 97 |
+
|
| 98 |
+
def get_refs(self):
|
| 99 |
+
refs = []
|
| 100 |
+
for key, note in self.parentfad.notes.items():
|
| 101 |
+
for pkey, pvalue in note.properties.items():
|
| 102 |
+
if isinstance(pvalue, int):
|
| 103 |
+
if pvalue == self.id:
|
| 104 |
+
refs.append(key)
|
| 105 |
+
return refs
|
| 106 |
+
|
| 107 |
+
def touch(self):
|
| 108 |
+
self.timestamp = time.time()
|
| 109 |
+
|
| 110 |
+
def is_newer(self, target: 'Note'): # type: ignore
|
| 111 |
+
return self.timestamp >= target.timestamp
|
| 112 |
+
|
| 113 |
+
def needs_updating_from(self):
|
| 114 |
+
refs_to_update = []
|
| 115 |
+
for key, value in self.properties.items():
|
| 116 |
+
if isinstance(value, int):
|
| 117 |
+
if not(self.is_newer(self.parentfad.notes[value])):
|
| 118 |
+
print(f"i refer to something newer than me! i need updating!\n me: {self.type()} at {self.timestamp}\n refer to: {self.parentfad.notes[value].type()} at {self.parentfad.notes[value].timestamp}")
|
| 119 |
+
refs_to_update.append(value)
|
| 120 |
+
return refs_to_update
|
| 121 |
+
|
| 122 |
+
def delete(self):
|
| 123 |
+
if self.get_refs():
|
| 124 |
+
raise(Exception(f"Note: {inspect.stack()[0][3]}: cannot delete a note that has references to it: from {self.get_refs()}"))
|
| 125 |
+
else:
|
| 126 |
+
self.parentfad.notes.pop(self.id)
|