You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+47-23Lines changed: 47 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -561,27 +561,28 @@ from dateutil.tz import UTC, tzlocal, gettz
561
561
```python
562
562
<D/DTn>= D/DT.today() # Current local date or naive datetime.
563
563
<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.
565
565
```
566
+
***To extract time use `'<DTn>.time()'` or `'<DTa>.timetz()'`.**
566
567
567
568
### Timezone
568
569
```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.
572
573
```
573
574
574
575
```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.
577
578
```
578
579
579
580
### Encode
580
581
```python
581
582
<D/T/DT>= D/T/DT.fromisoformat('<iso>') # Object from ISO string.
582
583
<DT>=DT.strptime(<str>, '<format>') # Datetime from str, according to format.
583
584
<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.
585
586
```
586
587
***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'`.**
587
588
***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
601
602
>>> dt.strftime("%A, %dth of %B '%y, %I:%M%p %Z")
602
603
"Thursday, 14th of May '15, 11:39PM UTC+02:00"
603
604
```
605
+
***For abbreviated weekday and month use `'%a'` and `'%b'`.**
604
606
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
+
```
608
614
609
615
610
616
Arguments
@@ -1277,8 +1283,10 @@ Open
1277
1283
**Opens a file and returns a corresponding file object.**
***`'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.**
1282
1290
1283
1291
### Modes
1284
1292
***`'r'` - Read (default).**
@@ -1295,7 +1303,8 @@ Open
1295
1303
```python
1296
1304
<file>.seek(0) # Moves to the start of the file.
1297
1305
<file>.seek(offset) # Moves 'offset' chars/bytes from the start.
<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.
1311
1320
<file>.flush() # Flushes write buffer.
1312
1321
```
1313
-
***Methods do not add or strip trailing newlines.**
1322
+
***Methods do not add or strip trailing newlines, even writelines().**
1314
1323
1315
1324
### Read Text from File
1316
1325
```python
@@ -1331,21 +1340,26 @@ Path
1331
1340
----
1332
1341
```python
1333
1342
from os import path, listdir
1343
+
from glob import glob
1344
+
```
1345
+
1346
+
```python
1334
1347
<bool>= path.exists('<path>')
1335
1348
<bool>= path.isfile('<path>')
1336
1349
<bool>= path.isdir('<path>')
1337
-
<list>= listdir('<path>')
1338
1350
```
1339
1351
1340
1352
```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.
1344
1355
```
1345
1356
1346
1357
### Pathlib
1347
1358
```python
1348
1359
from pathlib import Path
1360
+
```
1361
+
1362
+
```python
1349
1363
cwd = Path()
1350
1364
<Path>= Path('<path>' [, '<path>', <Path>, ...])
1351
1365
<Path>=<Path>/'<dir>'/'<file>'
@@ -1355,11 +1369,11 @@ cwd = Path()
1355
1369
<bool>=<Path>.exists()
1356
1370
<bool>=<Path>.is_file()
1357
1371
<bool>=<Path>.is_dir()
1358
-
<iter>=<Path>.iterdir()
1359
1372
```
1360
1373
1361
1374
```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.
1363
1377
```
1364
1378
1365
1379
```python
@@ -1711,7 +1725,7 @@ class MyMetaClass(type):
1711
1725
***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.**
1712
1726
1713
1727
### 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().**
1715
1729
1716
1730
```python
1717
1731
classMyClass(metaclass=MyMetaClass):
@@ -1723,7 +1737,12 @@ class MyClass(metaclass=MyMetaClass):
1723
1737
('abcde', 12345)
1724
1738
```
1725
1739
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
+
1727
1746
```text
1728
1747
+---------+-------------+
1729
1748
| Classes | Metaclasses |
@@ -1736,7 +1755,12 @@ class MyClass(metaclass=MyMetaClass):
1736
1755
+---------+-------------+
1737
1756
```
1738
1757
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.
0 commit comments