Package: littletable
littletable - a Python module to give ORM-like access to a collection of objectsIntroductionImporting data from CSV filesTabular outputFor More InfoSample DemoIntroductionThelittletablemodule provides a low-overhead, schema-less, in-memory database access to a collection
of user objects.littletableTables will accept Pythondicts or any user-defined object type, including:namedtuplesandtyping.NamedTuplesdataclassestypes.SimpleNamespacesattrsclassesPyDanticdata modelstraitletslittletableinfers the Table's "columns" from those objects'__dict__,__slots__, or_fieldsmappings to access
object attributes.If populated with Pythondicts, they get stored asSimpleNamespaces orlittletable.DictObjects.In addition to basic ORM-style insert/remove/query/delete access to the contents of aTable,littletableoffers:simple indexing for improved retrieval performance, and optional enforcing key uniquenessaccess to objects using indexed attributesdirect import/export to CSV and Excel .xlsx filesclean tabular output for data presentationsimplified joins using"+"operator syntax between annotatedTablesthe result of any query or join is a new first-classlittletableTablesimple full-text search against multi-word text attributesaccess like a standard Python list to the records in a Table, including indexing/slicing,iter,zip,len,groupby, etc.access like a standard Pythondictto attributes with a unique index, or like a standard Pythondefaultdict(list)to attributes with a non-unique indexlittletableTables do not require an upfront schema definition, but simply work off of the attributes in
the stored values, and those referenced in any query parameters.Importing data from CSV filesYou can easily import a CSV file into a Table using Table.csv_import():t=Table().csv_import("my_data.csv")In place of a local file name, you can also specify  an HTTP url:url="https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv"names=["sepal-length","sepal-width","petal-length","petal-width","class"]iris_table=Table('iris').csv_import(url,fieldnames=names)You can also directly import CSV data as a string:catalog=Table("catalog")catalog_data="""\sku,description,unitofmeas,unitpriceBRDSD-001,Bird seed,LB,3BBS-001,Steel BB's,LB,5MGNT-001,Magnet,EA,8"""catalog.csv_import(catalog_data,transforms={'unitprice':int})Data can also be directly imported from compressed .zip, .gz, and .xz files.Files containing JSON-formatted records can be similarly imported usingTable.json_import().Tabular outputTo produce a nice tabular output for a table, you can use the embedded support for
therichmodule,as_html()inJupyter Notebook,
or thetabulatemodule:Usingtable.present()(implemented usingrich;present()acceptsrichTablekeyword args):table(title_str).present(fields=["col1","col2","col3"])ortable.select("col1 col2 col3")(title_str).present(caption="caption text",caption_justify="right")UsingJupyter Notebook:fromIPython.displayimportHTML,displaydisplay(HTML(table.as_html()))Usingtabulate:fromtabulateimporttabulateprint(tabulate((vars(rec)forrecintable),headers="keys"))For More InfoExtended "getting started" notes athow_to_use_littletable.md.Sample DemoHere is a simple littletable data storage/retrieval example:fromlittletableimportTablecustomers=Table('customers')customers.create_index("id",unique=True)customers.csv_import("""\id,name0010,George Jetson0020,Wile E. Coyote0030,Jonny Quest""")catalog=Table('catalog')catalog.create_index("sku",unique=True)catalog.insert({"sku":"ANVIL-001","descr":"1000lb anvil","unitofmeas":"EA","unitprice":100})catalog.insert({"sku":"BRDSD-001","descr":"Bird seed","unitofmeas":"LB","unitprice":3})catalog.insert({"sku":"MAGNT-001","descr":"Magnet","unitofmeas":"EA","unitprice":8})catalog.insert({"sku":"MAGLS-001","descr":"Magnifying glass","unitofmeas":"EA","unitprice":12})wishitems=Table('wishitems')wishitems.create_index("custid")wishitems.create_index("sku")# easy to import CSV data from a string or filewishitems.csv_import("""\custid,sku0020,ANVIL-0010020,BRDSD-0010020,MAGNT-0010030,MAGNT-0010030,MAGLS-001""")# print a particular customer name# (unique indexes will return a single item; non-unique# indexes will return a list of all matching items)print(customers.by.id["0030"].name)# see all customer namesfornameincustomers.all.name:print(name)# print all items sold by the poundforitemincatalog.where(unitofmeas="LB"):print(item.sku,item.descr)# print all items that cost more than 10foritemincatalog.where(lambdao:o.unitprice>10):print(item.sku,item.descr,item.unitprice)# join tables to create queryable wishlists collectionwishlists=customers.join_on("id")+wishitems.join_on("custid")+catalog.join_on("sku")# print all wishlist items with price > 10 (can use Table.gt comparator instead of lambda)bigticketitems=wishlists().where(unitprice=Table.gt(10))foriteminbigticketitems:print(item)# list all wishlist items in descending order by priceforiteminwishlists().sort("unitprice desc"):print(item)# print output as a nicely-formatted tablewishlists().sort("unitprice desc")("Wishlists").present()# print output as an HTML tableprint(wishlists().sort("unitprice desc")("Wishlists").as_html())# print output as a Markdown tableprint(wishlists().sort("unitprice desc")("Wishlists").as_markdown())