跳转到主要内容

Python内存ORM数据库

项目描述

littletable - 一个Python模块,用于为对象集合提供类似ORM的访问方式

Build Status Binder

简介

littletable模块提供了一个低开销、无模式的内存数据库访问方式,用于用户对象的集合。littletable表格将接受Python dict或任何用户定义的对象类型,包括

  • namedtuplestyping.NamedTuples
  • dataclasses
  • types.SimpleNamespaces
  • attrs
  • PyDantic数据模型
  • traitlets

littletable从对象的__dict____slots___fields映射中推断出表的“列”,以访问对象属性。

如果填充了Python dict,它们将被存储为SimpleNamespace

除了基本的ORM风格插入/删除/查询/删除访问表的内部内容外,littletable还提供

  • 简单的索引以提高检索性能,以及可选的强制唯一键
  • 通过索引属性访问对象
  • 直接导入/导出到CSV、TSV、JSON和Excel .xlsx文件
  • 干净的表格输出以呈现数据
  • 使用“+”运算符语法在注解的Table之间进行简化连接
  • 任何查询或连接的结果都是一个新的第一类littletable Table
  • 对多词文本属性进行简单全文搜索
  • 像标准Python列表一样访问Table中的记录,包括索引/切片、iterziplengroupby等。
  • 像标准Python dict一样访问具有唯一索引的属性,或像标准Python defaultdict(list)一样访问具有非唯一索引的属性

littletable Table不需要预先定义架构,只需根据存储的值和任何查询参数中引用的属性进行工作。

可选依赖

基础littletable代码除了Python stdlib之外没有其他依赖。但是,一些操作需要额外的包安装

操作 需要额外安装
Table.present 丰富的
Table.excel_import/export openpyxl(加defusedxmllxml,推荐defusedxml
Table.as_dataframe pandas

从CSV文件导入数据

您可以使用Table.csv_import()轻松将CSV文件导入到Table

import littletable as lt
t = lt.Table().csv_import("my_data.csv")
# or
t = lt.csv_import("my_data.csv")

除了本地文件名外,还可以指定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)

您还可以直接将CSV数据作为字符串导入

catalog = Table("catalog")

catalog_data = """\
sku,description,unitofmeas,unitprice
BRDSD-001,Bird seed,LB,3
BBS-001,Steel BB's,LB,5
MGNT-001,Magnet,EA,8"""

catalog.csv_import(catalog_data, transforms={'unitprice': int})

数据也可以直接从压缩的.zip、.gz和.xz文件中导入。

包含JSON格式记录的文件可以类似地使用json_import()导入。

表格输出

要为表生成漂亮的表格输出,您可以使用嵌入的rich模块支持,在Jupyter Notebook中使用as_html()或使用tabulate模块

使用table.present()(使用rich实现;present()接受rich Table关键字参数)

table(title_str).present(fields=["col1", "col2", "col3"])
    or
table.select("col1 col2 col3")(title_str).present(caption="caption text", 
                                                  caption_justify="right")

使用Jupyter Notebook

from IPython.display import HTML, display
display(HTML(table.as_html()))

使用tabulate

from tabulate import tabulate
print(tabulate((vars(rec) for rec in table), headers="keys"))

更多信息

有关如何使用littletable的扩展“入门”说明,请参阅how_to_use_littletable.md

示例演示

以下是一个简单的littletable数据存储/检索示例

from littletable import Table

customers = Table('customers')
customers.create_index("id", unique=True)
customers.csv_import("""\
id,name
0010,George Jetson
0020,Wile E. Coyote
0030,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 file
wishitems.csv_import("""\
custid,sku
0020,ANVIL-001
0020,BRDSD-001
0020,MAGNT-001
0030,MAGNT-001
0030,MAGLS-001
""")

# print a particular customer name
# (unique indexes will return a single item; non-unique
# indexes will return a new Table of all matching items)
print(customers.by.id["0030"].name)

# see all customer names
for name in customers.all.name:
    print(name)

# print all items sold by the pound
for item in catalog.where(unitofmeas="LB"):
    print(item.sku, item.descr)

# print all items that cost more than 10
for item in catalog.where(lambda o: o.unitprice > 10):
    print(item.sku, item.descr, item.unitprice)

# join tables to create queryable wishlists collection
wishlists = 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))
for item in bigticketitems:
    print(item)

# list all wishlist items in descending order by price
for item in wishlists().sort("unitprice desc"):
    print(item)

# print output as a nicely-formatted table
wishlists().sort("unitprice desc")("Wishlists").present()

# print output as an HTML table
print(wishlists().sort("unitprice desc")("Wishlists").as_html())

# print output as a Markdown table
print(wishlists().sort("unitprice desc")("Wishlists").as_markdown())

项目详情


下载文件

下载您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

littletable-3.0.1.tar.gz (85.2 KB 查看哈希值

构建分布

littletable-3.0.1-py3-none-any.whl (47.5 KB 查看哈希值

上传于 Python 3

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面