fast-resource is a data transformation layer between the database and data returned to the application's users. Also, it can cache data by using Redis and Memcached.
Project description
fast-resource
Introduction
fast-resource
is a data transformation layer between the database and data returned to the application's users. Also, it can cache data by using Redis and Memcached.
Requirements
redis
pymemcache
Install
> pip install fast-resource
Usage
Quick Start
from typing import Dict
from fast_resource import Resource
class UserResource(Resource):
class Meta:
fields = (
'id',
'name',
)
def name(self, input_data: Dict) -> str:
return f'{input_data["name"]} {input_data["family"]}'
UserResource({'id': 1, 'name': 'bagher', 'family': 'rokni'}).to_dict()
Custom output
UserResource({'id': 1, 'name': 'bagher', 'family': 'rokni'}).to_dict(('id',))
Use cache decoder
from typing import Dict
from redis.client import Redis
from fast_resource import Resource, cache
from fast_resource.cache import RedisCache
def my_key_builder(input_data, field):
return f'post.{input_data["id"]}.{field}'
class PostResource(Resource):
class Meta:
fields = (
'id',
'title',
'comment_count',
)
@cache(key=my_key_builder)
def comment_count(self, input_data: Dict) -> str:
return comment.filter(id=input_data['id']).count()
Resource.cache_init(driver=RedisCache(Redis()))
PostResource({'id': 1, 'title': 'Why fast-resource?'}).to_dict()
Custom cache expire_time
@cache(key=my_key_builder, expire_time=60)
def comment_count(self, input_data: Dict) -> str:
return comment.filter(id=input_data['id']).count()
Cache delete
PostResource({'id': 1, 'name': 'bagher', 'family': 'rokni'}).cache_delete()
Cache delete & rebuild
PostResource({'id': 1, 'name': 'bagher', 'family': 'rokni'}).cache_delete(rebuild=True)
Cache delete by keys
PostResource.cache_delete(['post.1.comment_count'])
Create collection
user_collection = UserResource.collection([
{'id': 1, 'name': 'bagher', 'family': 'rokni'},
{'id': 2, 'name': 'sepehr', 'family': 'rokni'}
])
user_collection.to_dict()
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
fast-resource-0.1.0.tar.gz
(9.7 kB
view hashes)
Built Distribution
Close
Hashes for fast_resource-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 286aa86488a2294d67df091110a2f1a3cf66a7a3456a2bb2ef9d69be780f25cb |
|
MD5 | 36aa28ecfd34ca0f3e877f11c1b42aaa |
|
BLAKE2b-256 | 5b79389103d6192da728f44aea52fd4ed3b8cb4ea34780866e364ac6fcba19b3 |