@@ -1347,15 +1347,6 @@ lock.release()
1347
1347
```
1348
1348
1349
1349
1350
- Hashlib
1351
- -------
1352
- ``` python
1353
- >> > import hashlib
1354
- >> > hashlib.md5(< str > .encode()).hexdigest()
1355
- ' 33d0eba106da4d3ebca17fcd3f4c3d77'
1356
- ```
1357
-
1358
-
1359
1350
Introspection
1360
1351
-------------
1361
1352
** Inspecting code at runtime.**
@@ -1389,7 +1380,7 @@ Metaprograming
1389
1380
** Code that generates code.**
1390
1381
1391
1382
### 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.**
1393
1384
1394
1385
``` python
1395
1386
< class > = type (< class_name> , < parents_tuple> , < attributes_dict> )
@@ -1434,17 +1425,17 @@ class MyClass(metaclass=MyMetaClass):
1434
1425
Operator
1435
1426
--------
1436
1427
``` 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
1441
1432
```
1442
1433
1443
1434
``` python
1444
1435
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 ))
1448
1439
LogicOp = enum.Enum(' LogicOp' , {' AND' : op.and_, ' OR' : op.or_})
1449
1440
last_el = op.methodcaller(' pop' )(< list > )
1450
1441
```
@@ -1459,7 +1450,7 @@ Eval
1459
1450
3
1460
1451
>> > literal_eval(' [1, 2, 3]' )
1461
1452
[1 , 2 , 3 ]
1462
- >> > ast. literal_eval(' abs(1)' )
1453
+ >> > literal_eval(' abs(1)' )
1463
1454
ValueError : malformed node or string
1464
1455
```
1465
1456
@@ -1469,13 +1460,13 @@ import ast
1469
1460
from ast import Num, BinOp, UnaryOp
1470
1461
import operator as op
1471
1462
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>
1479
1470
1480
1471
def evaluate (expression ):
1481
1472
root = ast.parse(expression, mode = ' eval' )
@@ -1679,7 +1670,7 @@ def odds_handler(sport):
1679
1670
``` python
1680
1671
# $ pip3 install requests
1681
1672
>> > import requests
1682
- >> > url = ' http://localhost:8080/odds/football'
1673
+ >> > url = ' http://localhost:8080/odds/football'
1683
1674
>> > data = {' team' : ' arsenal f.c.' }
1684
1675
>> > response = requests.post(url, data = data)
1685
1676
>> > response.json()
@@ -1771,12 +1762,12 @@ import numpy as np
1771
1762
```
1772
1763
1773
1764
``` python
1774
- < array> = < array> .sum(< axis> )
1775
- indexes = < array> .argmin(< axis> )
1765
+ < array> = < array> .sum(axis = None )
1766
+ indexes = < array> .argmin(axis = None )
1776
1767
```
1777
1768
1778
1769
* ** 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. **
1780
1771
1781
1772
### Indexing
1782
1773
``` bash
@@ -1806,7 +1797,7 @@ left = [[0.1], [0.6], [0.8]] # Shape: (3, 1)
1806
1797
right = [ 0.1 , 0.6 , 0.8 ] # Shape: (3)
1807
1798
```
1808
1799
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:
1810
1801
``` python
1811
1802
left = [[0.1 ], [0.6 ], [0.8 ]] # Shape: (3, 1)
1812
1803
right = [[0.1 , 0.6 , 0.8 ]] # Shape: (1, 3) <- !
@@ -1931,7 +1922,7 @@ write_to_wav_file('test.wav', frames_i)
1931
1922
1932
1923
#### Plays Popcorn:
1933
1924
``` python
1934
- # pip3 install simpleaudio
1925
+ # $ pip3 install simpleaudio
1935
1926
import simpleaudio, math, struct
1936
1927
from itertools import chain, repeat
1937
1928
F = 44100
@@ -1940,8 +1931,8 @@ P2 = '71♪,73,,74♪,73,,74,,71,,73♪,71,,73,,69,,71♪,69,,71,,67,,71♪,,,'
1940
1931
get_pause = lambda seconds : repeat(0 , int (seconds * F))
1941
1932
sin_f = lambda i , hz : math.sin(i * 2 * math.pi * hz / F)
1942
1933
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 )
1945
1936
get_note = lambda note : get_wave(* parse_n(note)) if note else get_pause(0.125 )
1946
1937
frames_i = chain.from_iterable(get_note(n) for n in f ' { P1}{ P1}{ P2} ' .split(' ,' ))
1947
1938
frames_b = b ' ' .join(struct.pack(' <h' , int (a * 30000 )) for a in frames_i)
0 commit comments