pip install -r requirements.txt
python family-tree.py
Usage: family-tree.py [OPTIONS] COMMAND [ARGS]...
Command-line utility for managing a family tree.
Options:
--help Show this message and exit.
Commands:
register-relation Register a new relation type in the family tree.
add-person Add a new person to the family tree.
count Count the number of relations of a certain type for...
get Get the first entity related by a certain type of...
get-all Get all entities related by a certain type of...
relate Establish a relation between two people in the...
$ python family-tree.py register-relation father --reverse son
>> Relation type father registered
$ python family-tree.py register-relation friend --reverse friend
Relation type friend registered
$ python family-tree.py add-person cristiano
Added person: Person(name=cristiano, age=None)
$ python family-tree.py relate --help
Usage: family-tree.py relate [OPTIONS] SUBJECT_NAME RELATION_TYPE
RELATED_PERSON
Establish a relation between two people in the family tree.
Usages:
family-tree relate John mother Alice
family-tree relate John father Bob
Options:
--reverse TEXT Specify the reverse relation type
--help Show this message and exit.
$ python family-tree.py relate haaland father cristiano
Related cristiano as father of haaland
$ python family-tree.py count cristiano son
cristiano's son count: 1
$ python family-tree.py get haaland father
haaland's father: Person(name=cristiano, age=None)
family-tree-cli/
├── family-tree.py
├── person.py
└── relations
├── models
│ ├── base_model.py
│ ├── in_memory_relation_model.py
│ └── json_relation_model.py
├── relation_collection.py
└── store
├── json_object_store.py
└── object_store.py
This has two sub-packages: models
and store
and one module relation_collection.py
- relation_collection.py
- It has
RelationCollection
which manages with globally registered relations
- It has
- models package
- It has
RelationModel
which is the base class for managing relations- An implementation of this meant to be extended by the core data-class (like
Person
) - I've added two implementations of above in this codebase
InMemoryRelationModel
- It manages all the relations in the memory
- All its operation are in O(1) time complexity
JsonRelationModel
- It manages all the relations in the memory + saves it in a local JSON file
- I thought about adding a
SqlRelationModel
as well (which store objects in a Relational-DB like MySQL or Postgres)- It would extend
RelationCollection
and could've easily been a drop-in replacement for either of them
- It would extend
- An implementation of this meant to be extended by the core data-class (like
- It has
- store package
- It has
ObjectStore
which is the base class for persisting data of objects- An implementation of this meant to be mentioned in core data-class's
Meta
(See impl. ofPerson
for details) - I've added one implementations of above in this codebase
JsonObjectStore
- It stores the data in a local JSON
- Again, any new implementation like
MongoDbObjectStore
which extendsObjectStore
can be a drop-in replacement forJsonObjectStore
- An implementation of this meant to be mentioned in core data-class's
- It has
- This contains the core data-class (
Person
) which holds the data for our intended object (people)
- This contains the CLI-handler code