Package: cheetah-orm
Cheetah-ORMA lightweight and high-performance object relational mappper written in pure Python.Installationpip install cheetah_ormNote: To use MySQL or MariaDB you will need to install the "pymysql" package as well. And
to use PostgreSQL, you will need to install the "psycopg" package too. SQLite support requires no
additonal packages.Buildingclone this repoinstall the "wheel" package by executingpip install wheelfrom the repo folder, executepip wheel .there will be a wheel file inside the "dist" folderFeaturessupport for sqlite3, MariaDB/MySQL, and PostgreSQLhigh-level database-neutral API written in pure Pythonautomatically generates the needed SQL statements for whichever database system you prefer to
userich filtering API supports any conditional statement that is supported by the underlying database
system plus sorting, offsets, and limitsresult set countingUsage#!/usr/bin/python3"""Cheetah ORM - Demo"""fromdatetimeimportdatetimeimportosimportunittestfromcheetah_orm.fieldsimport(BigIntField,BlobField,DateTimeField,DoubleField,IntField,FloatField,PasswordField,StringField)fromcheetah_orm.indexesimportForeignKey,Index,UniqueIndexfromcheetah_orm.mappersimportSQLiteMapperfromcheetah_orm.modelimportDataModel# Delete last databasetry:os.unlink("users.db")except:pass# Define data modelsclassUser(DataModel):table="users"name=StringField(length=32,not_null=True)pswd=PasswordField(length=128,not_null=True)email=StringField(length=128,not_null=True)question=StringField(length=128,not_null=True)answer=StringField(length=128,not_null=True)joined=DateTimeField(not_null=True,default="now()")ban=DateTimeField(not_null=True,default="now()")name_idx=UniqueIndex("name")email_idx=UniqueIndex("email")joined_idx=Index("joined")classPost(DataModel):table="posts"user=BigIntField(not_null=True)date=DateTimeField(not_null=True,default="now()")content=StringField(length=65535,not_null=True)user_idx=ForeignKey(User,"id")date_idx=Index("date")# Establish a database connectionmapper=SQLiteMapper()mapper.connect(database="users.db")# Initialize data modelsmapper.init_model(User)mapper.init_model(Post)# Create some data models and save themdaniel=User(name="Daniel",pswd="lion",email="daniel@lionzrule.com",question="Favorite species?",answer="lion")leila=User(name="Leila",pswd="lioness",email="leila@lionzrule.com",question="Favorite species?",answer="lioness")james=User(name="James",pswd="cheetah",email="james@cheetahzrule.com",question="Favorite species?",answer="cheetah")abby=User(name="Abby",pswd="cheetahess",email="abby@cheetahzrule.com",question="Favorite species?",answer="cheetah")fiona=User(name="Fiona",pswd="fox",email="fiona@foxezrule.com",question="Favorite species?",answer="fox")unknown=User(name="Unknown",pswd="",email="",question="",answer="")mapper.save_model(daniel)mapper.save_model(leila)mapper.save_model(james)mapper.save_model(abby)mapper.save_model(fiona)mapper.save_model(unknown)mapper.commit()# Update the models and save themdaniel.question="Favorite animal?"leila.question="Favorite animal?"james.question="Favorite animal?"abby.question="Favorite animal?"fiona.question="Favorite animal?"mapper.save_model(daniel)mapper.save_model(leila)mapper.save_model(james)mapper.save_model(abby)mapper.save_model(fiona)mapper.commit()# Delete a modelmapper.delete_model(User(id=6))mapper.commit()# Fetch all usersusers=mapper.filter(User)print("Users:")foruserinusers:print(user)print()# Fetch Danielusers=mapper.filter(User,"`name`=?","Daniel")print("Users Named 'Daniel':")foruserinusers:print(user)print()# Fetch Leila and Abbyusers=mapper.filter(User,"`name`=? OR `name`=?","Leila","Abby")print("Users Named 'Leila' or 'Abby':")foruserinusers:print(user)print()# Fetch all users with "Favorite animal?" as their security question ordered by usernameusers=mapper.filter(User,"`question`=?","Favorite animal?",order_by=["name"])print("Users with the Security Question 'Favorite animal?' Ordered by Username:")foruserinusers:print(user)print()# The middle 2 usersusers=mapper.filter(User,"`question`=?","Favorite animal?",order_by=["name"],offset=1,limit=2)print("The Middle 2 Users:")foruserinusers:print(user)print()# Create some postspost=Post(user=daniel.id,content="Hello everyone!")mapper.save_model(post)post=Post(user=leila.id,content="Hello!")mapper.save_model(post)post=Post(user=james.id,content="Hi!")mapper.save_model(post)post=Post(user=abby.id,content="Heya!")mapper.save_model(post)post=Post(user=fiona.id,content="Hello darling!")mapper.save_model(post)post=Post(user=fiona.id,content="Huh? ...Help!")mapper.save_model(post)post=Post(user=daniel.id,content="Oh no!")mapper.save_model(post)# Now delete a usermapper.delete_model(fiona)mapper.commit()# Verify that the user's posts were deleted toopost_cnt=mapper.count(Post,"`user`=?",5)print(f"Fiona has{post_cnt}post(s).")# Disconnect from the databasemapper.disconnect()