|
| 1 | +# Pythonic Code! |
| 2 | +We want to write fast, memory efficient and super readable code. |
| 3 | +Try this out: |
| 4 | +```python |
| 5 | +import this |
| 6 | +``` |
| 7 | + |
| 8 | +At the beginning of my career, I did not pay a lot attention to this. |
| 9 | +However, you really need this not just for efficiency but also for being able to understand pythonic code. |
| 10 | + |
| 11 | + |
| 12 | +## Introduction |
| 13 | + |
| 14 | +### Unpacking and list apprehension |
| 15 | +Beyond List Apprehension: Unpacking! |
| 16 | +Convert a range object into a list: |
| 17 | +```python |
| 18 | +my_list = [*range(1,99,2)] |
| 19 | +``` |
| 20 | + |
| 21 | +Use enumerate for index and value pairs: |
| 22 | +```python |
| 23 | +indexed_vals_comp = [(i,val) for i, val in enumerate(vals)] |
| 24 | +## But this is not pythonic enough! Do this: |
| 25 | +indexed_vals_unpack = [*enumerate(vals, 1)] ## Begin index at 1 |
| 26 | + |
| 27 | +## try unpack with map() |
| 28 | + |
| 29 | +verbs_map = map(str.upper,verbs) |
| 30 | +verb_uppercase =[*verbs_map] |
| 31 | +``` |
| 32 | + |
| 33 | +Summerizing: you only need list apprehension if you are doing not vectorizble actions to entries in a iterator. |
| 34 | + |
| 35 | +### Numpy |
| 36 | +Boolean Indexing and Broadcasting |
| 37 | +Re-bind the IDs and the features |
| 38 | +```python |
| 39 | +old_features = [*range(2,99,3)] |
| 40 | + |
| 41 | +old_features_np = np.array(old_features) |
| 42 | +new_features = old_features * 9 |
| 43 | + |
| 44 | +new_bindings = [(IDs[i], new_feature) for i,new_feature in enumerate(new_features)] |
| 45 | + |
| 46 | +## Use the new bindings for new mappings |
| 47 | +def phrase(index,i): |
| 48 | + return " ".join([index,"record has val",i]) |
| 49 | + |
| 50 | +phrase_map = map(phrase,new_bindings) |
| 51 | + |
| 52 | +phrases = [*phrase_map] |
| 53 | +print(*phrases,sep='\n') |
| 54 | +``` |
| 55 | + |
| 56 | +## Examing Run Time |
| 57 | +IPython Magic commands are enhancements on top of normal Python syntax |
| 58 | +Prefixed by the "%" character |
| 59 | + |
| 60 | +Try this out: |
| 61 | +```python |
| 62 | +%lsmagic |
| 63 | +``` |
| 64 | + |
| 65 | +Now we focus on %timeit |
| 66 | +```python |
| 67 | +%timeit |
| 68 | + |
| 69 | +%%timeit |
| 70 | +``` |
| 71 | +After these, you can discover which codes are more efficient than others! |
| 72 | + |
| 73 | +### runs and times per run |
| 74 | +```python |
| 75 | +%timeit -r9 -n12 formal_dict = dict() |
| 76 | +%timeit -r9 -n12 literal_dict = {} |
| 77 | +``` |
| 78 | + |
| 79 | +### cell magic |
| 80 | +```python |
| 81 | +%%timeit |
| 82 | +## Anycode in this cell... |
| 83 | +``` |
| 84 | +### actual timings |
| 85 | +```python |
| 86 | +times = %timeit -o set(my_stuff) |
| 87 | + |
| 88 | +## times for each run |
| 89 | +print(times.timings) |
| 90 | +print(times.best) |
| 91 | +print(times.worst) |
| 92 | +print(times.average) |
| 93 | + |
| 94 | +``` |
| 95 | +## Code profiling |
| 96 | +Let's see the frequency and timing of each lines of a code |
| 97 | +```python |
| 98 | +!pip install line_profiler |
| 99 | + |
| 100 | +%load_ext line_profiler |
| 101 | + |
| 102 | +## Magic command for line-by-line times |
| 103 | +%lprun |
| 104 | + |
| 105 | +## profile a function |
| 106 | +%lprun -f function_name function_name(arg1,arg2,agr3) |
| 107 | + |
| 108 | +``` |
| 109 | + |
| 110 | + |
0 commit comments