Code Monkey home page Code Monkey logo

property-caching's Introduction

Property caching

https://travis-ci.org/yola/property-caching.svg?branch=fix/md-to-rst

Cached version of property

Written and used by the folks at Yola to support our free website builder.

Overview

  • cached_property - stores results of decorated methods in decorated object (in _cached_properties attribute)
  • class_cached_property - stores results of decorated methods in the class of decorated object (in _class_cached_properties attribute). All instances will share cached value.
  • clear_property_cache - deletes cached value (works for object cached properties only)
  • set_property_cache - explicitly sets property cache (works for object cached properties only)
  • is_property_cached - allows to check whether property was cached or not (works for object cached properties only)

Usage

from property_caching import (cached_property,
                              class_cached_property,
                              clear_property_cache,
                              set_property_cache,
                              truthy_cached_property)

class Dummy:
    @cached_property
    def foo(self):
        return self.service.expensive_operation()

    @truthy_cached_property
    def bar(self):
        # this value will only be cached if it doesn't evaluate to false:
        return self.service.expensive_operation2()

    @class_cached_property
    def service(self):
        return expensive_service_initialization()

d = Dummy()
d.foo   # calculates result and stores it in d._cached_properties
d.foo   # uses cached value

clear_property_cache(d, 'foo')   # clears cache for property `foo`
set_property_cache(d, 'foo', 42) # explicitly set cache for property `foo`

d2 = Dummy()
d2.foo  # re-calculates value of `foo` but uses cached service

Testing

Install development requirements:

pip install -r requirements.txt

Run the tests with:

python setup.py test
https://travis-ci.org/yola/property-caching.svg?branch=fix/md-to-rst

property-caching's People

Contributors

blaix avatar michaeljoseph avatar rayen avatar stefanor avatar zoidyzoidzoid avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

property-caching's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.