Skip to content

Commit d8de6f4

Browse files
committed
Numbers, Paths, Array, Logging
1 parent 89aca79 commit d8de6f4

3 files changed

Lines changed: 72 additions & 72 deletions

File tree

README.md

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -493,11 +493,11 @@ Format
493493
Numbers
494494
-------
495495
```python
496-
<int> = int(<float/str/bool>) # A whole number. Floats get truncated.
497-
<float> = float(<int/str/bool>) # A 64-bit decimal. Also <float>e±<int>.
498-
<complex> = complex(real=0, imag=0) # Complex number. Also <float>±<float>j.
499-
<Fraction> = fractions.Fraction(<int>, <int>) # `Fraction(1, 2) / 3 == Fraction(1, 6)`.
500-
<Decimal> = decimal.Decimal(<str/int/tuple>) # `Decimal((1, (2, 3), 4)) == -230_000`.
496+
<int> = int(<float/str/bool>) # A whole number. Truncates floats.
497+
<float> = float(<int/str/bool>) # 64-bit decimal. Also <fl>e±<int>.
498+
<complex> = complex(real=0, imag=0) # Complex number. Also <fl> ± <fl>j.
499+
<Fraction> = fractions.Fraction(numer, denom) # `<Fraction> = <Fraction> / <int>`.
500+
<Decimal> = decimal.Decimal(<str/int/tuple>) # `Decimal((1, (2,), 3)) == -2000`.
501501
```
502502
* **`'int(<str>)'` and `'float(<str>)'` raise ValueError exception if string is malformed.**
503503
* **Decimal objects store numbers exactly, unlike most floats where `'1.1 + 2.2 != 3.3'`.**
@@ -507,20 +507,20 @@ Numbers
507507

508508
### Built-in Functions
509509
```python
510-
<num> = pow(<num>, <num>) # E.g. `pow(2, 3) == 2 ** 3 == 8`.
510+
<num> = pow(<num>, <num>) # E.g. `pow(3, 4) == 3 ** 4 == 81`.
511511
<num> = abs(<num>) # E.g. `abs(complex(3, 4)) == 5`.
512512
<num> = round(<num> [, ±ndigits]) # E.g. `round(123.45, -1) == 120`.
513-
<num> = min(<coll_of_nums>) # Also max(<num>, <num> [, ...]).
514-
<num> = sum(<coll_of_nums>) # Also math.prod(<coll_of_nums>).
513+
<num> = min(<coll_of_nums>) # Also `max(<num>, <num> [, ...])`.
514+
<num> = sum(<coll_of_nums>) # Also `math.prod(<coll_of_nums>)`.
515515
```
516516

517517
### Math
518518
```python
519-
from math import floor, ceil, trunc # Funcs that convert floats to ints.
520-
from math import pi, inf, nan, isnan # `inf*0` and `nan+1` are both nan.
521-
from math import sqrt, factorial # `sqrt(-1)` raises ValueError excp.
519+
from math import floor, ceil, trunc # Funcs that convert float into int.
520+
from math import pi, inf, nan, isnan # `inf*0` and `nan+1` return `nan`.
521+
from math import sqrt, factorial # `sqrt(-1)` will raise ValueError.
522522
from math import sin, cos, tan # Also: degrees, radians, asin, etc.
523-
from math import log, log10, log2 # Base can be passed via second arg.
523+
from math import log, log10, log2 # Log() can accept 'base' argument.
524524
```
525525

526526
### Statistics
@@ -1452,8 +1452,8 @@ BaseException
14521452
| +-- UnboundLocalError # Raised when local name is used before it's being defined.
14531453
+-- OSError # Errors such as FileExistsError, TimeoutError (see #Open).
14541454
| +-- ConnectionError # Errors such as BrokenPipeError and ConnectionAbortedError.
1455-
+-- RuntimeError # Is raised by errors that don't fit into other categories.
1456-
| +-- NotImplementedEr… # Can be raised by abstract methods or by unfinished code.
1455+
+-- RuntimeError # Is raised by errors that do not fit into other categories.
1456+
| +-- NotImplementedEr… # Can be raised by abstract methods or by an unfinished code.
14571457
| +-- RecursionError # Raised if max recursion depth is exceeded (3k by default).
14581458
+-- StopIteration # Raised when exhausted (empty) iterator is passed to next().
14591459
+-- TypeError # When an argument of the wrong type is passed to function.
@@ -1535,7 +1535,7 @@ arguments = sys.argv[1:]
15351535
### Argument Parser
15361536
```python
15371537
from argparse import ArgumentParser
1538-
p = ArgumentParser(description=<str>) # Also accepts 'usage' arg.
1538+
p = ArgumentParser(description=<str>) # Also accepts 'usage' str.
15391539
p.add_argument('-<char>', '--<name>', action='store_true') # Flag (defaults to False).
15401540
p.add_argument('-<char>', '--<name>', type=<type>) # Option (defaults to None).
15411541
p.add_argument('<name>', type=<type>, nargs=1) # Mandatory first argument.
@@ -1622,9 +1622,9 @@ from pathlib import Path
16221622
```
16231623

16241624
```python
1625-
<str> = os.getcwd() # Returns working dir. Starts as shell's $PWD.
1626-
<str> = os.path.join(<path>, ...) # Uses os.sep to join strings or Path objects.
1627-
<str> = os.path.realpath(<path>) # Resolves symlinks and calls path.abspath().
1625+
<str> = os.getcwd() # Returns working dir. Starts as shell's `$PWD`.
1626+
<str> = os.path.join(<path>, ...) # Uses `os.sep` to join strings or Path objects.
1627+
<str> = os.path.realpath(<path>) # Resolves symlinks and calls os.path.abspath().
16281628
```
16291629

16301630
```python
@@ -1634,19 +1634,19 @@ from pathlib import Path
16341634
```
16351635

16361636
```python
1637-
<list> = os.listdir(path='.') # Returns all file/dirnames located at the path.
1637+
<list> = os.listdir(path='.') # Returns all file/dir names located at 'path'.
16381638
<list> = glob.glob('<pattern>') # Returns paths matching the wildcard pattern.
16391639
```
16401640

16411641
```python
16421642
<bool> = os.path.exists(<path>) # Checks if path exists. Also <Path>.exists().
1643-
<bool> = os.path.isfile(<path>) # Also <Path>.is_file() and <DirEntry>.is_file().
1643+
<bool> = os.path.isfile(<path>) # Also <Path>.is_file(), <DirEntry>.is_file().
16441644
<bool> = os.path.isdir(<path>) # Also <Path>.is_dir() and <DirEntry>.is_dir().
16451645
```
16461646

16471647
```python
16481648
<stat> = os.stat(<path>) # A status object. Also <Path/DirEntry>.stat().
1649-
<num> = <stat>.st_size/st_mtime/# Returns size in bytes, modification time, etc.
1649+
<num> = <stat>.st_size/st_mtime/# Returns size in bytes, modification time, ...
16501650
```
16511651

16521652
### DirEntry
@@ -1655,7 +1655,7 @@ from pathlib import Path
16551655
```python
16561656
<iter> = os.scandir(path='.') # Returns DirEntry objects located at the path.
16571657
<str> = <DirEntry>.path # Is absolute if 'path' argument was absolute.
1658-
<str> = <DirEntry>.name # Returns path's final component as a string.
1658+
<str> = <DirEntry>.name # Returns the path's final component as string.
16591659
<file> = open(<DirEntry>) # Opens the file and returns its file object.
16601660
```
16611661

@@ -1688,7 +1688,7 @@ from pathlib import Path
16881688

16891689
```python
16901690
<str> = str(<Path>) # Returns path as string. Also <Path>.as_uri().
1691-
<file> = open(<Path>) # Also <Path>.read_text/write_bytes(<args>).
1691+
<file> = open(<Path>) # Also <Path>.read_text/write_bytes/…(<args>).
16921692
```
16931693

16941694

@@ -1907,8 +1907,8 @@ with <conn>: # Exits the block with commit() o
19071907
<conn>.execute('<query>', <dict/namedtuple>) # Replaces every :<key> with a matching value.
19081908
<conn>.executemany('<query>', <coll_of_coll>) # Executes the query once for each collection.
19091909
```
1910-
* **Passed values can be of type str, int, float, bytes, None, or bool (stored as 1 or 0).**
1911-
* **SQLite does not restrict columns to any type unless table is declared as strict.**
1910+
* **Accepts strings, ints, floats, bytes, None objects and bools (stored as 1 or 0).**
1911+
* **Columns are not restricted to any type unless table is declared as strict.**
19121912

19131913
### Example
19141914
**Values are not actually saved in this example because `'conn.commit()'` is omitted!**
@@ -2043,8 +2043,8 @@ from array import array
20432043
```
20442044

20452045
```python
2046-
<bytes> = bytes(<array>) # Returns a copy of array's memory as bytes.
2047-
<file>.write(<array>) # Appends array's memory to the binary file.
2046+
<bytes> = bytes(<array>) # Returns the copy of array's memory as bytes.
2047+
<file>.write(<array>) # Appends the array's memory to a binary file.
20482048
```
20492049

20502050

@@ -2177,20 +2177,20 @@ log.debug/info/warning/error/critical(<str>) # Sends passed message to the
21772177
### Setup
21782178
```python
21792179
log.basicConfig(
2180-
filename=None, # Logs to stderr when filename is None.
2181-
filemode='a', # Pass 'w' to overwrite existing file.
2182-
format='%(levelname)s:%(name)s:%(message)s', # Add '%(asctime)s' for local datetime.
2183-
level=log.WARNING, # Drops messages with a lower priority.
2184-
handlers=[log.StreamHandler(sys.stderr)] # Uses FileHandler if filename is set.
2180+
filename=None, # Prints to stderr when filename is None.
2181+
filemode='a', # Use mode 'w' to overwrite existing file.
2182+
format='%(levelname)s:%(name)s:%(message)s', # Using '%(asctime)s' adds local datetime.
2183+
level=log.WARNING, # Drops messages that have lower priority.
2184+
handlers=[log.StreamHandler(sys.stderr)] # Uses FileHandler when 'filename' is set.
21852185
)
21862186
```
21872187

21882188
```python
2189-
<Formatter> = log.Formatter('<format>') # Formats messages according to format.
2189+
<Formatter> = log.Formatter('<format>') # Formats messages using the format str.
21902190
<Handler> = log.FileHandler(<path>, mode='a') # Appends to file. Also `encoding=None`.
21912191
<Handler>.setFormatter(<Formatter>) # Only outputs bare messages by default.
21922192
<Handler>.setLevel(<str/int>) # Prints/saves every message by default.
2193-
<Logger>.addHandler(<Handler>) # Logger can have more than one handler.
2193+
<Logger>.addHandler(<Handler>) # Loggers can have more than one handler.
21942194
<Logger>.setLevel(<str/int>) # What's sent to its/ancestors' handlers.
21952195
<Logger>.propagate = <bool> # Cuts off ancestors' handlers if False.
21962196
```

0 commit comments

Comments
 (0)