Skip to main content

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


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)

Uploaded Source

Built Distribution

fast_resource-0.1.0-py3-none-any.whl (13.1 kB view hashes)

Uploaded Python 3

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