Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python functools

import functools
import time

@functools.lru_cache(maxsize=256)
def find_user(name):
    # imitating slow search
    time.sleep(1)
    user = {"id": 11, "name": "Diane"}
    return user

find_user("Diane")
# kinda slow

find_user("Diane")
# blazingly fast
Comment

python functools

@cache
def factorial(n):
    return n * factorial(n-1) if n else 1

>>> factorial(10)      # no previously cached result, makes 11 recursive calls
3628800
>>> factorial(5)       # just looks up cached value result
120
>>> factorial(12)      # makes two new recursive calls, the other 10 are cached
479001600
Comment

PREVIOUS NEXT
Code Example
Python :: local variable referenced before assignment 
Python :: Yield Expressions in python 
Python :: print all objects in list python 
Python :: convert python code to pseudocode online 
Python :: django search 
Python :: python string: .upper() 
Python :: possible substrings of a string python 
Python :: argparse one argument or without argument 
Python :: why is c faster than python 
Python :: print variable python 
Python :: python list clear vs del 
Python :: is the multiply code in python 
Python :: string pythhon 
Python :: activate virtual environment python in linux 
Python :: how to connect mongodb database with python 
Python :: Python String count() example 
Python :: random.random 
Python :: turn list into string 
Python :: django form date picker 
Python :: python dict 
Python :: print column name and index dataframe 
Python :: Facet Grid for Bar Plot with non-shared y axes (CatPlot) 
Python :: tkinter hide legend 
Python :: display list 
Python :: doormat pattern 
Python :: Multiple page PyQt QStackedWidget 
Python :: how to add the number to email address in faker library in python? 
Python :: how to deploy to shinyapps.io 
Python :: text file sort by first item in each row 
Python :: docs in python parameter 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =