コード例 #1
0
ファイル: mod_03_decorators.py プロジェクト: imim/pycourse
 def memoized_version_of_f(*args):
     """Wrapper using memoization
     """
     res = simcache.get_key(args)
     if not res:
         res = decorated_func(*args)  # Call the real function
         simcache.set_key(args, res)
     return res
コード例 #2
0
 def memoized_version_of_f(*args):
     """Wrapper using memoization
     """
     res = simcache.get_key(args)
     if not res:
         res = decorated_func(*args)  # Call the real function
         simcache.set_key(args, res)
     return res
コード例 #3
0
ファイル: mod_03_decorators.py プロジェクト: imim/pycourse
 def memoized_version_of_func(n):
     """Wrapper using memoization
     """
     res = simcache.get_key(n)
     if not res:
         res = func_to_memoize(n)  # Call the real function
         simcache.set_key(n, res)
     return res
コード例 #4
0
 def memoized_version_of_func(n):
     """Wrapper using memoization
     """
     res = simcache.get_key(n)
     if not res:
         res = func_to_memoize(n)  # Call the real function
         simcache.set_key(n, res)
     return res
コード例 #5
0
ファイル: mod_03_decorators.py プロジェクト: imim/pycourse
def factorial(x):
    time.sleep(0.1)  # This sleep can not be removed!!
    if x < 2:
        return 1
    res = simcache.get_key(x - 1)
    if not res:
        res = factorial(x - 1)
        simcache.set_key(x - 1, res)
    return x * res
コード例 #6
0
def factorial(x):
    time.sleep(0.1)  # This sleep can not be removed!!
    if x < 2:
        return 1
    res = simcache.get_key(x - 1)
    if not res:
        res = factorial(x - 1)
        simcache.set_key(x - 1, res)
    return x * res
コード例 #7
0
ファイル: mod_03_decorators.py プロジェクト: imim/pycourse
def memoized_fibonacci(n):
    # Try to retrieve value from cache
    res = simcache.get_key(n)
    if not res:
        # If failed, call real fibonacci func
        print "Memoized fibonacci func, proceeding to call real func", real_fibonacci, n
        res = real_fibonacci(n)
        # Store real result
        simcache.set_key(n, res)
    return res
コード例 #8
0
def memoized_fibonacci(n):
    # Try to retrieve value from cache
    res = simcache.get_key(n)
    if not res:
        # If failed, call real fibonacci func
        print "Memoized fibonacci func, proceeding to call real func", real_fibonacci, n
        res = real_fibonacci(n)
        # Store real result
        simcache.set_key(n, res)
    return res
コード例 #9
0
ファイル: mod_03_decorators.py プロジェクト: imim/pycourse
def fibonacci(n):
    res = simcache.get_key(n)
    if not res:
        res = real_fibonacci(n)
        simcache.set_key(n, res)
    return res
コード例 #10
0
def fibonacci(n):
    res = simcache.get_key(n)
    if not res:
        res = real_fibonacci(n)
        simcache.set_key(n, res)
    return res