@@ -96,7 +96,7 @@ value = <dict>.pop(key) # Removes item or raises KeyErro
96
96
### Counter
97
97
``` python
98
98
>> > from collections import Counter
99
- >> > colors = [' blue' , ' red ' , ' blue' , ' red' , ' blue ' ]
99
+ >> > colors = [' blue' , ' blue ' , ' blue' , ' red' , ' red ' ]
100
100
>> > counter = Counter(colors)
101
101
>> > counter[' yellow' ] += 1
102
102
Counter({' blue' : 3 , ' red' : 2 , ' yellow' : 1 })
@@ -377,7 +377,7 @@ import re
377
377
```
378
378
379
379
### Special Sequences
380
- * ** By default digits, whitespaces and alphanumerics from all alphabets are matched, unless ` 'flags=re.ASCII' ` argument is used.**
380
+ * ** By default digits, alphanumerics and whitespaces from all alphabets are matched, unless ` 'flags=re.ASCII' ` argument is used.**
381
381
* ** Use a capital letter for negation.**
382
382
``` python
383
383
' \d' == ' [0-9]' # Matches any digit.
@@ -442,7 +442,7 @@ Format
442
442
#### Comparison of presentation types:
443
443
``` text
444
444
+---------------+-----------------+-----------------+-----------------+-----------------+
445
- | | {<real >} | {<real >:f} | {<real >:e} | {<real >:%} |
445
+ | | {<float >} | {<float >:f} | {<float >:e} | {<float >:%} |
446
446
+---------------+-----------------+-----------------+-----------------+-----------------+
447
447
| 0.000056789 | '5.6789e-05' | '0.000057' | '5.678900e-05' | '0.005679%' |
448
448
| 0.00056789 | '0.00056789' | '0.000568' | '5.678900e-04' | '0.056789%' |
@@ -456,7 +456,7 @@ Format
456
456
```
457
457
``` text
458
458
+---------------+-----------------+-----------------+-----------------+-----------------+
459
- | | {<float>:.2} | {<real >:.2f} | {<real >:.2e} | {<real >:.2%} |
459
+ | | {<float>:.2} | {<float >:.2f} | {<float >:.2e} | {<float >:.2%} |
460
460
+---------------+-----------------+-----------------+-----------------+-----------------+
461
461
| 0.000056789 | '5.7e-05' | '0.00' | '5.68e-05' | '0.01%' |
462
462
| 0.00056789 | '0.00057' | '0.00' | '5.68e-04' | '0.06%' |
@@ -541,7 +541,7 @@ shuffle(<list>)
541
541
Combinatorics
542
542
-------------
543
543
* ** Every function returns an iterator.**
544
- * ** If you want to print the iterator, you need to pass it to the list() function!**
544
+ * ** If you want to print the iterator, you need to pass it to the list() function first !**
545
545
546
546
``` python
547
547
from itertools import product, combinations, combinations_with_replacement, permutations
@@ -1093,7 +1093,7 @@ class MyComparable:
1093
1093
``` python
1094
1094
class MyHashable :
1095
1095
def __init__ (self , a ):
1096
- self ._a = copy.deepcopy(a)
1096
+ self ._a = a
1097
1097
@ property
1098
1098
def a (self ):
1099
1099
return self ._a
@@ -1146,7 +1146,7 @@ class Counter:
1146
1146
```
1147
1147
1148
1148
#### Python has many different iterator objects:
1149
- * ** Objects returned by the [ iter()] ( #iterator ) function, such as list\_ iterator and set\_ iterator.**
1149
+ * ** Iterators returned by the [ iter()] ( #iterator ) function, such as list\_ iterator and set\_ iterator.**
1150
1150
* ** Objects returned by the [ itertools] ( #itertools ) module, such as count, repeat and cycle.**
1151
1151
* ** Generators returned by the [ generator functions] ( #generator ) and [ generator expressions] ( #comprehension ) .**
1152
1152
* ** File objects returned by the [ open()] ( #open ) function, etc.**
@@ -1170,7 +1170,7 @@ class Counter:
1170
1170
```
1171
1171
1172
1172
### Context Manager
1173
- * ** Enter() should lock the resources and ( optionally) return an object.**
1173
+ * ** Enter() should lock the resources and optionally return an object.**
1174
1174
* ** Exit() should release the resources.**
1175
1175
* ** Any exception that happens inside the with block is passed to the exit() method.**
1176
1176
* ** If it wishes to suppress the exception it must return a true value.**
@@ -1205,8 +1205,9 @@ class MyIterable:
1205
1205
def __init__ (self , a ):
1206
1206
self .a = a
1207
1207
def __iter__ (self ):
1208
- for el in self .a:
1209
- yield el
1208
+ return iter (self .a)
1209
+ def __contains__ (self , el ):
1210
+ return el in self .a
1210
1211
```
1211
1212
1212
1213
``` python
@@ -1336,7 +1337,7 @@ from functools import partial
1336
1337
LogicOp = Enum(' LogicOp' , {' AND' : partial(lambda l , r : l and r),
1337
1338
' OR' : partial(lambda l , r : l or r)})
1338
1339
```
1339
- * ** Another solution in this particular case is to use built-in functions ` 'and_' ` and ` 'or_' ` from the module [ operator] ( #operator ) .**
1340
+ * ** Another solution in this particular case is to use built-in functions and \_ () and or \_ () from the module [ operator] ( #operator ) .**
1340
1341
1341
1342
1342
1343
Exceptions
@@ -1419,7 +1420,7 @@ BaseException
1419
1420
+-- StopIteration # Raised by next() when run on an empty iterator.
1420
1421
+-- TypeError # Raised when an argument is of wrong type.
1421
1422
+-- ValueError # When an argument is of right type but inappropriate value.
1422
- +-- UnicodeError # Raised when encoding/decoding strings from/to bytes fails.
1423
+ +-- UnicodeError # Raised when encoding/decoding strings to/from bytes fails.
1423
1424
```
1424
1425
1425
1426
#### Collections and their exceptions:
@@ -1589,7 +1590,7 @@ from glob import glob
1589
1590
```
1590
1591
1591
1592
``` python
1592
- < str > = getcwd() # Returns current working directory.
1593
+ < str > = getcwd() # Returns the current working directory.
1593
1594
< str > = path.join(< path> , ... ) # Joins two or more pathname components.
1594
1595
< str > = path.abspath(< path> ) # Returns absolute path.
1595
1596
```
@@ -1663,15 +1664,15 @@ from pathlib import Path
1663
1664
OS Commands
1664
1665
-----------
1665
1666
### Files and Directories
1666
- * ** Paths can be either strings, Paths, or DirEntry objects.**
1667
+ * ** Paths can be either strings, Paths or DirEntry objects.**
1667
1668
* ** Functions report OS related errors by raising either OSError or one of its [ subclasses] ( #exceptions-1 ) .**
1668
1669
1669
1670
``` python
1670
1671
import os, shutil
1671
1672
```
1672
1673
1673
1674
``` python
1674
- os.chdir(< path> ) # Changes current working directory.
1675
+ os.chdir(< path> ) # Changes the current working directory.
1675
1676
os.mkdir(< path> , mode = 0o 777 ) # Creates a directory. Mode is in octal.
1676
1677
```
1677
1678
@@ -1787,7 +1788,7 @@ import csv
1787
1788
< writer> .writerow(< collection> ) # Encodes objects using `str(<el>)`.
1788
1789
< writer> .writerows(< coll_of_coll> ) # Appends multiple rows.
1789
1790
```
1790
- * ** File must be opened with ` 'newline=""' ` argument, or an extra '\r' will be added to every '\n' on platforms that use '\r\n' linendings !**
1791
+ * ** File must be opened with ` 'newline=""' ` argument, or an extra '\r' will be added to every '\n' on platforms that use '\r\n' line endings !**
1791
1792
1792
1793
### Parameters
1793
1794
* ** ` 'dialect' ` - Master parameter that sets the default values.**
@@ -1846,9 +1847,9 @@ db.close()
1846
1847
### Read
1847
1848
** Returned values can be of type str, int, float, bytes or None.**
1848
1849
``` python
1849
- < cursor> = db.execute(' <query>' ) # Can raise sqlite3.OperationalError .
1850
+ < cursor> = db.execute(' <query>' ) # Raises a subclass of sqlite3.Error .
1850
1851
< tuple > = < cursor> .fetchone() # Returns next row. Also next(<cursor>).
1851
- < list > = < cursor> .fetchall() # Returns remaining rows.
1852
+ < list > = < cursor> .fetchall() # Returns remaining rows. Also list(<cursor>).
1852
1853
```
1853
1854
1854
1855
### Write
@@ -1889,7 +1890,7 @@ db.executemany('<query>', <coll_of_above>) # Runs execute() many times.
1889
1890
from mysql import connector
1890
1891
db = connector.connect(host = < str > , user = < str > , password = < str > , database = < str > )
1891
1892
< cursor> = db.cursor()
1892
- < cursor> .execute(' <query>' ) # Only cursor has execute method .
1893
+ < cursor> .execute(' <query>' ) # Raises a subclass of mysql.connector.Error .
1893
1894
< cursor> .execute(' <query>' , < list / tuple > ) # Replaces '%s's in query with values.
1894
1895
< cursor> .execute(' <query>' , < dict / namedtuple> ) # Replaces '%(<key>)s's with values.
1895
1896
```
@@ -1958,7 +1959,7 @@ b'\x00\x01\x00\x02\x00\x00\x00\x03'
1958
1959
```
1959
1960
1960
1961
### Format
1961
- #### For standard sizes start format string with:
1962
+ #### For standard type sizes start format string with:
1962
1963
* ** ` '=' ` - native byte order**
1963
1964
* ** ` '<' ` - little-endian**
1964
1965
* ** ` '>' ` - big-endian (also ` '!' ` )**
@@ -1982,8 +1983,9 @@ Array
1982
1983
1983
1984
``` python
1984
1985
from array import array
1985
- < array> = array(' <typecode>' , < collection> ) # Array from coll. of numbers.
1986
+ < array> = array(' <typecode>' , < collection> ) # Array from collection of numbers.
1986
1987
< array> = array(' <typecode>' , < bytes > ) # Array from bytes object.
1988
+ < array> = array(' <typecode>' , < array> ) # Treats array as a sequence of numbers.
1987
1989
< bytes > = bytes (< array> ) # Or: <array>.tobytes()
1988
1990
```
1989
1991
0 commit comments