Code Monkey home page Code Monkey logo

py-cache-decorator's Introduction

Cache decorator

You have a big array with data. You have to run a long runtime function with this data. Data can be repeated. To not re-run function with repeatable data, it will be good to store results of completed runs.

Write decorator cache that stores results of completed runs with different arguments, number of arguments can be also different. If decorated function runs with repeating arguments it should return stored result instead of calling function again. Decorator cache creating for decorating only functions that take immutable arguments.

Also note, that decorator cache should work correctly with few decorated functions simultaneously and correctly return for every function separately.

Also cache should print Getting from cache when returns stored value and Calculating new result when run function with new arguments.

Example:

@cache
def long_time_func(a, b, c):
    return (a ** b ** c) % (a * c)

@cache
def long_time_func_2(n_tuple, power):
    return [number ** power for number in n_tuple]

long_time_func(1, 2, 3)
long_time_func(2, 2, 3)
long_time_func_2((5, 6, 7), 5)
long_time_func(1, 2, 3)
long_time_func_2((5, 6, 7), 10)
long_time_func_2((5, 6, 7), 10)

# Calculating new result 
# Calculating new result 
# Calculating new result 
# Getting from cache 
# Calculating new result 
# Getting from cache

Note: Check your code using this checklist before pushing your solution.

py-cache-decorator's People

Contributors

masterpieceelbow avatar dmytrosvirsa avatar abnormaltype avatar danylott avatar

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.