misc:code_snippets:python
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| misc:code_snippets:python [2016/09/29 16:30] – extended colored output example, print_error included sascha | misc:code_snippets:python [2017/04/11 15:13] (current) – Fix sascha | ||
|---|---|---|---|
| Line 21: | Line 21: | ||
| raise e | raise e | ||
| sys.exit(1) | sys.exit(1) | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Liste zeilenweise ausgeben ==== | ||
| + | |||
| + | <code python> | ||
| + | print(*myList, | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Erstes Listenelement oder None erhalten ==== | ||
| + | |||
| + | <code python> | ||
| + | next(iter(your_list or []), None) | ||
| </ | </ | ||
| Line 48: | Line 62: | ||
| else: | else: | ||
| print(' | print(' | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Temporäres Verzeichnis als Context Manager verwenden ==== | ||
| + | |||
| + | <code python> | ||
| + | import tempfile | ||
| + | |||
| + | with tempfile.TemporaryDirectory() as tmp_dir: | ||
| + | pass # do stuff | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Mittels Pipe übergebene Daten lesen ==== | ||
| + | |||
| + | <code python> | ||
| + | import sys | ||
| + | |||
| + | # ensure the script only reads piped data and doesn' | ||
| + | if not sys.stdin.isatty(): | ||
| + | lst = [l.strip() for l in sys.stdin.readlines() if not l.startswith('#' | ||
| + | print(lst) | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Dateien aus einem Verzeichnis erhalten oder aus Datei lesen ==== | ||
| + | |||
| + | <code python> | ||
| + | import os | ||
| + | from os.path import expanduser, isdir, isfile, abspath, join as pjoin | ||
| + | |||
| + | def get_files(path=/ | ||
| + | files = [] | ||
| + | path = expanduser(path) | ||
| + | |||
| + | if isdir(path): | ||
| + | files = [pjoin(abspath(path), | ||
| + | | ||
| + | elif isfile(path): | ||
| + | with open(path, ' | ||
| + | files = [l.strip() for l in lst.readlines() if not l.startswith('#' | ||
| + | |||
| + | return files | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Anführungszeichen um einen String entfernen ==== | ||
| + | |||
| + | <code python> | ||
| + | def unquote(string): | ||
| + | """ | ||
| + | if (string[0] == string[-1]) and string.startswith(("'", | ||
| + | return string[1: | ||
| + | return string | ||
| + | </ | ||
| + | |||
| + | Ohne Überprüfen von passenden Angührungszeichen: | ||
| + | |||
| + | <code python> | ||
| + | return re.sub(r' | ||
| </ | </ | ||
| Line 200: | Line 274: | ||
| progress_simple(total) | progress_simple(total) | ||
| progress(total, | progress(total, | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Verzeichnis temporär wechseln (Exception-safe) ==== | ||
| + | |||
| + | <code python cd_exception_safe.py> | ||
| + | from contextlib import contextmanager | ||
| + | import os | ||
| + | |||
| + | @contextmanager | ||
| + | def cd(newdir): | ||
| + | prevdir = os.getcwd() | ||
| + | os.chdir(os.path.expanduser(newdir)) | ||
| + | try: | ||
| + | yield | ||
| + | finally: | ||
| + | os.chdir(prevdir) | ||
| + | |||
| + | # cd using context manager and decorator | ||
| + | # directory is reverted even after an exception is thrown | ||
| + | |||
| + | os.chdir('/ | ||
| + | |||
| + | with cd('/ | ||
| + | # ... | ||
| + | raise Exception(" | ||
| + | # Directory is now back to '/ | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Liste mit Inhalt einer anderen Liste filtern ==== | ||
| + | |||
| + | <code python> | ||
| + | # filter a list by checking if substring of another list is contained in entries of the list to be filtered | ||
| + | # here flags is some list which contain entries from the list regex --> filter matching regex from list flags | ||
| + | flags = ['" | ||
| + | regex = [unquote(flag) for flag in flags if unquote(flag).startswith(' | ||
| + | flags = [flag for flag in flags if not any(flag for reg in regex if reg in flag)] | ||
| </ | </ | ||
misc/code_snippets/python.1475166640.txt.gz · Last modified: by sascha
