Skip to main content

Redistil

logo

Declarative data types using Cerberus for schemas, optimised for Redis.

This codebase is similar to Modulus, except it is far simpler and features optimisations for Redis that preserve the simple key/value nature of it - specifically selective loading and saving of fields.

Features

  • Declarative data model
  • Cerberus schemas remove the need for bytes->string encode/decode
  • Simple implementation
  • Selective loading/saving of Fields
  • Saving / Loading use Redis pipelines for performance
  • Extensible to new datatypes

Installation

$ pip install redistil

Example

A basic example:

from redis import Redis
from redistil import Model, Field, String, Integer, Float, List

# define a model
class MyModel(Model):
    string = Field(String, primary_key=True)
    integer = Field(Integer)
    float = Field(Float)
    list = List(String)


redis = Redis()

# create an object but don't save it
obj = MyModel(string='abc', integer=123, float=1.23, list=['a', 'b'])

# create an object and immediately save it
obj = MyModel.create(redis,
    string='test string',
    integer=123,
    float=4.56,
    list=['a', 'b', 'c'],
)

# load the object using the primary key field
obj = MyModel.load(redis, 'test string')

# selectively load fields
obj = MyModel.load(redis, 'test string', MyModel.integer, MyModel.float)
# primary_key will always be loaded
print(obj.string)
print(obj.integer)
print(obj.float)
# 'list' will be None

# load fields after the fact
obj.load_fields(MyModel.list)
print(obj.list)

# update and selectively save fields
obj.list = ['d', 'e', 'f']
obj.save(redis, MyModel.list)

# determine where values are saved so we can bypass the object model if we want to
MyModel.key('test string')
# 'MyModel::test string'
MyModel.string.field()
# 'string'
MyModel.list.key(MyModel.key('test string'))
# 'MyModel::test string::list'

Usage

Available Fields

Properties:

  • Field - Indicates a Redish hash field
  • Set - A set associated with the model
  • List - A list associated with the model

Cerberus 'dict' type is not supported, instead you should flatten them into the model itself.

Field types:

  • Boolean
  • Binary
  • Date
  • DateTime
  • Float
  • Integer
  • Number
  • String
  • EmailAddress
  • IPAddress
  • IPV4Address
  • IPV6Address

Models

All data types are specified as a sub-class of Model.

Each field is specified as a class attribute which is a Field object containing a field type.

For example:

>>> from redis import Redis
>>> from redistil import Model, Field, String, List
>>>
>>> class MyModel(Model):
...     id = Field(String, primary_key=True)
...     values = List(String, required=True)
...
>>> redis = Redis()
>>> mymodel = MyModel.create(redis, id='abc', values=['a', 'b', 'c'])
>>> # reload
>>> mymodel = MyModel.load(redis, 'abc')
>>> print(mymodel.data)
{'id': 'abc', 'values': ['a', 'b', 'c']}

Field validation and defaults

Parameters to fields are simply passed through to the Cerberus schema. See this documentation for more Cerberus validation rules.

Selective saving of fields will only perform validation on the fields specified.

When you perform selective loading of fields, those fields' values are not loaded and may fail validation. In this case you should also perform selective saving of those same fields.

Cerberus validator rules can be added by adding a child class called "Validator" to your model definition.

from redistil import Model, Field, String

class MyModel(Model):
    # default_setter is a cerberus attribute which will set the value if it is not already
    # but only on save
    # the value may be either a function or a string
    # if the value is a string, the function must be defined in the Validator class as _normalize_default_setter_<name>
    # https://docs.python-cerberus.org/en/stable/normalization-rules.html
    value = Field(String, default_setter='generated_string')

    class Validator(Model.Validator):
        def _normalize_default_setter_generated_string(self, document):
            return 'abcdefg'

Limitations

  • Containers cannot be nested. Ie. lists and sets cannot contain lists, sets, or dicts.

Future Work

  • Support partial text search
  • Support indexing of fields
  • Improve README

Changelog

1.1.1

  • Fix Field.key taking a key as an argument.

1.1.0

  • Change how Field/Set/List work:
    • Field now inherits from FieldBase.
    • Set/List are now siblings of Field rather than Types.
  • Rename FieldType to Type as they are not limited to Fields.
  • Permit discovery of redis value paths:
    • Add key to Container types.
    • Add field to Field types.

1.0.0

  • Initial release

BSD 2-Clause License

Copyright (c) 2021, Adam Griffiths All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Metadata

Release files for redistil 1.1.2

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

Source distribution (sdist)

Source distribution for redistil 1.1.2
File Size Uploaded Provenance
redistil-1.1.2.tar.gz 9.7 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 18.6 kB

Release files / redistil-1.1.2.tar.gz

Download URL redistil-1.1.2.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
eb4c3880dae560d5b85a8cb54a149f1db47a5dc3586bd88eac2b53e2ca05ef83
BLAKE2b-256 checksum
How to use checksums
e9d5740b77d96408349af1fbffc04e437b521a7889e394dae4d18ad6e65968b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

Release files / redistil-1.1.2-py3-none-any.whl

Download URL redistil-1.1.2-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cdf8babdeb1fd5223c919b7eac32b65b7aeb867a1dbde830ab474e41837bce86
BLAKE2b-256 checksum
How to use checksums
fd00031fdeeecb424f629d18c03fd7c1470e8f26415c7dcb751ef3f8f6718806
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

Security for redistil 1.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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