relations is a simple Python implementation of a relational algebra engine.
Create a relation with a heading (i.e. a list of field names):
>>> import relations
>>> employees = relations.Relation('employee_name', 'dept_name')
< Relation('dept_name', 'employee_name') >
The fields are re-ordered alphabetically, so that the order of values in tuples is consistent between equivalent but separate relations. Add tuples to the relation:
>>> alice = employees.add(employee_name='Alice', dept_name='Finance')
>>> bob = employees.add(employee_name='Bob', dept_name='Sales')
A relation is a set; duplicate tuples are considered identical:
>>> _ = employees.add(employee_name='Alice', dept_name='Finance')
A relation implements the relational algebra, including the unary operators Select:
>>> finance_emps = employees.select(lambda emp: emp.dept_name == 'Finance')
>>> names = employees.project('employee_name')
>>> employees_renamed = employees.rename(name='employee_name')
It also supports the set operations Union, Intersection and Difference.