Skip to main content

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.

Features

  • Entity objects are stored in a compress form to reduce memory usage.
  • Snack is using Entity fields to define a unique key to represent an object stored in the db.
  • Snack is supporting batch saving and reading data to achieve high performance.

Core concepts

  • Entity - a class defines a schema of single object stored in db
  • key fields - a list of fields (defined as a list of str values) that will be used to create a key for a given Entity object.
  • key values - a list of values for key fields from given Entity
  • key - a str value created for a given Entity
    • created in a format: <Entity type name>-<key value 1>_<key value 2>...<key value N>

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.

We are recommending adding data validation to your entities. The easiest way is using pydantic for type validation of all 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, key_fields=['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'
entity = 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(Person, [["1"], ["2"]])
# [Person(index='1', name='John'), Person(index='2', name='Anna')]

4.1 Set expire time for entities

You can also specify number of seconds until the item is expired using Snack.

snack.set(Person("1", "John"), expire=100)
# 'Person-1'

5. Delete your entities using Snack

After you're done with your data you can delete it using Snack.

snack.delete(Person, ["1"])
# Person(index='1', name='John')
snack.delete_many(Person, [["1"], ["2"]])
# [Person(index='1', name='John'), Person(index='2', name='Anna')]

Documentation

Access documentation

WIP. Documentation will be hosted on github pages.

Setup documentation

Setup documentation directory

mkdir docs
cd docs

Create documentation scaffold. Make sure to select an option with separated directories for source and build.

sphinx-quickstart

Update extensions in docs/source/conf.py.

extensions = ['sphinx.ext.autodoc', 'sphinx.ext.napoleon']

Update apidoc documentation

Before you start make sure to import project src directory at the very top of docs/source/conf.py file.

import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join('..', '..', 'src')))

Since documentation uses additional modules (other than base data-snack), we need to install additional requirements:

pip install -r docs/requirements.txt

Update the scaffold and generate the html docs.

sphinx-apidoc -o ./source ../src/data_snack
make html

Contact

Plugin was created by the Data Science team from Webinterpret.

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

data_snack-0.3.1.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

data_snack-0.3.1-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file data_snack-0.3.1.tar.gz.

File metadata

  • Download URL: data_snack-0.3.1.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for data_snack-0.3.1.tar.gz
Algorithm Hash digest
SHA256 ce6235ab59f7655d77445ff033f4fb8c207c01ef82142502bca30263f0645545
MD5 417f5de7cb7700f1bc47fe7c84aa0f04
BLAKE2b-256 ac81a1a2e06b70dc5b56b3ddd1fc24cca5013586b1dfbe2951be4ddabb97126b

See more details on using hashes here.

File details

Details for the file data_snack-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: data_snack-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for data_snack-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f74d1461ef205a6d454cb4ee7aea0223bfc141cdbd0dba269dedfd35a0b62d7d
MD5 de4a921518ca35b926ccabd560ca39df
BLAKE2b-256 239e9f693b3098627f11433f8fb01898459de95822897a10836a3d74d3b13514

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