Skip to content

Commit 4c3cb26

Browse files
committed
Few changes
1 parent 6d4aa97 commit 4c3cb26

File tree

1 file changed

+24
-33
lines changed

1 file changed

+24
-33
lines changed

README.md

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,15 +1347,6 @@ lock.release()
13471347
```
13481348

13491349

1350-
Hashlib
1351-
-------
1352-
```python
1353-
>>> import hashlib
1354-
>>> hashlib.md5(<str>.encode()).hexdigest()
1355-
'33d0eba106da4d3ebca17fcd3f4c3d77'
1356-
```
1357-
1358-
13591350
Introspection
13601351
-------------
13611352
**Inspecting code at runtime.**
@@ -1389,7 +1380,7 @@ Metaprograming
13891380
**Code that generates code.**
13901381

13911382
### Type
1392-
**Type is the root class. If only passed the object it returns it's type (class). Otherwise it creates a new class (and not an instance!).**
1383+
**Type is the root class. If only passed the object it returns it's type (class). Otherwise it creates a new class.**
13931384

13941385
```python
13951386
<class> = type(<class_name>, <parents_tuple>, <attributes_dict>)
@@ -1434,17 +1425,17 @@ class MyClass(metaclass=MyMetaClass):
14341425
Operator
14351426
--------
14361427
```python
1437-
from operator import add, sub, mul, truediv, floordiv, mod, pow, neg, abs, \
1438-
eq, ne, lt, le, gt, ge, \
1439-
not_, and_, or_, \
1440-
itemgetter, attrgetter, methodcaller
1428+
from operator import add, sub, mul, truediv, floordiv, mod, pow, neg, abs
1429+
from operator import eq, ne, lt, le, gt, ge
1430+
from operator import not_, and_, or_
1431+
from operator import itemgetter, attrgetter, methodcaller
14411432
```
14421433

14431434
```python
14441435
import operator as op
1445-
product_of_elems = functools.reduce(op.mul, <list>)
1446-
sorted_by_second = sorted(<list>, key=op.itemgetter(1))
1447-
sorted_by_both = sorted(<list>, key=op.itemgetter(1, 0))
1436+
product_of_elems = functools.reduce(op.mul, <collection>)
1437+
sorted_by_second = sorted(<collection>, key=op.itemgetter(1))
1438+
sorted_by_both = sorted(<collection>, key=op.itemgetter(1, 0))
14481439
LogicOp = enum.Enum('LogicOp', {'AND': op.and_, 'OR' : op.or_})
14491440
last_el = op.methodcaller('pop')(<list>)
14501441
```
@@ -1459,7 +1450,7 @@ Eval
14591450
3
14601451
>>> literal_eval('[1, 2, 3]')
14611452
[1, 2, 3]
1462-
>>> ast.literal_eval('abs(1)')
1453+
>>> literal_eval('abs(1)')
14631454
ValueError: malformed node or string
14641455
```
14651456

@@ -1469,13 +1460,13 @@ import ast
14691460
from ast import Num, BinOp, UnaryOp
14701461
import operator as op
14711462

1472-
LEGAL_OPERATORS = {ast.Add: op.add,
1473-
ast.Sub: op.sub,
1474-
ast.Mult: op.mul,
1475-
ast.Div: op.truediv,
1476-
ast.Pow: op.pow,
1477-
ast.BitXor: op.xor,
1478-
ast.USub: op.neg}
1463+
LEGAL_OPERATORS = {ast.Add: op.add, # <el> + <el>
1464+
ast.Sub: op.sub, # <el> - <el>
1465+
ast.Mult: op.mul, # <el> * <el>
1466+
ast.Div: op.truediv, # <el> / <el>
1467+
ast.Pow: op.pow, # <el> ** <el>
1468+
ast.BitXor: op.xor, # <el> ^ <el>
1469+
ast.USub: op.neg} # - <el>
14791470

14801471
def evaluate(expression):
14811472
root = ast.parse(expression, mode='eval')
@@ -1679,7 +1670,7 @@ def odds_handler(sport):
16791670
```python
16801671
# $ pip3 install requests
16811672
>>> import requests
1682-
>>> url = 'http://localhost:8080/odds/football'
1673+
>>> url = 'http://localhost:8080/odds/football'
16831674
>>> data = {'team': 'arsenal f.c.'}
16841675
>>> response = requests.post(url, data=data)
16851676
>>> response.json()
@@ -1771,12 +1762,12 @@ import numpy as np
17711762
```
17721763

17731764
```python
1774-
<array> = <array>.sum(<axis>)
1775-
indexes = <array>.argmin(<axis>)
1765+
<array> = <array>.sum(axis=None)
1766+
indexes = <array>.argmin(axis=None)
17761767
```
17771768

17781769
* **Shape is a tuple of dimension sizes.**
1779-
* **Axis is an index of dimension that gets collapsed.**
1770+
* **Axis is an index of dimension that gets collapsed. Leftmost dimension has index 0.**
17801771

17811772
### Indexing
17821773
```bash
@@ -1806,7 +1797,7 @@ left = [[0.1], [0.6], [0.8]] # Shape: (3, 1)
18061797
right = [ 0.1 , 0.6 , 0.8 ] # Shape: (3)
18071798
```
18081799

1809-
#### 1. If array shapes differ, left-pad the smaller shape with ones:
1800+
#### 1. If array shapes differ in length, left-pad the smaller shape with ones:
18101801
```python
18111802
left = [[0.1], [0.6], [0.8]] # Shape: (3, 1)
18121803
right = [[0.1 , 0.6 , 0.8]] # Shape: (1, 3) <- !
@@ -1931,7 +1922,7 @@ write_to_wav_file('test.wav', frames_i)
19311922

19321923
#### Plays Popcorn:
19331924
```python
1934-
# pip3 install simpleaudio
1925+
# $ pip3 install simpleaudio
19351926
import simpleaudio, math, struct
19361927
from itertools import chain, repeat
19371928
F = 44100
@@ -1940,8 +1931,8 @@ P2 = '71♪,73,,74♪,73,,74,,71,,73♪,71,,73,,69,,71♪,69,,71,,67,,71♪,,,'
19401931
get_pause = lambda seconds: repeat(0, int(seconds * F))
19411932
sin_f = lambda i, hz: math.sin(i * 2 * math.pi * hz / F)
19421933
get_wave = lambda hz, seconds: (sin_f(i, hz) for i in range(int(seconds * F)))
1943-
get_hz = lambda n: 8.176 * 2 ** (int(n) / 12)
1944-
parse_n = lambda note: (get_hz(note[:2]), 0.25 if len(note) > 2 else 0.125)
1934+
get_hz = lambda key: 8.176 * 2 ** (int(key) / 12)
1935+
parse_n = lambda note: (get_hz(note[:2]), 0.25 if '' in note else 0.125)
19451936
get_note = lambda note: get_wave(*parse_n(note)) if note else get_pause(0.125)
19461937
frames_i = chain.from_iterable(get_note(n) for n in f'{P1}{P1}{P2}'.split(','))
19471938
frames_b = b''.join(struct.pack('<h', int(a * 30000)) for a in frames_i)

0 commit comments

Comments
 (0)