Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from nanotable import Table, UniqueIndex, SortedMultiIndex
- from datetime import datetime
- from dataclasses import dataclass
- from typing import Any, Protocol
- @dataclass # Doesn't have to be a dataclass. Any objects and mappings are supported
- class Contact:
- name: str
- phone: str
- date_added: datetime
- # Completely optional, but results in full IDE support
- class Indexes(Protocol):
- name: UniqueIndex[Contact, str]
- phone: UniqueIndex[Contact, str]
- date_added: SortedMultiIndex[Contact, datetime]
- table = Table[Contact, Indexes, UniqueIndex[Contact, str]](of_objects=True).primary_index_on("name")\
- .index_on("phone", kind=UniqueIndex, required=False)\
- .index_on("date_added", kind=SortedMultiIndex, required=True)
- table.add(Contact("John Doe", "0123456789", datetime.now()))
- table.add(Contact("John Smith", "9876543210", datetime.now()))
- table.at["John Doe"]
- table.by.name["John Doe"]
- # Range queries on sorted indexes
- table.by.date_added[datetime(2026, 6, 4):datetime.now()]
- guy = table.by.phone["9876543210"]
- with table.rekey(guy):
- guy.name += " (owes me money!)"
- # Indexes are automatically updated
Advertisement
Add Comment
Please, Sign In to add comment