Skip to main content

A minimal framework inspired by Django

Cherrypyrest is a minimal framework that provides all basic ORM functionalities and more such as Models, Serializers, REST API Controllers and some basic common utilities.

How to use

from cherrypyrest import models
from cherrypyrest import fields as base_fields

class User(models.Model):

    NAME = 'name'
    EMAIL = 'email'
    AGE = 'age'

    fields = [NAME, EMAIL, AGE]

    name = base_fields.String(null=True)
    email = base_fields.Email(required=True)
    age = base_fields.Number()

user = User()
user.set_value({"name": "abc", "email": "abc@xyz.com"})
print(user.db_repr())  # db representation of object
print(user.serialize()) # json serializable form


class Address(models.Model):

    CITY = 'city'
    STATE = 'state'
    COUNTRY = 'country'
    POSTAL_CODE = 'postal_code'  # internal representation of a field
    USER = 'user'
    alias = {
          POSTAL_CODE: 'pin_code',  # external or api reppresentation of a field
    }
    fields = [CITY, STATE, COUNTRY, POSTAL_CODE, USER]

    city = base_fields.String()
    state = base_fields.String()
    country = base_fields.String()
    postal_code = base_fields.Number(required=True)
    user = base_fields.RelatedField(child=User(), required=True)


address = Address()

address.set_value({"state": "ABC", "postal_code": 12345, "user": {"email": "abc@xyz.com"}})

print(address.serialize())
#{'city': '',
# 'state': 'ABC',
# 'country': '',
# 'pin_code': 12345,   # use of alias
# 'user': {'name': None, 'email': 'abc@xyz.com', 'age': None}}

You can add a lot of attributes in the model class such as public_fields, read_only_fields etc to make model response more flexible.

The model works best with MongoDB as the ObjectID field is already provided in the fields module. Add a properly setup manager class obejct in the model.

manager = UserManager()

NOTE: Manager object should have a valid database client object to connect to. You can conect with any database but mongodb works well without any changes in the code.

Add an attribute in the models class to identify the database fields db_fields which gets a list of fields that will be fetched from the database when required. So in our Address class

db_fields = [USER]

and then

user = User()
user.set_value({"email": "abc@xyz.com"})
user.save()
user_id = user.pk

address = Address()
address.set_value({"postal_code": 234325, "user": user_id})  # no call to the database
print(address.user.pk)
# $ ObjectId("23487h2i374x2748bbksjedhskf")
print(address.user.email)
# a call will be initiated to the database to fetch the user object by id and will set the whole object to the user attribute of addres object

So a call to db will only happend when we really need the related data.

I created this small package during my work on a project of one of my previous company. I haven’t really paid a lot of attention to the design patterns and structure but it worked for me at that time. You can update the code according to your needs.

Release files for cherrypyrest 0.2.1.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cherrypyrest 0.2.1.9
File Size Uploaded
cherrypyrest-0.2.1.9.tar.gz 18.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cherrypyrest 0.2.1.9
File Interpreter ABI Platform
cherrypyrest-0.2.1.9-py3-none-any.whl Python 3 none any Details

Total release size: 37.3 kB

Release files / cherrypyrest-0.2.1.9.tar.gz

Download URL cherrypyrest-0.2.1.9.tar.gz
Size 18.6 kB
Tags Source
SHA-256 checksum
How to use checksums
63e7369217cefd9e6c629e0bf732820a8e01a39f9fe7f4000fa80a5bc57c8a56
BLAKE2b-256 checksum
How to use checksums
b918fd9795d23c02bec6dce3568b9049b0dc55dbad102ba30d584793a9455f91
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.8.10

Release files / cherrypyrest-0.2.1.9-py3-none-any.whl

Download URL cherrypyrest-0.2.1.9-py3-none-any.whl
Size 18.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3b2ba9162ad68ab8a14d6db2456cd8c28a13a3edc3a1eac0f25516d7ce938dd1
BLAKE2b-256 checksum
How to use checksums
7d251c8ea0ed2b30ae003779e3689562a24348daaf2d919aa424dd296151f93a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.2.1.9 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page