Skip to main content

A REST like implementation of cherrypy

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cherrypyrest-0.2.1.9.tar.gz (18.6 kB view details)

Uploaded Source

Built Distribution

cherrypyrest-0.2.1.9-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file cherrypyrest-0.2.1.9.tar.gz.

File metadata

  • Download URL: cherrypyrest-0.2.1.9.tar.gz
  • Upload date:
  • Size: 18.6 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for cherrypyrest-0.2.1.9.tar.gz
Algorithm Hash digest
SHA256 63e7369217cefd9e6c629e0bf732820a8e01a39f9fe7f4000fa80a5bc57c8a56
MD5 7223373d2c13624a68ef4f6cb32c0ba7
BLAKE2b-256 b918fd9795d23c02bec6dce3568b9049b0dc55dbad102ba30d584793a9455f91

See more details on using hashes here.

File details

Details for the file cherrypyrest-0.2.1.9-py3-none-any.whl.

File metadata

  • Download URL: cherrypyrest-0.2.1.9-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for cherrypyrest-0.2.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 3b2ba9162ad68ab8a14d6db2456cd8c28a13a3edc3a1eac0f25516d7ce938dd1
MD5 25f33bbc76eeed6be8c285895c39dddd
BLAKE2b-256 7d251c8ea0ed2b30ae003779e3689562a24348daaf2d919aa424dd296151f93a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page