cheetah-orm


Namecheetah-orm JSON
Version 2.0.0b7 PyPI version JSON
download
home_page
SummaryA lightweight and high-performance object-relational mapper for Python.
upload_time2023-06-30 21:46:32
maintainer
docs_urlNone
author
requires_python>=3
licenseMIT
keywords object-relational mapper
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Cheetah-ORM
A lightweight and high-performance object relational mappper written in pure Python.


# Installation
`pip install cheetah_orm`

Note: 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.


# Building
1. clone this repo
2. install the "wheel" package by executing `pip install wheel`
3. from the repo folder, execute `pip wheel .`
4. there will be a wheel file inside the "dist" folder


# Features
* support for sqlite3, MariaDB/MySQL, and PostgreSQL
* high-level database-neutral API written in pure Python
* automatically generates the needed SQL statements for whichever database system you prefer to
  use
* rich filtering API supports any conditional statement that is supported by the underlying database
  system plus sorting, offsets, and limits
* result set counting


# Usage
```python
#!/usr/bin/python3
"""Cheetah ORM - Demo"""

from datetime import datetime
import os
import unittest

from cheetah_orm.fields import (
    BigIntField,
    BlobField,
    DateTimeField,
    DoubleField,
    IntField,
    FloatField,
    PasswordField,
    StringField
)
from cheetah_orm.indexes import ForeignKey, Index, UniqueIndex
from cheetah_orm.mappers import SQLiteMapper
from cheetah_orm.model import DataModel


# Delete last database
try:
    os.unlink("users.db")

except:
    pass


# Define data models
class User(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")


class Post(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 connection
mapper = SQLiteMapper()
mapper.connect(database="users.db")

# Initialize data models
mapper.init_model(User)
mapper.init_model(Post)

# Create some data models and save them
daniel = 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 them
daniel.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 model
mapper.delete_model(User(id=6))
mapper.commit()

# Fetch all users
users = mapper.filter(User)
print("Users:")

for user in users:
    print(user)

print()

# Fetch Daniel
users = mapper.filter(User, "`name`=?", "Daniel")
print("Users Named 'Daniel':")

for user in users:
    print(user)

print()

# Fetch Leila and Abby
users = mapper.filter(User, "`name`=? OR `name`=?", "Leila", "Abby")
print("Users Named 'Leila' or 'Abby':")

for user in users:
    print(user)

print()

# Fetch all users with "Favorite animal?" as their security question ordered by username
users = mapper.filter(User, "`question`=?", "Favorite animal?", order_by=["name"])
print("Users with the Security Question 'Favorite animal?' Ordered by Username:")

for user in users:
    print(user)

print()

# The middle 2 users
users = mapper.filter(User, "`question`=?", "Favorite animal?", order_by=["name"], offset=1, limit=2)
print("The Middle 2 Users:")

for user in users:
    print(user)

print()

# Create some posts
post = 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 user
mapper.delete_model(fiona)
mapper.commit()

# Verify that the user's posts were deleted too
post_cnt = mapper.count(Post, "`user`=?", 5)
print(f"Fiona has {post_cnt} post(s).")

# Disconnect from the database
mapper.disconnect()
```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "cheetah-orm",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3",
    "maintainer_email": "",
    "keywords": "object-relational mapper",
    "author": "",
    "author_email": "Cybermals <cybermals@googlegroups.com>",
    "download_url": "",
    "platform": null,
    "description": "# Cheetah-ORM\r\nA lightweight and high-performance object relational mappper written in pure Python.\r\n\r\n\r\n# Installation\r\n`pip install cheetah_orm`\r\n\r\nNote: To use MySQL or MariaDB you will need to install the \"pymysql\" package as well. And\r\nto use PostgreSQL, you will need to install the \"psycopg\" package too. SQLite support requires no\r\nadditonal packages.\r\n\r\n\r\n# Building\r\n1. clone this repo\r\n2. install the \"wheel\" package by executing `pip install wheel`\r\n3. from the repo folder, execute `pip wheel .`\r\n4. there will be a wheel file inside the \"dist\" folder\r\n\r\n\r\n# Features\r\n* support for sqlite3, MariaDB/MySQL, and PostgreSQL\r\n* high-level database-neutral API written in pure Python\r\n* automatically generates the needed SQL statements for whichever database system you prefer to\r\n  use\r\n* rich filtering API supports any conditional statement that is supported by the underlying database\r\n  system plus sorting, offsets, and limits\r\n* result set counting\r\n\r\n\r\n# Usage\r\n```python\r\n#!/usr/bin/python3\r\n\"\"\"Cheetah ORM - Demo\"\"\"\r\n\r\nfrom datetime import datetime\r\nimport os\r\nimport unittest\r\n\r\nfrom cheetah_orm.fields import (\r\n    BigIntField,\r\n    BlobField,\r\n    DateTimeField,\r\n    DoubleField,\r\n    IntField,\r\n    FloatField,\r\n    PasswordField,\r\n    StringField\r\n)\r\nfrom cheetah_orm.indexes import ForeignKey, Index, UniqueIndex\r\nfrom cheetah_orm.mappers import SQLiteMapper\r\nfrom cheetah_orm.model import DataModel\r\n\r\n\r\n# Delete last database\r\ntry:\r\n    os.unlink(\"users.db\")\r\n\r\nexcept:\r\n    pass\r\n\r\n\r\n# Define data models\r\nclass User(DataModel):\r\n    table      = \"users\"\r\n    name       = StringField(length=32, not_null=True)\r\n    pswd       = PasswordField(length=128, not_null=True)\r\n    email      = StringField(length=128, not_null=True)\r\n    question   = StringField(length=128, not_null=True)\r\n    answer     = StringField(length=128, not_null=True)\r\n    joined     = DateTimeField(not_null=True, default=\"now()\")\r\n    ban        = DateTimeField(not_null=True, default=\"now()\")\r\n    name_idx   = UniqueIndex(\"name\")\r\n    email_idx  = UniqueIndex(\"email\")\r\n    joined_idx = Index(\"joined\")\r\n\r\n\r\nclass Post(DataModel):\r\n    table      = \"posts\"\r\n    user       = BigIntField(not_null=True)\r\n    date       = DateTimeField(not_null=True, default=\"now()\")\r\n    content    = StringField(length=65535, not_null=True)\r\n    user_idx   = ForeignKey(User, \"id\")\r\n    date_idx   = Index(\"date\")\r\n\r\n\r\n# Establish a database connection\r\nmapper = SQLiteMapper()\r\nmapper.connect(database=\"users.db\")\r\n\r\n# Initialize data models\r\nmapper.init_model(User)\r\nmapper.init_model(Post)\r\n\r\n# Create some data models and save them\r\ndaniel = User(\r\n    name=\"Daniel\",\r\n    pswd=\"lion\",\r\n    email=\"daniel@lionzrule.com\",\r\n    question=\"Favorite species?\",\r\n    answer=\"lion\"\r\n)\r\nleila = User(\r\n    name=\"Leila\",\r\n    pswd=\"lioness\",\r\n    email=\"leila@lionzrule.com\",\r\n    question=\"Favorite species?\",\r\n    answer=\"lioness\"\r\n)\r\njames = User(\r\n    name=\"James\",\r\n    pswd=\"cheetah\",\r\n    email=\"james@cheetahzrule.com\",\r\n    question=\"Favorite species?\",\r\n    answer=\"cheetah\"\r\n)\r\nabby = User(\r\n    name=\"Abby\",\r\n    pswd=\"cheetahess\",\r\n    email=\"abby@cheetahzrule.com\",\r\n    question=\"Favorite species?\",\r\n    answer=\"cheetah\"\r\n)\r\nfiona = User(\r\n    name=\"Fiona\",\r\n    pswd=\"fox\",\r\n    email=\"fiona@foxezrule.com\",\r\n    question=\"Favorite species?\",\r\n    answer=\"fox\"\r\n)\r\nunknown = User(\r\n    name=\"Unknown\",\r\n    pswd=\"\",\r\n    email=\"\",\r\n    question=\"\",\r\n    answer=\"\"\r\n)\r\n\r\nmapper.save_model(daniel)\r\nmapper.save_model(leila)\r\nmapper.save_model(james)\r\nmapper.save_model(abby)\r\nmapper.save_model(fiona)\r\nmapper.save_model(unknown)\r\nmapper.commit()\r\n\r\n# Update the models and save them\r\ndaniel.question = \"Favorite animal?\"\r\nleila.question = \"Favorite animal?\"\r\njames.question = \"Favorite animal?\"\r\nabby.question = \"Favorite animal?\"\r\nfiona.question = \"Favorite animal?\"\r\n\r\nmapper.save_model(daniel)\r\nmapper.save_model(leila)\r\nmapper.save_model(james)\r\nmapper.save_model(abby)\r\nmapper.save_model(fiona)\r\nmapper.commit()\r\n\r\n# Delete a model\r\nmapper.delete_model(User(id=6))\r\nmapper.commit()\r\n\r\n# Fetch all users\r\nusers = mapper.filter(User)\r\nprint(\"Users:\")\r\n\r\nfor user in users:\r\n    print(user)\r\n\r\nprint()\r\n\r\n# Fetch Daniel\r\nusers = mapper.filter(User, \"`name`=?\", \"Daniel\")\r\nprint(\"Users Named 'Daniel':\")\r\n\r\nfor user in users:\r\n    print(user)\r\n\r\nprint()\r\n\r\n# Fetch Leila and Abby\r\nusers = mapper.filter(User, \"`name`=? OR `name`=?\", \"Leila\", \"Abby\")\r\nprint(\"Users Named 'Leila' or 'Abby':\")\r\n\r\nfor user in users:\r\n    print(user)\r\n\r\nprint()\r\n\r\n# Fetch all users with \"Favorite animal?\" as their security question ordered by username\r\nusers = mapper.filter(User, \"`question`=?\", \"Favorite animal?\", order_by=[\"name\"])\r\nprint(\"Users with the Security Question 'Favorite animal?' Ordered by Username:\")\r\n\r\nfor user in users:\r\n    print(user)\r\n\r\nprint()\r\n\r\n# The middle 2 users\r\nusers = mapper.filter(User, \"`question`=?\", \"Favorite animal?\", order_by=[\"name\"], offset=1, limit=2)\r\nprint(\"The Middle 2 Users:\")\r\n\r\nfor user in users:\r\n    print(user)\r\n\r\nprint()\r\n\r\n# Create some posts\r\npost = Post(\r\n    user=daniel.id,\r\n    content=\"Hello everyone!\"\r\n)\r\nmapper.save_model(post)\r\n\r\npost = Post(\r\n    user=leila.id,\r\n    content=\"Hello!\"\r\n)\r\nmapper.save_model(post)\r\n\r\npost = Post(\r\n    user=james.id,\r\n    content=\"Hi!\"\r\n)\r\nmapper.save_model(post)\r\n\r\npost = Post(\r\n    user=abby.id,\r\n    content=\"Heya!\"\r\n)\r\nmapper.save_model(post)\r\n\r\npost = Post(\r\n    user=fiona.id,\r\n    content=\"Hello darling!\"\r\n)\r\nmapper.save_model(post)\r\n\r\npost = Post(\r\n    user=fiona.id,\r\n    content=\"Huh? ...Help!\"\r\n)\r\nmapper.save_model(post)\r\n\r\npost = Post(\r\n    user=daniel.id,\r\n    content=\"Oh no!\"\r\n)\r\nmapper.save_model(post)\r\n\r\n# Now delete a user\r\nmapper.delete_model(fiona)\r\nmapper.commit()\r\n\r\n# Verify that the user's posts were deleted too\r\npost_cnt = mapper.count(Post, \"`user`=?\", 5)\r\nprint(f\"Fiona has {post_cnt} post(s).\")\r\n\r\n# Disconnect from the database\r\nmapper.disconnect()\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A lightweight and high-performance object-relational mapper for Python.",
    "version": "2.0.0b7",
    "project_urls": null,
    "split_keywords": [
        "object-relational",
        "mapper"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "658c94ec3f5865493a4d84e553b2fb6d1c745544b73a8729ea72931f7f2ce8db",
                "md5": "59b57a556155f0bf8c43c4a62d3b9dc3",
                "sha256": "eee0b2759a720b4d99452ef5436caad6e7f4271132febd6a9e298208dd665b9e"
            },
            "downloads": -1,
            "filename": "cheetah_orm-2.0.0b7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "59b57a556155f0bf8c43c4a62d3b9dc3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3",
            "size": 13800,
            "upload_time": "2023-06-30T21:46:32",
            "upload_time_iso_8601": "2023-06-30T21:46:32.970635Z",
            "url": "https://files.pythonhosted.org/packages/65/8c/94ec3f5865493a4d84e553b2fb6d1c745544b73a8729ea72931f7f2ce8db/cheetah_orm-2.0.0b7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-30 21:46:32",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "cheetah-orm"
}
        
Elapsed time: 0.11415s