Last active
August 5, 2019 18:45
-
-
Save Subangkar/b1e42ee80d2f7fdf99f4557c0847a793 to your computer and use it in GitHub Desktop.
Useful python snippets like using shell, command line args
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# *list expands the list elements as positional arguments print(*[1,2,3]) = print(1,2,3) | |
l1 = [4, 7, 8] | |
l2 = ["a", "b", "c"] | |
zl = list(zip(l1, l2)) | |
print(zl) | |
l1_rec, l2_rec = zip(*zl) | |
print(l1_rec) | |
print(l2_rec) | |
print(dict(zip(l1, l2))) | |
# val_last has to be kwarg | |
# val can't be kwarg as it is just before *arg | |
def func(val, *val_args, val_last, **key_args): | |
return (val, val_args, val_last, key_args) | |
print(func(10, 2, 3, id=1, val_last=5, key=3)) | |
print(func(2, 3, 5, val_last=7, id=1, key=3, dummy=400)) | |
first, *mid, last = [1, 2, 3, 4] | |
print(first, mid, last) | |
def foo(x, y, z): | |
print("x=" + str(x)) | |
print("y=" + str(y)) | |
print("z=" + str(z)) | |
mydict = {"x": 1, "z": 3, "y": 2} | |
foo(**mydict) # maps dict to paramaters |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from argparse import ArgumentParser | |
parser = ArgumentParser() | |
parser.add_argument("-f", "--file", dest="filename", | |
help="write report to FILE", metavar="FILE") | |
parser.add_argument("-o", "--option", dest="option", | |
help="option value") | |
parser.add_argument("-q", "--quiet", | |
action="store_false", dest="verbose", default=True, | |
help="don't print status messages to stdout") | |
args = parser.parse_args() | |
print(args.filename) | |
print(args.option) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import sys | |
print('Number of arguments:', len(sys.argv), 'arguments.') | |
print('Argument List:', str(sys.argv)) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print("------") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from subprocess import Popen, PIPE | |
command = "find . -name '*.py' | sed 's/\.\///' | sort" | |
for filename in Popen(command, shell=True, stdout=PIPE).stdout: | |
print(filename.decode("utf-8").strip()) # default is in utf-8 encoded, strip to remove trailing newlines | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment