<jupyter_start><jupyter_text>`@cache`: guardar el valor de la función para esos argumentos    * Hacer que esa la cache de la función se guarde en un archivo `.json`  * ¿Alguien puede conseguir lo mismo con solo 1 línea de código?<jupyter_code>import time
def memoize(function):

    memo = {}

    def wrapper(*args, **kwargs):
        serialized = (args, tuple(list(kwargs.items())))
        if serialized in memo:
            return memo[serialized]  # = valor retornado
        else:
            rv = function(*args, **kwargs)
            memo[serialized] = rv
            return rv

    return wrapper
@memoize
def suma2(a,b,c=None):
    return a + b + c
suma2(1,2,c=3)
d = {}
d[(1,2,("as", 2))] = 1
d["asd"] = 1
d["asda"] =2
tuple(list(d.items()))
list(d.items())
@memoize
def fibonacci(n):
    # time.sleep(2)
    if n < 2:
        return n
    return fibonacci(n - 1) + fibonacci(n - 2)


def fibonacci_nocache(n):
    # time.sleep(2)
    if n < 2:
        return n
    return fibonacci(n - 1) + fibonacci(n - 2)
%%timeit

fibonacci(200)
%%timeit

fibonacci_nocache(200)
# cache
import functools


def cache(func):
    """Keep a cache of previous function calls"""

    @functools.wraps(func)
    def wrapper_cache(*args, **kwargs):
        cache_key = args + tuple(kwargs.items())
        if cache_key not in wrapper_cache.cache:
            wrapper_cache.cache[cache_key] = func(*args, **kwargs)
        return wrapper_cache.cache[cache_key]

    wrapper_cache.cache = dict()
    return wrapper_cache<jupyter_output><empty_output><jupyter_text>**Otros cursos**<jupyter_code>import time
import functools
import json
from collections import defaultdict
import os


class Cache:
    def __init__(self, func):
        functools.update_wrapper(self, func)
        self.func = func
        self.cache = (
            self.open_json() if os.path.isfile("cache.json") else defaultdict(str)
        )
        self._time_start = time.time()

    def __call__(self, *args, **kwargs):
        rutas = str(
            [argument for argument in args]
            + [
                (key, kwargs[key])
                for key in sorted(kwargs.keys(), key=lambda x: x.lower())
            ]
        )
        result = self.cache[rutas]
        if not result:
            result = self.func(*args, **kwargs)
            self.cache[rutas] = result

        if (time.time() - self._time_start) >= 5:
            self.save_json()
            self._time_start = time.time()
            
        return result

    def open_json(self):
        with open("cache.json", "r") as json_file:
            cache = json.load(json_file)
        return defaultdict(str, cache)

    def save_json(self):
        with open("cache.json", "w") as json_file:
            json.dump(self.cache, json_file)
            time.sleep(1)
import time
def memoize(function):

    memo = {}

    def wrapper(*args, **kwargs):
        serialized = (args, tuple(list(kwargs.items())))
        if serialized in memo:
            return memo[serialized]  # = valor retornado
        else:
            rv = function(*args, **kwargs)
            memo[serialized] = rv
            return rv

    return wrapper
@memoize
def suma2(a,b,c=None):
    return a + b + c
suma2(1,2,c=3)
d = {}
d[(1,2,("as", 2))] = 1
d["asd"] = 1
d["asda"] =2
tuple(list(d.items()))
list(d.items())
@memoize
def fibonacci(n):
    # time.sleep(2)
    if n < 2:
        return n
    return fibonacci(n - 1) + fibonacci(n - 2)


def fibonacci_nocache(n):
    # time.sleep(2)
    if n < 2:
        return n
    return fibonacci(n - 1) + fibonacci(n - 2)
%%timeit

fibonacci(200)
%%timeit

fibonacci_nocache(200)
# cache
import functools


def cache(func):
    """Keep a cache of previous function calls"""

    @functools.wraps(func)
    def wrapper_cache(*args, **kwargs):
        cache_key = args + tuple(kwargs.items())
        if cache_key not in wrapper_cache.cache:
            wrapper_cache.cache[cache_key] = func(*args, **kwargs)
        return wrapper_cache.cache[cache_key]

    wrapper_cache.cache = dict()
    return wrapper_cache<jupyter_output><empty_output><jupyter_text>**Otros cursos**<jupyter_code>import time
import functools
import json
from collections import defaultdict
import os


class Cache:
    def __init__(self, func):
        functools.update_wrapper(self, func)
        self.func = func
        self.cache = (
            self.open_json() if os.path.isfile("cache.json") else defaultdict(str)
        )
        self._time_start = time.time()

    def __call__(self, *args, **kwargs):
        rutas = str(
            [argument for argument in args]
            + [
                (key, kwargs[key])
                for key in sorted(kwargs.keys(), key=lambda x: x.lower())
            ]
        )
        result = self.cache[rutas]
        if not result:
            result = self.func(*args, **kwargs)
            self.cache[rutas] = result

        if (time.time() - self._time_start) >= 5:
            self.save_json()
            self._time_start = time.time()
            
        return result

    def open_json(self):
        with open("cache.json", "r") as json_file:
            cache = json.load(json_file)
        return defaultdict(str, cache)

    def save_json(self):
        with open("cache.json", "w") as json_file:
            json.dump(self.cache, json_file)
            time.sleep(1)
from functools import lru_cache<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup