https://github.com/talamus/python-relatable
An easier way to manipulate tabular data that has complex references.
https://github.com/talamus/python-relatable
data-structures database python
Last synced: 6 months ago
JSON representation
An easier way to manipulate tabular data that has complex references.
- Host: GitHub
- URL: https://github.com/talamus/python-relatable
- Owner: talamus
- License: mit
- Created: 2022-11-07T15:03:29.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-11-11T10:56:04.000Z (over 3 years ago)
- Last Synced: 2025-09-24T13:56:25.028Z (6 months ago)
- Topics: data-structures, database, python
- Language: Python
- Homepage:
- Size: 45.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RelaTable
## A relational database -like table of rows.
Supports foreign key -style referencing to an another `RelaTable` or other containers.
### An Example
```python
from relatable import RelaTable
colors = ["red", "blue", "green"]
pet_names = {"dog": "Musti", "cat": "Mirri"}
persons = RelaTable(
primary_key_column="id",
foreign_keys={"color": colors},
rows=[
{"id": 123, "name": "Jaakko", "color": 0},
{"id": 456, "name": "Teppo", "color": 1},
],
)
# Insert new user in the middle of the table
persons.insert(1, {"id": 789, "name": "Seppo", "color": 2})
pets = RelaTable(
# No primary key defined => index is used
foreign_keys={"name": pet_names, "owner": persons},
rows=[
{"name": "cat", "owner": 123}, # index 0
{"name": "dog", "owner": 456}, # index 1
],
)
print(persons)
# Prints out:
# [{'id': 123, 'name': 'Jaakko', 'color': 'red'},
# {'id': 789, 'name': 'Seppo', 'color': 'green'},
# {'id': 456, 'name': 'Teppo', 'color': 'blue'}]
print(pets)
# Prints out:
# [{'name': 'Mirri', 'owner': {'id': 123, 'name': 'Jaakko', 'color': 'red'}},
# {'name': 'Musti', 'owner': {'id': 456, 'name': 'Teppo', 'color': 'blue'}}]
print(persons[789].name)
# Prints out:
# Seppo
print(pets[0].owner.color)
# Prints out:
# red
print(pets[0].data())
# Prints out the non-expanded row data:
# {'name': 'cat', 'owner': 123}
```