Skip to main content

caroline

Build Status Codacy Badge Codacy Badge Code style: black Packaged with Poetry

A key-value JSON ODM with a memorable name.

What is caroline?

Caroline is yet another way to store JSON data. It contains backends for Elasticsearch and Redis, and allows you to use both in the same project! Included in the box (and heavily recommended that you use) is jsonschema support.

Why?

The existing options that are available didn't quite work for what we were looking for, so we wrote our own for internal use and decided to open source it. The goal of the project is easy integration without high overhead.

How does it work?

The only thing you need to do to get started is from caroline import Prototype. Then you can start building your data classes:

from caroline import Prototype

class Dog(Prototype):
    default = {
        "breed": "",
        "age": 1,
        "name": ""
    }
    schema = {
        "type": "object",
        "properties": {
            "breed": {
                "type": "string"
            },
            "age": {
                "type": "number"
            },
            "name": {
                "type": "string"
            }
        }
    }
    
sam = Dog('sam')

print(sam)
# >>> {"breed": "", "age": 1, "name": ""}

sam.update('name', 'Sam')
# OR
sam['name'] = 'Sam'

sam.update('age', 5)
sam.update('breed', 'mutt')
sam.save()

When you create a new instance of your class with an ID (like Dog('sam')), that's the key that the particular record will be saved under. That means that if you create a class with that name, it'll load the same record again:

from example_above import Dog

x = Dog('sam')

print(x)
# >>> {'breed': 'mutt', 'age': 5, 'name': 'Sam'}

If you create an instance of your class with an ID that isn't in your chosen database, then it will instantiate it using the default dict that you defined in the class.

caroline will automatically handle its own connections, but if you've got a custom one, feel free to pass it in through your model:

class Cat(Prototype):
    elasticsearch_conn = your_elasticsearch_connection
    # OR
    redis_conn = your_redis_connection
    
    default = {}

NOTE: You cannot have more than one connection on your model! Each model can only work with one database; that being said, you can have each model route to a different database if you want to and caroline with handle it for you. If you don't want to pass a specific connection with each model, we don't blame you; The default connection is Elasticsearch, but you can change that by setting the environment variable CAROLINE_DEFAULT_DB to either "elasticsearch" or "redis". You can also import the caroline config directly and manually set the requested database as the default (caroline.config.default_db = "redis").

There is currently not a way to change the ElasticSearch location, but you can set the Redis location by formatting your Redis address as a URL, (e.g. redis://localhost:6379/0) which caroline will pick up if you set it as the environment variable CAROLINE_REDIS_URL.

If time goes on and you need to upgrade your models, we have a plan for that! Just modify your model (add new fields or remove them), then load your keys as normal. Call .upgrade() on the object that you've retrieved from the database and caroline will force your existing data into the new model. THIS IS A DESTRUCTIVE CALL.

from caroline import Prototype

class Dog(Prototype):
    default = {
        "age": 1,
        "name": "",
        "sire": "",
        "dam": ""
    }
    db = 'redis'

x = Dog('sam')
print(x)

# we get the last time we used the key `sam` for the prototype class `Dog`
# >>> {'breed': 'mutt', 'age': 5, 'name': 'Sam'}

# THIS CALL RESULTS IN THE DESTRUCTION OF DATA
x.upgrade()
print(x)

# >>> {'age': 5, 'name': 'Sam', 'sire': '', 'dam': ''}
x.save()

caroline also gives you the ability to choose your database root key names; by default, it will be the name of the class model you create. So for example, if you have a class named Dog with the record ID of 'sam' from above, then the record in the DB will have the key of ::dog::sam. You can adjust the first part by adding another parameter: db_key. Don't bother with this unless you have a good reason to change it.

A note on schemas: while we provide support for jsonschema (and that was the driving force behind the creation of this package), you don't have to use it -- as the above example illustrates, you don't need to create a schema key. If no schema is passed in, it does not perform the validation step on .save() -- otherwise, it validates the data against the schema every time .save() is called.

Finally, sometimes you just need a dict; our prototype classes do a great job of pretending to be a dict, but if you ever actually just need the data, call .to_dict() and a regular dictionary will be returned.

Metadata

Release files for caroline 0.5.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 caroline 0.5.2
File Size Uploaded Provenance
caroline-0.5.2.tar.gz 12.0 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 35.3 kB

Release files / caroline-0.5.2.tar.gz

Download URL caroline-0.5.2.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
ce0e308de92f964ab10b379dce82b2dea8bce2d71253da6b1631340bd71a68cc
BLAKE2b-256 checksum
How to use checksums
0505b420520d4a3f63cea2b2dc45e4f086a8cbae2931328397d4d74fcf24ac84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.11 CPython/3.7.3 Darwin/18.0.0

Release files / caroline-0.5.2-py3-none-any.whl

Download URL caroline-0.5.2-py3-none-any.whl
Size 23.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c6132660e0dbc7d89e7f5fea480bd7dc591471979d8778ec91549f76d3f494a4
BLAKE2b-256 checksum
How to use checksums
5b38e1b6bde8b8a009fd5fd9bc29a8e9ba541c5610aec674925f6a26f88b2035
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.11 CPython/3.7.3 Darwin/18.0.0

Security for caroline 0.5.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