From ee6eeb6fbdba5536e4574fbe96eeac312b7cb8cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sat, 23 Feb 2019 18:00:37 +0100 Subject: [PATCH] Argparse --- README.md | 87 +++++++++++++++++++------------------------------------ 1 file changed, 29 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index b056fbc..33f857e 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ sum_of_elements = sum() elementwise_sum = [sum(pair) for pair in zip(list_a, list_b)] sorted_by_second = sorted(, key=lambda el: el[1]) sorted_by_both = sorted(, key=lambda el: (el[1], el[0])) -flattened_list = list(itertools.chain.from_iterable()) +flatter_list = list(itertools.chain.from_iterable()) product_of_elems = functools.reduce(lambda out, x: out * x, ) list_of_chars = list() ``` @@ -968,6 +968,31 @@ b'.\n..\nfile1.txt\nfile2.txt\n' ``` +Command Line Arguments +---------------------- +### Basic +```python +import sys +script_name = sys.argv[0] +arguments = sys.argv[1:] +``` + +### Argparse +```python +from argparse import ArgumentParser, FileType + = ArgumentParser(description=) +.add_argument('-', '--', action='store_true') # Flag +.add_argument('-', '--', type=) # Option +.add_argument('', type=, nargs=1) # First argument +.add_argument('', type=, nargs='+') # Ramaining arguments + = .parse_args() +value = . +``` + +* **Use `'help='` for argument description.** +* **Use `'type=FileType()'` for files.** + + Path ---- ### Basic @@ -990,7 +1015,7 @@ from os import path, listdir ```python from pathlib import Path -pwd = Path() +cwd = Path() = Path('' [, '', , ...]) = / '' / '' ``` @@ -1017,37 +1042,6 @@ pwd = Path() ``` -Command Line Arguments ----------------------- -### Basic -```python -import sys -script_name = sys.argv[0] -arguments = sys.argv[1:] -``` - -### Argparse -```python -from argparse import ArgumentParser -desc = 'calculate X to the power of Y' -parser = ArgumentParser(description=desc) -group = parser.add_mutually_exclusive_group() -group.add_argument('-v', '--verbose', action='store_true') -group.add_argument('-q', '--quiet', action='store_true') -parser.add_argument('x', type=int, help='the base') -parser.add_argument('y', type=int, help='the exponent') -args = parser.parse_args() -answer = args.x ** args.y - -if args.quiet: - print(answer) -elif args.verbose: - print(f'{args.x} to the power {args.y} equals {answer}') -else: - print(f'{args.x}^{args.y} == {answer}') -``` - - JSON ---- ```python @@ -1646,8 +1640,8 @@ Audio #### Saves a list of floats with values between -1 and 1 to a WAV file: ```python import wave, struct -samples_f = [struct.pack('] -samples_b = b''.join(samples_f) +samples_l = [struct.pack('] +samples_b = b''.join(samples_l) wf = wave.open('test.wav', 'wb') wf.setnchannels(1) @@ -1677,29 +1671,6 @@ simpleaudio.play_buffer(samples_b, 1, 2, F) ``` -Url ---- -```python -from urllib.parse import quote, quote_plus, unquote, unquote_plus -``` - -### Encode -```python ->>> quote("Can't be in URL!") -'Can%27t%20be%20in%20URL%21' ->>> quote_plus("Can't be in URL!") -'Can%27t+be+in+URL%21' -``` - -### Decode -```python ->>> unquote('Can%27t+be+in+URL%21') -"Can't+be+in+URL!" ->>> unquote_plus('Can%27t+be+in+URL%21') -"Can't be in URL!" -``` - - Scraping -------- ```python