Skip to content

Commit 32b2b63

Browse files
committed
System
1 parent d3e2e93 commit 32b2b63

File tree

1 file changed

+70
-62
lines changed

1 file changed

+70
-62
lines changed

README.md

Lines changed: 70 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ while True:
888888
break
889889
```
890890

891-
#### Raising exception:
891+
### Raising Exception
892892
```python
893893
raise ValueError('A very specific message!')
894894
```
@@ -906,16 +906,15 @@ KeyboardInterrupt
906906
```
907907

908908

909-
System
910-
------
911-
### Print Function
909+
Print
910+
-----
912911
```python
913912
print(<el_1>, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
914913
```
915914

916915
* **Use `'file=sys.stderr'` for errors.**
917916

918-
#### Pretty print:
917+
### Pretty Print
919918
```python
920919
>>> from pprint import pprint
921920
>>> pprint(dir())
@@ -924,7 +923,9 @@ print(<el_1>, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
924923
'__doc__', ...]
925924
```
926925

927-
### Input Function
926+
927+
Input
928+
-----
928929
* **Reads a line from user input or pipe if present.**
929930
* **The trailing newline gets stripped.**
930931
* **The prompt string is printed to standard output before reading input.**
@@ -942,14 +943,16 @@ while True:
942943
break
943944
```
944945

945-
### Open Function
946+
947+
Open
948+
----
946949
**Opens file and returns a corresponding file object.**
947950

948951
```python
949952
<file> = open('<path>', mode='r', encoding=None)
950953
```
951954

952-
#### Modes:
955+
### Modes
953956
* **`'r'` - Read (default).**
954957
* **`'w'` - Write (truncate).**
955958
* **`'x'` - Write or fail if the file already exists.**
@@ -960,80 +963,30 @@ while True:
960963
* **`'t'` - Text mode (default).**
961964
* **`'b'` - Binary mode.**
962965

963-
#### Seek:
966+
### Seek
964967
```python
965968
<file>.seek(0) # Move to start of the file.
966969
<file>.seek(offset) # Move 'offset' chars/bytes from the start.
967970
<file>.seek(offset, <anchor>) # Anchor: 0 start, 1 current pos., 2 end.
968971
```
969972

970-
#### Read Text from File:
973+
### Read Text from File
971974
```python
972975
def read_file(filename):
973976
with open(filename, encoding='utf-8') as file:
974977
return file.readlines()
975978
```
976979

977-
#### Write Text to File:
980+
### Write Text to File
978981
```python
979982
def write_to_file(filename, text):
980983
with open(filename, 'w', encoding='utf-8') as file:
981984
file.write(text)
982985
```
983986

984-
### Command Execution
985-
```python
986-
import os
987-
<str> = os.popen(<command>).read()
988-
```
989-
990-
#### Or:
991-
```python
992-
>>> import subprocess
993-
>>> a = subprocess.run(['ls', '-a'], stdout=subprocess.PIPE)
994-
>>> a.stdout
995-
b'.\n..\nfile1.txt\nfile2.txt\n'
996-
>>> a.returncode
997-
0
998-
```
999-
1000-
### Recursion Limit
1001-
```python
1002-
>>> import sys
1003-
>>> sys.getrecursionlimit()
1004-
1000
1005-
>>> sys.setrecursionlimit(5000)
1006-
```
1007-
1008-
1009-
Command Line Arguments
1010-
----------------------
1011-
### Basic
1012-
```python
1013-
import sys
1014-
script_name = sys.argv[0]
1015-
arguments = sys.argv[1:]
1016-
```
1017-
1018-
### Argparse
1019-
```python
1020-
from argparse import ArgumentParser, FileType
1021-
p = ArgumentParser(description=<str>)
1022-
p.add_argument('-<short_name>', '--<name>', action='store_true') # Flag
1023-
p.add_argument('-<short_name>', '--<name>', type=<type>) # Option
1024-
p.add_argument('<name>', type=<type>, nargs=1) # Argument
1025-
p.add_argument('<name>', type=<type>, nargs='+') # Arguments
1026-
args = p.parse_args()
1027-
value = args.<name>
1028-
```
1029-
1030-
* **Use `'help=<str>'` for argument description.**
1031-
* **Use `'type=FileType(<mode>)'` for files.**
1032-
1033987

1034988
Path
1035989
----
1036-
### Basic
1037990
```python
1038991
from os import path, listdir
1039992
<bool> = path.exists('<path>')
@@ -1048,7 +1001,9 @@ from os import path, listdir
10481001
['1.gif', 'card.gif']
10491002
```
10501003

1051-
### Pathlib
1004+
1005+
Pathlib
1006+
-------
10521007
**This module offers classes representing filesystem paths with semantics appropriate for different operating systems.**
10531008

10541009
```python
@@ -1080,6 +1035,59 @@ cwd = Path()
10801035
```
10811036

10821037

1038+
Command Line Arguments
1039+
----------------------
1040+
### Basic
1041+
```python
1042+
import sys
1043+
script_name = sys.argv[0]
1044+
arguments = sys.argv[1:]
1045+
```
1046+
1047+
### Argparse
1048+
```python
1049+
from argparse import ArgumentParser, FileType
1050+
p = ArgumentParser(description=<str>)
1051+
p.add_argument('-<short_name>', '--<name>', action='store_true') # Flag
1052+
p.add_argument('-<short_name>', '--<name>', type=<type>) # Option
1053+
p.add_argument('<name>', type=<type>, nargs=1) # Argument
1054+
p.add_argument('<name>', type=<type>, nargs='+') # Arguments
1055+
args = p.parse_args()
1056+
value = args.<name>
1057+
```
1058+
1059+
* **Use `'help=<str>'` for argument description.**
1060+
* **Use `'type=FileType(<mode>)'` for files.**
1061+
1062+
1063+
Command Execution
1064+
-----------------
1065+
```python
1066+
import os
1067+
<str> = os.popen(<command>).read()
1068+
```
1069+
1070+
#### Or:
1071+
```python
1072+
>>> import subprocess
1073+
>>> a = subprocess.run(['ls', '-a'], stdout=subprocess.PIPE)
1074+
>>> a.stdout
1075+
b'.\n..\nfile1.txt\nfile2.txt\n'
1076+
>>> a.returncode
1077+
0
1078+
```
1079+
1080+
1081+
Recursion Limit
1082+
---------------
1083+
```python
1084+
>>> import sys
1085+
>>> sys.getrecursionlimit()
1086+
1000
1087+
>>> sys.setrecursionlimit(5000)
1088+
```
1089+
1090+
10831091
JSON
10841092
----
10851093
```python

0 commit comments

Comments
 (0)