Spaces:
Runtime error
Runtime error
Dmitry Kirsanov commited on
Commit ·
2f690fb
1
Parent(s): 5530b65
cleanup, separate methods for property as string, more tests
Browse files
fad.py
CHANGED
|
@@ -20,15 +20,17 @@ notetypes = bidict.bidict({
|
|
| 20 |
|
| 21 |
idlength = 8
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
class Fad:
|
| 25 |
|
| 26 |
notes = {}
|
| 27 |
|
| 28 |
-
pattern_marks = re.compile("[⌦⌫⌧]")
|
| 29 |
-
|
| 30 |
highest_note_id = 0
|
| 31 |
|
|
|
|
| 32 |
class Note:
|
| 33 |
def __init__(self, parentfad, id, typeid, value=None) -> None:
|
| 34 |
self.id = id
|
|
@@ -45,11 +47,24 @@ class Fad:
|
|
| 45 |
return notetypes.inverse[self.typeid][0]
|
| 46 |
|
| 47 |
def get(self, name=None):
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
if isinstance(value, int): # any int value is supposed to be a ref to id; if you need to store numbers, use floats
|
| 50 |
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
|
| 51 |
elif isinstance(value, list):
|
| 52 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
else:
|
| 54 |
return value
|
| 55 |
|
|
@@ -82,7 +97,6 @@ class Fad:
|
|
| 82 |
raise(Exception(f"Note: add: trying to add to list (properties[{name}]) but the existing value is not a list"))
|
| 83 |
#print(f"add: {self.type()} id {id} timestamp {self.timestamp}")
|
| 84 |
|
| 85 |
-
|
| 86 |
def get_refs(self):
|
| 87 |
refs = []
|
| 88 |
for key, note in self.parentfad.notes.items():
|
|
@@ -109,7 +123,8 @@ class Fad:
|
|
| 109 |
raise(Exception(f"Note: cannot delete a note that has references to it: from {self.get_refs()}"))
|
| 110 |
else:
|
| 111 |
self.parentfad.notes.pop(self.id)
|
| 112 |
-
|
|
|
|
| 113 |
|
| 114 |
def newnote(self, type, value=None):
|
| 115 |
id = self.highest_note_id + 1
|
|
@@ -122,13 +137,14 @@ class Fad:
|
|
| 122 |
for value in values:
|
| 123 |
self.newnote(type, value)
|
| 124 |
|
|
|
|
| 125 |
def get_notes_by_type(self, valuetype):
|
| 126 |
ids = []
|
| 127 |
for id, note in self.notes.items():
|
| 128 |
if note.typeid == notetypes[valuetype]:
|
| 129 |
ids.append(id)
|
| 130 |
return ids
|
| 131 |
-
|
| 132 |
def delete_notes_by_type(self, valuetype):
|
| 133 |
ids = self.get_notes_by_type(valuetype)
|
| 134 |
for id in ids:
|
|
@@ -138,12 +154,17 @@ class Fad:
|
|
| 138 |
values = []
|
| 139 |
for id, note in self.notes.items():
|
| 140 |
if note.typeid == notetypes[valuetype]:
|
| 141 |
-
values.append(note.
|
| 142 |
return values
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
def get_note_by_value(self, value, valuetype):
|
| 145 |
for id, note in self.notes.items():
|
| 146 |
-
if note.typeid == notetypes[valuetype] and note.
|
| 147 |
return id
|
| 148 |
return None
|
| 149 |
|
|
@@ -151,14 +172,14 @@ class Fad:
|
|
| 151 |
ret = []
|
| 152 |
for id, m in self.notes.items():
|
| 153 |
if notetypes[type] == m.typeid:
|
| 154 |
-
if value == m.
|
| 155 |
ret.append(id)
|
| 156 |
return ret
|
| 157 |
|
| 158 |
|
| 159 |
def get_next_mark(self, fad, start=0):
|
| 160 |
tstart = start
|
| 161 |
-
new =
|
| 162 |
if new != None:
|
| 163 |
tend = new.start()
|
| 164 |
id = int(fad[tend + 1:tend + idlength + 1])
|
|
@@ -184,7 +205,7 @@ class Fad:
|
|
| 184 |
break
|
| 185 |
if self.notes[id].typeid == notetypes[notetype]:
|
| 186 |
id_s = f' id="{id}"' if show_id else ''
|
| 187 |
-
properties_s = ''.join([(f' {name}="{str(self.notes[id].
|
| 188 |
if markchar == '⌧':
|
| 189 |
newfad = newfad[:newi - idlength - 1] + f'<{notetype}{id_s}{properties_s}/>' + newfad[newi:]
|
| 190 |
else:
|
|
@@ -192,7 +213,7 @@ class Fad:
|
|
| 192 |
i = newi
|
| 193 |
return self.get_plaintext(newfad)
|
| 194 |
|
| 195 |
-
def
|
| 196 |
i = 0
|
| 197 |
spans = []
|
| 198 |
while True:
|
|
@@ -203,10 +224,25 @@ class Fad:
|
|
| 203 |
close_i = fad.find(self.closemark(id), newi)
|
| 204 |
if close_i != -1:
|
| 205 |
spans.append(self.get_plaintext(fad[newi:close_i]))
|
| 206 |
-
newi = close_i
|
| 207 |
i = newi
|
| 208 |
return spans
|
| 209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
|
| 212 |
|
|
@@ -360,11 +396,18 @@ if __name__ == "__main__":
|
|
| 360 |
|
| 361 |
f.set_property_to_mark(fad, "span", 2, "audiences", None, "audience")
|
| 362 |
assert(f.get_plaintext_with_markup(fad, "span", show_id=True, show_properties=True) == '<span id="4" actors="actor1" audiences="audience1, audience2, audience3">A1234<span id="6" actors="actor1">56789</span id="4">B123456789<span id="5">C1234</span id="6">56789</span id="5">')
|
|
|
|
|
|
|
| 363 |
assert(f.get_plaintext_with_markup(fad, "span", show_id=True, show_properties=True, properties_to_show=['actors']) == '<span id="4" actors="actor1">A1234<span id="6" actors="actor1">56789</span id="4">B123456789<span id="5">C1234</span id="6">56789</span id="5">')
|
| 364 |
|
| 365 |
-
f.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
|
| 367 |
-
assert(f.get_spans_by_property_value(fad, "span",
|
| 368 |
|
| 369 |
for id, note in f.notes.items():
|
| 370 |
assert(note.needs_updating_from() == []) # in normal operation, there should be no contratemporal newer -> older refs
|
|
|
|
| 20 |
|
| 21 |
idlength = 8
|
| 22 |
|
| 23 |
+
pattern_marks = re.compile("[⌦⌫⌧]")
|
| 24 |
+
|
| 25 |
+
|
| 26 |
|
| 27 |
class Fad:
|
| 28 |
|
| 29 |
notes = {}
|
| 30 |
|
|
|
|
|
|
|
| 31 |
highest_note_id = 0
|
| 32 |
|
| 33 |
+
|
| 34 |
class Note:
|
| 35 |
def __init__(self, parentfad, id, typeid, value=None) -> None:
|
| 36 |
self.id = id
|
|
|
|
| 47 |
return notetypes.inverse[self.typeid][0]
|
| 48 |
|
| 49 |
def get(self, name=None):
|
| 50 |
+
propertyname = name if name is not None else "value"
|
| 51 |
+
if propertyname in self.properties.keys():
|
| 52 |
+
value = self.properties[propertyname] # "value" is default property
|
| 53 |
+
else:
|
| 54 |
+
value = None
|
| 55 |
if isinstance(value, int): # any int value is supposed to be a ref to id; if you need to store numbers, use floats
|
| 56 |
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
|
| 57 |
elif isinstance(value, list):
|
| 58 |
+
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
|
| 59 |
+
else:
|
| 60 |
+
return value
|
| 61 |
+
|
| 62 |
+
def get_string(self, name=None):
|
| 63 |
+
value = self.get(name)
|
| 64 |
+
if value == None:
|
| 65 |
+
return ''
|
| 66 |
+
elif isinstance(value, list):
|
| 67 |
+
return ", ".join(value)
|
| 68 |
else:
|
| 69 |
return value
|
| 70 |
|
|
|
|
| 97 |
raise(Exception(f"Note: add: trying to add to list (properties[{name}]) but the existing value is not a list"))
|
| 98 |
#print(f"add: {self.type()} id {id} timestamp {self.timestamp}")
|
| 99 |
|
|
|
|
| 100 |
def get_refs(self):
|
| 101 |
refs = []
|
| 102 |
for key, note in self.parentfad.notes.items():
|
|
|
|
| 123 |
raise(Exception(f"Note: cannot delete a note that has references to it: from {self.get_refs()}"))
|
| 124 |
else:
|
| 125 |
self.parentfad.notes.pop(self.id)
|
| 126 |
+
|
| 127 |
+
|
| 128 |
|
| 129 |
def newnote(self, type, value=None):
|
| 130 |
id = self.highest_note_id + 1
|
|
|
|
| 137 |
for value in values:
|
| 138 |
self.newnote(type, value)
|
| 139 |
|
| 140 |
+
|
| 141 |
def get_notes_by_type(self, valuetype):
|
| 142 |
ids = []
|
| 143 |
for id, note in self.notes.items():
|
| 144 |
if note.typeid == notetypes[valuetype]:
|
| 145 |
ids.append(id)
|
| 146 |
return ids
|
| 147 |
+
|
| 148 |
def delete_notes_by_type(self, valuetype):
|
| 149 |
ids = self.get_notes_by_type(valuetype)
|
| 150 |
for id in ids:
|
|
|
|
| 154 |
values = []
|
| 155 |
for id, note in self.notes.items():
|
| 156 |
if note.typeid == notetypes[valuetype]:
|
| 157 |
+
values.append(note.get())
|
| 158 |
return values
|
| 159 |
|
| 160 |
+
def get_value_by_type(self, valuetype): # if we know there's only one such note
|
| 161 |
+
for id, note in self.notes.items():
|
| 162 |
+
if note.typeid == notetypes[valuetype]:
|
| 163 |
+
return note.get()
|
| 164 |
+
|
| 165 |
def get_note_by_value(self, value, valuetype):
|
| 166 |
for id, note in self.notes.items():
|
| 167 |
+
if note.typeid == notetypes[valuetype] and note.get() == value:
|
| 168 |
return id
|
| 169 |
return None
|
| 170 |
|
|
|
|
| 172 |
ret = []
|
| 173 |
for id, m in self.notes.items():
|
| 174 |
if notetypes[type] == m.typeid:
|
| 175 |
+
if value == m.get():
|
| 176 |
ret.append(id)
|
| 177 |
return ret
|
| 178 |
|
| 179 |
|
| 180 |
def get_next_mark(self, fad, start=0):
|
| 181 |
tstart = start
|
| 182 |
+
new = pattern_marks.search(fad, start)
|
| 183 |
if new != None:
|
| 184 |
tend = new.start()
|
| 185 |
id = int(fad[tend + 1:tend + idlength + 1])
|
|
|
|
| 205 |
break
|
| 206 |
if self.notes[id].typeid == notetypes[notetype]:
|
| 207 |
id_s = f' id="{id}"' if show_id else ''
|
| 208 |
+
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
|
| 209 |
if markchar == '⌧':
|
| 210 |
newfad = newfad[:newi - idlength - 1] + f'<{notetype}{id_s}{properties_s}/>' + newfad[newi:]
|
| 211 |
else:
|
|
|
|
| 213 |
i = newi
|
| 214 |
return self.get_plaintext(newfad)
|
| 215 |
|
| 216 |
+
def get_spans_by_note_type(self, fad, notetype):
|
| 217 |
i = 0
|
| 218 |
spans = []
|
| 219 |
while True:
|
|
|
|
| 224 |
close_i = fad.find(self.closemark(id), newi)
|
| 225 |
if close_i != -1:
|
| 226 |
spans.append(self.get_plaintext(fad[newi:close_i]))
|
|
|
|
| 227 |
i = newi
|
| 228 |
return spans
|
| 229 |
|
| 230 |
+
def get_spans_by_property_values(self, fad, notetype, propertyname, propertyvalues):
|
| 231 |
+
i = 0
|
| 232 |
+
spans = []
|
| 233 |
+
while True:
|
| 234 |
+
text, markchar, id, newi = self.get_next_mark(fad, i)
|
| 235 |
+
if newi == None:
|
| 236 |
+
break
|
| 237 |
+
if self.notes[id].typeid == notetypes[notetype] and markchar == '⌦' and propertyname in self.notes[id].properties.keys() and set(self.notes[id].get(propertyname)).intersection(set(propertyvalues)):
|
| 238 |
+
close_i = fad.find(self.closemark(id), newi)
|
| 239 |
+
if close_i != -1:
|
| 240 |
+
spans.append(self.get_plaintext(fad[newi:close_i]))
|
| 241 |
+
i = newi
|
| 242 |
+
return spans
|
| 243 |
+
|
| 244 |
+
def get_spans_by_property_value(self, fad, notetype, proprtyname, propertyvalue): # when we only need to check one value
|
| 245 |
+
return self.get_spans_by_property_values(fad, notetype, proprtyname, [propertyvalue])
|
| 246 |
|
| 247 |
|
| 248 |
|
|
|
|
| 396 |
|
| 397 |
f.set_property_to_mark(fad, "span", 2, "audiences", None, "audience")
|
| 398 |
assert(f.get_plaintext_with_markup(fad, "span", show_id=True, show_properties=True) == '<span id="4" actors="actor1" audiences="audience1, audience2, audience3">A1234<span id="6" actors="actor1">56789</span id="4">B123456789<span id="5">C1234</span id="6">56789</span id="5">')
|
| 399 |
+
|
| 400 |
+
f.add_property_to_mark(fad, "span", 3, "audiences", "audience3", "audience")
|
| 401 |
assert(f.get_plaintext_with_markup(fad, "span", show_id=True, show_properties=True, properties_to_show=['actors']) == '<span id="4" actors="actor1">A1234<span id="6" actors="actor1">56789</span id="4">B123456789<span id="5">C1234</span id="6">56789</span id="5">')
|
| 402 |
|
| 403 |
+
f.add_property_to_mark(fad, "span", 2, "audiences", "audience2", "audience")
|
| 404 |
+
assert(f.get_plaintext_with_markup(fad, "span", show_id=True, show_properties=True, properties_to_show=['audiences']) == '<span id="4" audiences="audience1, audience2, audience3">A1234<span id="6" audiences="audience2">56789</span id="4">B123456789<span id="5" audiences="audience3">C1234</span id="6">56789</span id="5">')
|
| 405 |
+
|
| 406 |
+
assert(f.get_spans_by_note_type(fad, "span") == ['A123456789', '56789B123456789C1234', 'C123456789'])
|
| 407 |
+
|
| 408 |
+
assert(f.get_spans_by_property_values(fad, "span", propertyname="audiences", propertyvalues=['audience1', 'audience3']) == ['A123456789', 'C123456789'])
|
| 409 |
|
| 410 |
+
assert(f.get_spans_by_property_value(fad, "span", "audiences", 'audience2') == ['A123456789', '56789B123456789C1234'])
|
| 411 |
|
| 412 |
for id, note in f.notes.items():
|
| 413 |
assert(note.needs_updating_from() == []) # in normal operation, there should be no contratemporal newer -> older refs
|