From 32b2b63c9b6bd70e10cc09ef8e622f0798304a50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sun, 24 Feb 2019 06:04:00 +0100 Subject: [PATCH] System --- README.md | 132 +++++++++++++++++++++++++++++------------------------- 1 file changed, 70 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index da94875..da44b28 100644 --- a/README.md +++ b/README.md @@ -888,7 +888,7 @@ while True: break ``` -#### Raising exception: +### Raising Exception ```python raise ValueError('A very specific message!') ``` @@ -906,16 +906,15 @@ KeyboardInterrupt ``` -System ------- -### Print Function +Print +----- ```python print(, ..., sep=' ', end='\n', file=sys.stdout, flush=False) ``` * **Use `'file=sys.stderr'` for errors.** -#### Pretty print: +### Pretty Print ```python >>> from pprint import pprint >>> pprint(dir()) @@ -924,7 +923,9 @@ print(, ..., sep=' ', end='\n', file=sys.stdout, flush=False) '__doc__', ...] ``` -### Input Function + +Input +----- * **Reads a line from user input or pipe if present.** * **The trailing newline gets stripped.** * **The prompt string is printed to standard output before reading input.** @@ -942,14 +943,16 @@ while True: break ``` -### Open Function + +Open +---- **Opens file and returns a corresponding file object.** ```python = open('', mode='r', encoding=None) ``` -#### Modes: +### Modes * **`'r'` - Read (default).** * **`'w'` - Write (truncate).** * **`'x'` - Write or fail if the file already exists.** @@ -960,80 +963,30 @@ while True: * **`'t'` - Text mode (default).** * **`'b'` - Binary mode.** -#### Seek: +### Seek ```python .seek(0) # Move to start of the file. .seek(offset) # Move 'offset' chars/bytes from the start. .seek(offset, ) # Anchor: 0 start, 1 current pos., 2 end. ``` -#### Read Text from File: +### Read Text from File ```python def read_file(filename): with open(filename, encoding='utf-8') as file: return file.readlines() ``` -#### Write Text to File: +### Write Text to File ```python def write_to_file(filename, text): with open(filename, 'w', encoding='utf-8') as file: file.write(text) ``` -### Command Execution -```python -import os - = os.popen().read() -``` - -#### Or: -```python ->>> import subprocess ->>> a = subprocess.run(['ls', '-a'], stdout=subprocess.PIPE) ->>> a.stdout -b'.\n..\nfile1.txt\nfile2.txt\n' ->>> a.returncode -0 -``` - -### Recursion Limit -```python ->>> import sys ->>> sys.getrecursionlimit() -1000 ->>> sys.setrecursionlimit(5000) -``` - - -Command Line Arguments ----------------------- -### Basic -```python -import sys -script_name = sys.argv[0] -arguments = sys.argv[1:] -``` - -### Argparse -```python -from argparse import ArgumentParser, FileType -p = ArgumentParser(description=) -p.add_argument('-', '--', action='store_true') # Flag -p.add_argument('-', '--', type=) # Option -p.add_argument('', type=, nargs=1) # Argument -p.add_argument('', type=, nargs='+') # Arguments -args = p.parse_args() -value = args. -``` - -* **Use `'help='` for argument description.** -* **Use `'type=FileType()'` for files.** - Path ---- -### Basic ```python from os import path, listdir = path.exists('') @@ -1048,7 +1001,9 @@ from os import path, listdir ['1.gif', 'card.gif'] ``` -### Pathlib + +Pathlib +------- **This module offers classes representing filesystem paths with semantics appropriate for different operating systems.** ```python @@ -1080,6 +1035,59 @@ cwd = Path() ``` +Command Line Arguments +---------------------- +### Basic +```python +import sys +script_name = sys.argv[0] +arguments = sys.argv[1:] +``` + +### Argparse +```python +from argparse import ArgumentParser, FileType +p = ArgumentParser(description=) +p.add_argument('-', '--', action='store_true') # Flag +p.add_argument('-', '--', type=) # Option +p.add_argument('', type=, nargs=1) # Argument +p.add_argument('', type=, nargs='+') # Arguments +args = p.parse_args() +value = args. +``` + +* **Use `'help='` for argument description.** +* **Use `'type=FileType()'` for files.** + + +Command Execution +----------------- +```python +import os + = os.popen().read() +``` + +#### Or: +```python +>>> import subprocess +>>> a = subprocess.run(['ls', '-a'], stdout=subprocess.PIPE) +>>> a.stdout +b'.\n..\nfile1.txt\nfile2.txt\n' +>>> a.returncode +0 +``` + + +Recursion Limit +--------------- +```python +>>> import sys +>>> sys.getrecursionlimit() +1000 +>>> sys.setrecursionlimit(5000) +``` + + JSON ---- ```python