A relational database using python and json
This may actually be better described as a relational interpreter.
This is explaied below in Conceptual overview
mem_seg = newMemorySegment()
person = Noun(mem_seg, "person").addr
bob = Noun(mem_seg, "Bob", person).addr
billy = Noun(mem_seg, "Billy", person).addr
siblings = LinkingVerb(mem_seg, "is siblings").addr
Link(mem_seg, bob, siblings, billy)
write_mem_seg(mem_seg, "test.json")
mem_seg = load_mem_seg("test.json")
The premise is that all systems can be described using objects and relations between those objects.
There are three classes I made to make such descriptions programmatically.
I have given them names from grammar because that was the easiest way for me to summarize it for myself.
Each of these classes needs to be passed a MemorySegment object.
MemorySegment is not designed specifically for this project, and there is a bit of extra setup needed, so use newMemorySegment to get a MemorySegment object to pass to new instances of the classes below.
mem_seg = newMemorySegment()
All instances of the below classes are referenced by their "address" (index) in the MemorySegment.
- Noun - This is the "thing" or "object." Every
Nounhas aname:person = Noun(mem_seg, "person").addr
EachNouncan optionally be aninstance_ofanotherNoun:bob = Noun(mem_seg, "Bob", person).addrbilly = Noun(mem_seg, "Billy", person).addr - LinkingVerb - These are words/phrases that are the types of relations, or "links", that can be made (eg "is a", "are", "owns", "was").
is_a = LinkingVerb(mem_seg, "is a").addr
'siblings = LinkingVerb(mem_seg, "is siblings").addr` - Link - This is what relates
Nounsto each other:Link(mem_seg, bob, siblings, billy)- read as "Bob is siblings with Billy"
When a newNounis initialized, if theinstance_ofparameter is notNone, it creates aLinkbetween itself and theinstance_of``Noun:Link(self.addr, is_a, self.instance_of)
It is possible to create aLinkbetween any of these three classes, although I'm considering removing the ability to do so withLinks.
All instances of the above classes keep a list of the addresses of Links they are involved in in their attribute .links.
MemorySegment(iterable=None, empty_values=None)extendslist.store_obj(object_, addr=None)- returns addr where object was stored in list..delete_obj(add)- removes object at addr from list..check_empty_values()- find skipped addresses in list..search(attr, value)- return list of objects in list with attr equal to value.
Noun(mem, name, instance_of=None, addr=None, links=None).mem.name.instance_of.addr.links
LinkingVerb(mem, name, addr=None, links=None).mem.name.addr.links
Link(mem, thing1, linking_verb, thing2, addr=None, links=None).mem.thing1.linking_verb.thing2.links
- Noun, Link, and LinkingVerb all share:
.__dict__().delete()- use instead ofdel <Noun/LinkingVerb/Link>..add_link(link_addr)- addslink_addrtoself.links. This is done automatically every time a newLinkis created..remove_link(link_addr)- removeslink_addrfromself.links. Called byLink.delete()..list_links()- return list ofLinkobjects inself.links..list_linked()- return list of objects involved in links inself.links.