kepler-atlas
基于 SQLAlchemy 的 Python ORM 库,专为交互式编程(Jupyter/IPython)优化,提供更简洁的 API。
特性
- 🚀 懒加载表反射 - 按需加载表结构,启动速度快
- 🔤 大小写不敏感 -
db.users、db.USERS、db.Users都能访问同一张表 - 📊 DataFrame 集成 - 查询结果一键转为 pandas DataFrame
- 🗄️ 多数据库支持 - MySQL、PostgreSQL、SQLite、Oracle、MSSQL 等
- ✨ Tab 补全 - IPython/Jupyter 中自动提示表名
安装
pip install kepler-atlas
快速开始
连接数据库
from kepler.atlas import DataBase
# 方式1: 连接字符串
db = DataBase('mysql://user:pass@localhost/db')
# 方式2: SQLAlchemy Engine
from sqlalchemy import create_engine
engine = create_engine('postgresql://user:pass@localhost/db')
db = DataBase(engine)
# 方式3: 上下文管理器(自动关闭连接)
with DataBase('sqlite:///example.db') as db:
...
访问表
# 大小写不敏感
users = db.users
users = db.USERS
users = db.Users # 都指向同一张表
# 列出所有表
print(db.tables)
查询
# 基本查询
db.query(db.users).all()
# 条件过滤
db.query(db.users).filter(db.users.age > 18).all()
# 返回 DataFrame
df = db.query(db.users).filter(db.users.age > 18).to_df()
# 排序、分页
db.query(db.users).order_by(db.users.id.desc()).limit(10).all()
插入
# 单条插入
user = db.users(name='Alice', email='alice@example.com')
db.update(user)
db.commit()
# 批量插入 - DataFrame
import pandas as pd
df = pd.DataFrame({
'name': ['Alice', 'Bob'],
'email': ['alice@example.com', 'bob@example.com']
})
db.insert(db.users, df)
db.commit()
# 批量插入 - 字典列表
data = [{'name': 'Alice'}, {'name': 'Bob'}]
db.insert(db.users, data, ignore=True) # ignore=True 忽略重复键
db.commit()
更新
# 查询后修改
user = db.query(db.users).filter(db.users.name == 'Alice').first()
user.email = 'new@email.com'
db.commit()
# 批量更新
db.query(db.users).filter(db.users.age < 18).update({'status': 'minor'})
db.commit()
# merge(存在则更新,不存在则插入)
user = db.users(name='Alice', email='new@email.com')
db.merge(user)
db.commit()
删除
# 删除单条
user = db.query(db.users).filter(db.users.name == 'Alice').first()
db.delete(user)
db.commit()
# 批量删除
db.query(db.users).filter(db.users.status == 'inactive').delete()
db.commit()
自定义模型
from sqlalchemy import Column, Integer, String
class User(db.Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String(50))
email = Column(String(100))
db.create_all() # 创建表
API 参考
DataBase
| 方法/属性 | 说明 |
|---|---|
DataBase(bind, schema=None) |
创建数据库实例 |
db.tables |
返回所有表名列表 |
db.Base |
获取 automap 基类 |
db.query(*entities) |
创建查询 |
db.insert(table, data, ignore=True) |
批量插入 |
db.update(obj) |
添加对象到会话 |
db.merge(obj) |
合并对象 |
db.delete(obj) |
删除对象 |
db.commit() |
提交事务 |
db.rollback() |
回滚事务 |
db.flush() |
刷新到数据库(不提交) |
db.close() |
关闭连接 |
db.refresh() |
刷新元数据 |
Query
| 方法 | 说明 |
|---|---|
.filter(condition) |
过滤条件 |
.order_by(column) |
排序 |
.limit(n) |
限制数量 |
.offset(n) |
偏移量 |
.all() |
返回所有结果 |
.first() |
返回第一条 |
.one() |
返回唯一一条 |
.count() |
计数 |
.to_df() |
转为 DataFrame |
异常
from kepler.atlas import (
AtlasError, # 基础异常
TableError, # 表操作错误
ConnectionError, # 连接错误
InsertError, # 插入错误
ValidationError, # 验证错误
)
交互式编程
在 IPython/Jupyter 中,Tab 补全会自动显示表名:
db = DataBase('sqlite:///example.db')
db.u<Tab> # 显示: db.users
列名也同样支持:
数据库方言支持
| 数据库 | 忽略重复键方式 |
|---|---|
| MySQL | INSERT IGNORE |
| SQLite | INSERT OR REPLACE |
| PostgreSQL | ON CONFLICT DO NOTHING |
| Oracle | /*+ IGNORE_ROW_ON_DUPKEY_INDEX */ |
| MSSQL | IGNORE_DUP_KEY |
注意事项
- ⚠️ 表必须有主键才能被 ORM 映射
License
GPL-3.0
Metadata
Release files for kepler.atlas 0.1.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| kepler_atlas-0.1.6.tar.gz | 27.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| kepler_atlas-0.1.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 53.9 kB
Release files / kepler_atlas-0.1.6.tar.gz
| Download URL | kepler_atlas-0.1.6.tar.gz |
|---|---|
| Size | 27.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b3a59239f760cd5fac9b23cb9e93f833b867d269e0b2f0c7a365a31307cf205a
|
|
BLAKE2b-256 checksum How to use checksums |
eca5d774044e47fa14a451081aa7f20db367d8f0ee85618fee1fef4bb8c54199
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Release files / kepler_atlas-0.1.6-py3-none-any.whl
| Download URL | kepler_atlas-0.1.6-py3-none-any.whl |
|---|---|
| Size | 26.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0db8408848401e94a6619c268844873e051775e008f7442a60f890e5c4c09f0e
|
|
BLAKE2b-256 checksum How to use checksums |
ab40fbe8dadd688443994642131ba8266a68f28e2b1aa4671f0c3037091e1490
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Security for kepler.atlas 0.1.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.