-
Notifications
You must be signed in to change notification settings - Fork 6
Home
littletable began as an experiment in creating a simple in-memory "database table" as a list of objects. No upfront schema would be required, one would simply query using the attributes of the objects in the table. The philosophy of the littletable API is:
- Tables are iterable just like lists.
- Every Table query returns its results as a new Table.
In addition:
- Tables can contain any user-defined object, namedtuple, or slotted type.
- Tables can contain DataObject's, a special semi-mutable class defined in littletable.
- Tables can be joined.
- Tables can be pivoted.
- Tables can be imported and exported in CSV and JSON format.
- Indexed access to table can be very similar to a dict.
The syntax is simpler to write, and simpler to read. Here is a simple table:
Customer = namedtuple("Customer", "id name")
customers = Table("customers")
customers.create_index("id", unique=True)
customers.insert(Customer(id="0010", name="George Jetson"))
customers.insert(Customer(id="0020", name="Wile E. Coyote"))
customers.insert(Customer(id="0030", name="Jonny Quest"))
Since customers is iterable like a list, one can extract all the customers whose name starts with "Jonny" using a list comprehension:
jonnies = [customer for customer in customers if customer.name.startswith("Jonny")]
Using the Table API, you can query using where():
jonnies = customers.where(lambda customer: customer.name.startswith("Jonny"))
To find all records whose attribute exactly matches a given value, where() will accept keyword arguments:
j = customers.where(id="0030")
Since there is a unique index defined on the id attribute, we can also access a particular customer by their id:
j = customers.by.id["0030"]
In contrast, here would be the pure Python list retrieval, using next():
j = next(customer for customer in customers if customer.id == "0030")
littletable Tables are purely in-memory data structures, very similar to SQLite's :memory database. They are not queryable using SQL, although the Table API is reminiscent in many ways of SQL (insert(), remove(), where(), etc.). They can be easily persisted by exporting their contents to JSON or CSV format files.