No project description provided
Project description
Data Snack
About
Data Snack is a minimalistic framework for storing and accessing structured data.
It uses an Entity
objects to define a schema for your data. Snack
provides an interface
for automatically serializing and storing entities in a cache database of you choice.
General interface that allows you to use different backends: redis, memcached.
Install
Data Snack can be easily installed using pypi repository.
pip install data_snack
Usage
This examples shows a basic usage of defining an entity and using Snack
to save and load it from the cache.
More examples can be found in the Examples section.
Example 1 - Creating new entities and saving
1. Define entities
The first thing you need to do is to define an Entity
.
Entities are used to define a common structure of the objects stores in your database.
Snack
is using pydantic
for type validation of entity fields.
from pydantic.dataclasses import dataclass
from typing import Text
from data_snack.entities import Entity
@dataclass
class Person(Entity):
index: Text
name: Text
2. Connect to Redis
Connect to you a cache database of your choice.
In this example we are using Redis
, but you could also use Memcached
if you want.
import redis
redis_connection = redis.Redis(host='127.0.0.1', port=6379, password='')
3. Create Snack instance
In this step we create a Snack
instance and connect it to our Redis
database.
Notice, that Redis
client is wrapped in our RedisConnection
class to ensure shared interface.
And at least we can register all entities that will be used in our project.
For each entity we specify a list of fields that will be used to define keys when saving our data.
from data_snack import Snack
from data_snack.connections.redis import RedisConnection
snack = Snack(connection=RedisConnection(redis_connection)) # create instance
snack.register_entity(Person, keys=['index']) # register your entity
4. Save and load your entities using Snack
You are ready to save and load data using Snack
.
snack.set(Person("1", "John"))
# 'Person-1'
snack.get(Person, ["1"])
# Person(index='1', name='John')
snack.set_many([Person("1", "John"), Person("2", "Anna")])
# ['Person-1', 'Person-2']
entities = snack.get_many(CarEntity, [["1"], ["2"]])
# [Person(index='1', name='John'), Person(index='2', name='Anna')]
Contact
Plugin was created by the Data Science team from Webinterpret.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for data_snack-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a8dd7b6f24fe2c1261725a5a50512230d966f6fa19a8b30d8f9b08c9cb611aa |
|
MD5 | 913aac8f3b35fd76ccd9805d173fa168 |
|
BLAKE2b-256 | e1435286a604ddb578c4c4c03ae8d9bf6ebd9ee43f0d610296eaa5a6b4a11b9a |