def main(): say_whee() say_whee_from_import() greet("World") print(return_greeting("Adam")) waste_some_time(1) print(make_greeting("Benjamin", age=112)) math.factorial = decorators.debug(math.factorial) approximate_e(5) countdown(3) greet2("World") say_whee2() say_whee2() print(say_whee2.num_calls) counter = decorators.Counter() counter() counter() say_whee3() say_whee3() print(say_whee3.num_calls) first_one = TheOne() another_one = TheOne() print(id(first_one)) print(id(another_one)) print(first_one is another_one) nocache_fibonacci(10) print(nocache_fibonacci.num_calls) print(fibonacci(10)) print(fibonacci(8)) fibonaccifunc(10) fibonaccifunc(8) fibonaccifunc(5) fibonaccifunc(8) fibonaccifunc(5) print(fibonaccifunc.cache_info()) print(volume(3, 5)) print(volume.unit) bolt = average_speed(100, 9.58) print(bolt) print(bolt.to("km per hour")) print(bolt.to("mph").m)
waste_some_time(1000) @debug def make_greeting(name,age=None): if age is None: return f'Howdy {name}!' else: return f'Whoa {name}! {age} already, you are growing up!' print(make_greeting("Benjamin")) print(make_greeting("Richard", age=112)) print(make_greeting(name="Dorrisile", age=116)) # Apply a decorator to a standard library function math.factorial = debug(math.factorial) def approximate_e(terms=18): return sum(1 / math.factorial(n) for n in range(terms)) print(approximate_e(5)) @slow_down def countdown(from_number): if from_number < 1: print("Liftoff!") else: print(from_number) countdown(from_number - 1) countdown(3)
# sum([i ** 2 for i in range(10000)]) # # # waste_some_time(5) # # @debug # def make_greeting(name, age=None): # if age is None: # return f"Howdy {name}!" # else: # return f"Whoa {name}! {age} already, you are growing up!" # # # make_greeting("Richard", age=112) math.factorial = debug( math.factorial) # Apply a decorator to a standard library function def approximate_e(terms=18): return sum(1 / math.factorial(n) for n in range(terms)) # print(approximate_e()) @slow_down def countdown(from_number): if from_number < 1: print("Liftoff!") else: print(from_number)
# License for the specific language governing permissions and limitations # under the License. import socket import subprocess import urllib2 import logging import json import threading import select from decorators import debug logger = logging.getLogger(__name__) logwrap = debug(logger) class HTTPClient(object): def __init__(self, url=""): self.url = url self.opener = urllib2.build_opener(urllib2.HTTPHandler) def get(self, endpoint): req = urllib2.Request(self.url + endpoint) return self._open(req) def post(self, endpoint, data=None, content_type="application/json"): if not data: data = {} req = urllib2.Request(self.url + endpoint, data=json.dumps(data))
import decorators import math @decorators.timer def waste_some_time(num_times): res = 0 for _ in range(num_times): res += sum([i * 2 for i in range(10000)]) return res math.factorial = decorators.debug(math.factorial) def approximate_e(terms=18): return sum(1 / math.factorial(n) for n in range(terms))