Skip to content

Commit d234829

Browse files
committed
Merge branch 'master' of github.com:gto76/python-cheatsheet
2 parents 33bf37e + cbdad5e commit d234829

File tree

2 files changed

+91
-48
lines changed

2 files changed

+91
-48
lines changed

README.md

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -561,27 +561,28 @@ from dateutil.tz import UTC, tzlocal, gettz
561561
```python
562562
<D/DTn> = D/DT.today() # Current local date or naive datetime.
563563
<DTn> = DT.utcnow() # Naive datetime from current UTC time.
564-
<DTa> = DT.now(<tz>) # Aware datetime from current tz time.
564+
<DTa> = DT.now(<tzinfo>) # Aware datetime from current tz time.
565565
```
566+
* **To extract time use `'<DTn>.time()'` or `'<DTa>.timetz()'`.**
566567

567568
### Timezone
568569
```python
569-
<tz> = UTC # UTC timezone. London without DST.
570-
<tz> = tzlocal() # Local timezone.
571-
<tz> = gettz('<Cont.>/<City>') # Timezone from 'Continent/City_Name' str.
570+
<tzinfo> = UTC # UTC timezone. London without DST.
571+
<tzinfo> = tzlocal() # Local timezone.
572+
<tzinfo> = gettz('<Cont.>/<City>') # Timezone from 'Continent/City_Name' str.
572573
```
573574

574575
```python
575-
<DTa> = <DT>.astimezone(<tz>) # Datetime, converted to passed timezone.
576-
<Ta/DTa> = <T/DT>.replace(tzinfo=<tz>) # Unconverted object with new timezone.
576+
<DTa> = <DT>.astimezone(<tzinfo>) # Datetime, converted to passed timezone.
577+
<Ta/DTa> = <T/DT>.replace(tzinfo=<tzinfo>) # Unconverted object with new timezone.
577578
```
578579

579580
### Encode
580581
```python
581582
<D/T/DT> = D/T/DT.fromisoformat('<iso>') # Object from ISO string.
582583
<DT> = DT.strptime(<str>, '<format>') # Datetime from str, according to format.
583584
<D/DTn> = D/DT.fromordinal(<int>) # D/DTn from days since Christ.
584-
<DTa> = DT.fromtimestamp(<real>, <tz>) # DTa from seconds since Epoch in tz time.
585+
<DTa> = DT.fromtimestamp(<real>, <tz.>) # DTa from seconds since Epoch in tz time.
585586
```
586587
* **ISO strings come in following forms: `'YYYY-MM-DD'`, `'HH:MM:SS.ffffff[±<offset>]'`, or both separated by `'T'`. Offset is formatted as: `'HH:MM'`.**
587588
* **On Unix systems Epoch is `'1970-01-01 00:00 UTC'`, `'1970-01-01 01:00 CET'`, ...**
@@ -601,10 +602,15 @@ from dateutil.tz import UTC, tzlocal, gettz
601602
>>> dt.strftime("%A, %dth of %B '%y, %I:%M%p %Z")
602603
"Thursday, 14th of May '15, 11:39PM UTC+02:00"
603604
```
605+
* **For abbreviated weekday and month use `'%a'` and `'%b'`.**
604606

605-
#### Rest of the codes:
606-
* **`'a'` - Weekday, abbreviated name.**
607-
* **`'b'` - Month, abbreviated name.**
607+
### Arithmetics
608+
```python
609+
<D/DT> = <D/DT> ± <TD>
610+
<TD> = <TD> ± <TD>
611+
<TD> = <TD> */ <real>
612+
<float> = <TD> / <TD>
613+
```
608614

609615

610616
Arguments
@@ -1277,8 +1283,10 @@ Open
12771283
**Opens a file and returns a corresponding file object.**
12781284

12791285
```python
1280-
<file> = open('<path>', mode='r', encoding=None)
1286+
<file> = open('<path>', mode='r', encoding=None, endline=None)
12811287
```
1288+
* **`'encoding=None'` means default encoding is used, which is platform dependent. Best practice is to use `'encoding="utf-8"'` whenever possible.**
1289+
* **`'endline=None'` means all different end of line combinations are converted to '\n' on read, while on write all '\n' characters are converted to system's default line separator.**
12821290

12831291
### Modes
12841292
* **`'r'` - Read (default).**
@@ -1295,7 +1303,8 @@ Open
12951303
```python
12961304
<file>.seek(0) # Moves to the start of the file.
12971305
<file>.seek(offset) # Moves 'offset' chars/bytes from the start.
1298-
<file>.seek(offset, <anchor>) # Anchor: 0 start, 1 current pos., 2 end.
1306+
<file>.seek(0, 2) # Moves to the end of the file.
1307+
<bin_file>.seek(±offset, <anchor>) # Anchor: 0 start, 1 current pos., 2 end.
12991308
```
13001309

13011310
```python
@@ -1307,10 +1316,10 @@ Open
13071316

13081317
```python
13091318
<file>.write(<str/bytes>) # Writes a string or bytes object.
1310-
<file>.writelines(<list>) # Writes a list of strings or bytes objects.
1319+
<file>.writelines(<coll.>) # Writes a coll. of strings or bytes objects.
13111320
<file>.flush() # Flushes write buffer.
13121321
```
1313-
* **Methods do not add or strip trailing newlines.**
1322+
* **Methods do not add or strip trailing newlines, even writelines().**
13141323

13151324
### Read Text from File
13161325
```python
@@ -1331,21 +1340,26 @@ Path
13311340
----
13321341
```python
13331342
from os import path, listdir
1343+
from glob import glob
1344+
```
1345+
1346+
```python
13341347
<bool> = path.exists('<path>')
13351348
<bool> = path.isfile('<path>')
13361349
<bool> = path.isdir('<path>')
1337-
<list> = listdir('<path>')
13381350
```
13391351

13401352
```python
1341-
>>> from glob import glob
1342-
>>> glob('../*.gif')
1343-
['1.gif', 'card.gif']
1353+
<list> = listdir('<path>') # List of filenames located at 'path'.
1354+
<list> = glob('<pattern>') # Filenames matching the wildcard pattern.
13441355
```
13451356

13461357
### Pathlib
13471358
```python
13481359
from pathlib import Path
1360+
```
1361+
1362+
```python
13491363
cwd = Path()
13501364
<Path> = Path('<path>' [, '<path>', <Path>, ...])
13511365
<Path> = <Path> / '<dir>' / '<file>'
@@ -1355,11 +1369,11 @@ cwd = Path()
13551369
<bool> = <Path>.exists()
13561370
<bool> = <Path>.is_file()
13571371
<bool> = <Path>.is_dir()
1358-
<iter> = <Path>.iterdir()
13591372
```
13601373

13611374
```python
1362-
<iter> = <Path>.glob('<pattern>')
1375+
<iter> = <Path>.iterdir() # Iterator of filenames located at path.
1376+
<iter> = <Path>.glob('<pattern>') # Filenames matching the wildcard pattern.
13631377
```
13641378

13651379
```python
@@ -1711,7 +1725,7 @@ class MyMetaClass(type):
17111725
* **New() can also be called directly, usually from a new() method of a child class (**`def __new__(cls): return super().__new__(cls)`**), in which case init() is not called.**
17121726

17131727
### Metaclass Attribute
1714-
**When class is created it checks if it has metaclass defined. If not, it recursively checks if any of his parents has it defined and eventually comes to type().**
1728+
**Right before a class is created it checks if it has metaclass defined. If not, it recursively checks if any of his parents has it defined and eventually comes to type().**
17151729

17161730
```python
17171731
class MyClass(metaclass=MyMetaClass):
@@ -1723,7 +1737,12 @@ class MyClass(metaclass=MyMetaClass):
17231737
('abcde', 12345)
17241738
```
17251739

1726-
#### Type diagram (str is an instance of type, ...):
1740+
### Type Diagram
1741+
```python
1742+
type(MyClass) == MyMetaClass # MyClass is an instance of MyMetaClass.
1743+
type(MyMetaClass) == type # MyMetaClass is an instance of type.
1744+
```
1745+
17271746
```text
17281747
+---------+-------------+
17291748
| Classes | Metaclasses |
@@ -1736,7 +1755,12 @@ class MyClass(metaclass=MyMetaClass):
17361755
+---------+-------------+
17371756
```
17381757

1739-
#### Inheritance diagram (str is a subclass of object, ...):
1758+
### Inheritance Diagram
1759+
```python
1760+
MyClass.__base__ == object # MyClass is a subclass of object.
1761+
MyMetaClass.__base__ == type # MyMetaClass is a subclass of type.
1762+
```
1763+
17401764
```text
17411765
+---------+-------------+
17421766
| Classes | Metaclasses |

0 commit comments

Comments
 (0)