Spaces:
Runtime error
Runtime error
Download note.py from PrecisionContent/DU: direct link, hf CLI and curl.
- Browser
- Download file 5.92 kB
-
https://huggingface.co/spaces/PrecisionContent/DU/resolve/main/note.py
- Command line
-
hf download hf://spaces/PrecisionContent/DU/note.py
-
curl -L -o note.py https://huggingface.co/spaces/PrecisionContent/DU/resolve/main/note.py
5.92 kB
| import time | |
| import inspect | |
| class Note: | |
| def __init__(self, parentfad, id, typeid, value=None) -> None: | |
| self.id = id | |
| self.timestamp = time.time() | |
| self.typeid = typeid | |
| #print(f"constructor: {self.type()} id {id} timestamp {self.timestamp}") | |
| self.parentfad = parentfad | |
| self.properties = {} | |
| if value is not None: | |
| self.properties["value"] = value | |
| def type(self): | |
| return notetypes.inverse[self.typeid][0] | |
| def get(self, name=None): | |
| propertyname = name if name is not None else "value" | |
| if propertyname in self.properties.keys(): | |
| value = self.properties[propertyname] # "value" is default property | |
| else: | |
| value = None | |
| if isinstance(value, int): # any int value is supposed to be a ref to id; if you need to store numbers, use floats | |
| 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 | |
| elif isinstance(value, list): | |
| 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 | |
| else: | |
| return value | |
| def get_string(self, name=None): | |
| value = self.get(name) | |
| if value == None: | |
| return '' | |
| elif isinstance(value, list): | |
| return ", ".join(value) | |
| else: | |
| return value | |
| def set(self, value): | |
| if isinstance(value, int): # any int value is supposed to be a ref to id | |
| assert(value in self.parentfad.notes.keys()) # so we ensure this id exists | |
| self.properties["value"] = value | |
| self.timestamp = time.time() | |
| #print(f"set value: {self.type()} id {id} timestamp {self.timestamp}") | |
| def set_property(self, name, value): | |
| if isinstance(value, int): # any int value is supposed to be a ref to id | |
| assert(value in self.parentfad.notes.keys()) # so we ensure this id exists | |
| self.properties[name] = value | |
| self.timestamp = time.time() | |
| #print(f"set name value: {self.type()} id {id} timestamp {self.timestamp}") | |
| def set_property_newnote(self, name, value, valuetype): | |
| existing = self.parentfad.get_by_type_and_value(valuetype, value) | |
| if existing: | |
| note = existing[0] # we assume there's only one | |
| else: | |
| note = self.parentfad.newnote(valuetype, value) | |
| self.properties[name] = note.id | |
| self.timestamp = time.time() | |
| #print(f"set name value: {self.type()} id {id} timestamp {self.timestamp}") | |
| return note | |
| def add_to_property(self, name, value): | |
| if isinstance(value, int): # any int value is supposed to be a ref to id | |
| if value not in self.parentfad.notes.keys(): # so we ensure this id exists | |
| raise(Exception(f"Note: {inspect.stack()[0][3]}: trying to add an id ({value}) which does not point to a Note")) | |
| elif value == None: | |
| raise(Exception(f"Note: {inspect.stack()[0][3]}: trying to add a None value")) | |
| if not name in self.properties.keys() or self.properties[name] is None: # no such property yet, or it's None: create list | |
| self.properties[name] = [value] | |
| elif isinstance(self.properties[name], list): # already a list | |
| self.properties[name].append(value) | |
| else: # not a list nor None, create list from the existing value and new one | |
| self.properties[name] = [self.properties[name], value] | |
| self.timestamp = time.time() | |
| #print(f"add: {self.type()} id {id} timestamp {self.timestamp}") | |
| def add_to_property_newnote(self, name, value, valuetype): | |
| if value == None: | |
| raise(Exception(f"Note: {inspect.stack()[0][3]}: trying to add a None value")) | |
| existing = self.parentfad.get_by_type_and_value(valuetype, value) | |
| if existing: | |
| note = existing[0] # we assume there's only one | |
| else: | |
| note = self.parentfad.newnote(valuetype, value) | |
| if not name in self.properties.keys() or self.properties[name] is None: # no such property yet, or it's None: create list | |
| self.properties[name] = [note.id] | |
| elif isinstance(self.properties[name], list): # already a list | |
| self.properties[name].append(note.id) | |
| else: # not a list nor None, create list from the existing value and new one | |
| self.properties[name] = [self.properties[name], note.id] | |
| self.timestamp = time.time() | |
| return note | |
| def get_refs(self): | |
| refs = [] | |
| for key, note in self.parentfad.notes.items(): | |
| for pkey, pvalue in note.properties.items(): | |
| if isinstance(pvalue, int): | |
| if pvalue == self.id: | |
| refs.append(key) | |
| return refs | |
| def touch(self): | |
| self.timestamp = time.time() | |
| def is_newer(self, target: 'Note'): # type: ignore | |
| return self.timestamp >= target.timestamp | |
| def needs_updating_from(self): | |
| refs_to_update = [] | |
| for key, value in self.properties.items(): | |
| if isinstance(value, int): | |
| if not(self.is_newer(self.parentfad.notes[value])): | |
| 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}") | |
| refs_to_update.append(value) | |
| return refs_to_update | |
| def delete(self): | |
| if self.get_refs(): | |
| raise(Exception(f"Note: {inspect.stack()[0][3]}: cannot delete a note that has references to it: from {self.get_refs()}")) | |
| else: | |
| self.parentfad.notes.pop(self.id) | |