|
33 | 33 | ```
|
34 | 34 |
|
35 | 35 | ```python
|
36 |
| -sum_of_elements = sum(<list>) |
37 |
| -elementwise_sum = [sum(pair) for pair in zip(list_a, list_b)] |
38 |
| -sorted_by_second = sorted(<list>, key=lambda el: el[1]) |
39 |
| -sorted_by_both = sorted(<list>, key=lambda el: (el[1], el[0])) |
40 |
| -flattened_list = list(itertools.chain.from_iterable(<list>)) |
41 |
| -list_of_chars = list(<str>) |
42 |
| -product_of_elems = functools.reduce(lambda out, x: out * x, <list>) |
| 36 | +sum_of_elements = sum(<list>) |
| 37 | +elementwise_sum = [sum(pair) for pair in zip(list_a, list_b)] |
| 38 | +sorted_by_second = sorted(<list>, key=lambda el: el[1]) |
| 39 | +sorted_by_both = sorted(<list>, key=lambda el: (el[1], el[0])) |
| 40 | +flattened_list = list(itertools.chain.from_iterable(<list>)) |
| 41 | +list_of_chars = list(<str>) |
| 42 | +product_of_elems = functools.reduce(lambda out, x: out * x, <list>) |
| 43 | +no_duplicates = list(set(<list>)) # Does not preserve order |
| 44 | +no_duplicates_ordered = list(dict.fromkeys(<list>)) # Preserves order |
43 | 45 | ```
|
44 | 46 |
|
45 | 47 | ```python
|
|
99 | 101 | <set> = <set>.intersection(<set>) # Or: <set> & <set>
|
100 | 102 | <set> = <set>.difference(<set>) # Or: <set> - <set>
|
101 | 103 | <set> = <set>.symmetric_difference(<set>) # Or: <set> ^ <set>
|
102 |
| -<bool> = <set>.issubset(<set>) |
103 |
| -<bool> = <set>.issuperset(<set>) |
| 104 | +<bool> = <set>.issubset(<set>) # Or: <set> < <set> |
| 105 | +<bool> = <set>.issuperset(<set>) # Or: <set> > <set> |
104 | 106 | ```
|
105 | 107 |
|
106 | 108 | ### Frozenset
|
@@ -1339,6 +1341,64 @@ duration = time() - start_time
|
1339 | 1341 | from timeit import timeit
|
1340 | 1342 | timeit('"-".join(str(n) for n in range(100))',
|
1341 | 1343 | number=10000, globals=globals())
|
| 1344 | + |
| 1345 | +``` |
| 1346 | + |
| 1347 | +#### Decorator for timing function execution: |
| 1348 | +```python |
| 1349 | +from timeit import default_timer |
| 1350 | +from datetime import timedelta |
| 1351 | + |
| 1352 | +def stopwatch(func): |
| 1353 | + """Print runtime of decorated function.""" |
| 1354 | + def wrap(*args, **kw): |
| 1355 | + start = default_timer() |
| 1356 | + result = func(*args, **kw) |
| 1357 | + delta = timedelta(seconds=(default_timer() - start)) |
| 1358 | + print(f"Function {func.__name__} finished in {delta}") |
| 1359 | + return result |
| 1360 | + return wrap |
| 1361 | +``` |
| 1362 | + |
| 1363 | +#### Decorator for profiling functions: |
| 1364 | +```python |
| 1365 | +import cProfile |
| 1366 | + |
| 1367 | +def profiler(func): |
| 1368 | + """Decorator. |
| 1369 | + Create a run call profile of the decorated function.""" |
| 1370 | + def wrap(*args, **kwargs): |
| 1371 | + profile = cProfile.Profile() |
| 1372 | + result = profile.runcall(func, *args, **kwargs) |
| 1373 | + with open(f"profile_{func.__name__}.txt", "w") as stream: |
| 1374 | + stats = pstats.Stats(profile, stream=stream) |
| 1375 | + stats.strip_dirs().sort_stats("tottime") |
| 1376 | + stats.print_stats(20) |
| 1377 | + print(f"Profile saved as 'profile_{func.__name__}.txt'") |
| 1378 | + return result |
| 1379 | + return wrap |
| 1380 | +``` |
| 1381 | + |
| 1382 | +#### Decorator for function tracing: |
| 1383 | +```python |
| 1384 | +def tracer(func): |
| 1385 | + """Print a trace of the input and output of a function in one line.""" |
| 1386 | + def traced_func(*args, **kwargs): |
| 1387 | + result = func(*args, **kwargs) |
| 1388 | + if len(args) is not 0: |
| 1389 | + argslist = ", ".join(f"{x}" for x in args) |
| 1390 | + if len(kwargs) is not 0: |
| 1391 | + argslist = argslist + ", " if len(kwargs) is not 0 else "" |
| 1392 | + else: |
| 1393 | + argslist = "" |
| 1394 | + if len(kwargs) is not 0: |
| 1395 | + kwargslist = ", ".join([f"{k}={v}" for k, v in kwargs.items()]) |
| 1396 | + else: |
| 1397 | + kwargslist = "" |
| 1398 | + print( |
| 1399 | + f"{func.__name__}({argslist}{kwargslist}) = {result}") |
| 1400 | + return result |
| 1401 | + return traced_func |
1342 | 1402 | ```
|
1343 | 1403 |
|
1344 | 1404 | #### Generates a PNG image of call graph and highlights the bottlenecks:
|
|
0 commit comments