Skip to main content

Structures like SQLAlchemy's declarative_base or Django's models in your project!

Project description

prettyrecord allows you to define your data structures in declarative way, similar to SQLAlchemy’s declarative_base or Django’s models. To achieve the goal, prettyrecord uses Python’s descriptor and metaclass mechanisms.

Usage

  • to define new structure, subclass Record or derived class and fill with fields, eg.:

    from prettyrecord import Record
    
    
    class DummyRecord(Record, some_trait=False):
        field1 = Field(required=True, default_value='')
        field2 = Field(default_value=0)
        field3 = ListField(constraint=lambda value: -5 < value < 5)
    
    
    dr = DummyRecord(field1='foo', field3=[-2, 3, 1, 0, 4)
    print(dr.field1)          # shows 'foo'
    print(dr.__some_trait__)  # shows 'False'
  • to define new field, subclass Field, RecordField, ListField or derived class, eg.:

    from prettyrecord import Field
    
    
    class Integer(Field):
        def normalize(self, value):
            value = super(Integer, self).normalize(value)
            return int(value)
  • to change creation process of your structures, subclass MetaRecord and define new base class for them, eg.:

    from prettyrecord.record import Record, MetaRecord
    
    
    class MetaMyRecord(MetaRecord):
        def __new__(mcs, name, bases, attrs, **kwargs):
            # your code here - remember to call super().__new__
    
        def __init__(cls, name, bases, attrs, **kwargs):
            # your code here - remember to call super().__init__
    
    
    class MyRecord(Record, metaclass=MetaMyRecord):
        pass

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

prettyrecord-0.2.zip (11.6 kB view details)

Uploaded Source

File details

Details for the file prettyrecord-0.2.zip.

File metadata

  • Download URL: prettyrecord-0.2.zip
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for prettyrecord-0.2.zip
Algorithm Hash digest
SHA256 9b0fe0deb09e7dbd979532c898a0c573df59ebcf04a731a95578aefc23aaf070
MD5 906a28d8227b7e32cb9e3567d859c29c
BLAKE2b-256 3aaaedcb940fb587644369cbae2c4ce41b7b4e4f55087bd4737ab51ad212d5bc

See more details on using hashes here.

Supported by

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