This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
public:it:python [2021/09/14 22:17] – phil | public:it:python [Unknown date] (current) – removed - external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | * https:// | ||
- | * https:// | ||
- | * https:// | ||
- | |||
- | ==== Logging ==== | ||
- | |||
- | * https:// | ||
- | |||
- | ==== Chunking ===== | ||
- | |||
- | < | ||
- | adding = set() #of many things | ||
- | i = iter(adding) | ||
- | while True: | ||
- | chunk = list(islice(i, | ||
- | if not chunk: | ||
- | break | ||
- | else: | ||
- | print("," | ||
- | </ | ||
- | |||
- | ===== Memory Usage with decorators ===== | ||
- | ==== memory_profiler ==== | ||
- | * https:// | ||
- | |||
- | Install: | ||
- | < | ||
- | python3 -m pip install memory_profiler | ||
- | </ | ||
- | |||
- | Set the function decorator '' | ||
- | < | ||
- | @profile | ||
- | def my_func(arg1): | ||
- | # do stuff | ||
- | </ | ||
- | |||
- | Run your code like this: | ||
- | < | ||
- | python3 -m memory_profiler helloworld.py | ||
- | </ | ||
- | |||
- | |||
- | You get output like this: | ||
- | < | ||
- | Line # Mem usage Increment | ||
- | ============================================== | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | </ | ||
- | |||
- | |||
- | ===== Timing functions with decorators ===== | ||
- | - [[https:// | ||
- | |||
- | < | ||
- | import time, functools | ||
- | |||
- | def timer(func): | ||
- | """ | ||
- | @functools.wraps(func) | ||
- | def wrapper_timer(*args, | ||
- | start_time = time.perf_counter() | ||
- | value = func(*args, *kwargs) | ||
- | end_time = time.perf_counter() | ||
- | run_time = end_time - start_time | ||
- | print(f" | ||
- | return value | ||
- | return wrapper_timer | ||
- | |||
- | |||
- | @timer | ||
- | def dostuff(): | ||
- | # does stuff | ||
- | </ |