Skip to content

Instantly share code, notes, and snippets.

@nathants nathants/pys.sh
Last active Jan 13, 2020

Embed
What would you like to do?
3 bash functions for quick python oneliners
# exec a python snippet and print the result, stdin is not used
# >> p '60*60*24'
# 86400
p() {
python -c "
$PY_IMPORTS
val = $@
if val:
if isinstance(val, list):
print(' '.join(map(str, val)))
else:
print(val)
"
}
# exec a python snippet and print the result, stdin is read (not streamed per line) and assigned to "i"
# >> echo -e 'a\nb\nc' | py 'random.choice(i.splitlines())'
# c
py() {
python -c "
$PY_IMPORTS
i = sys.stdin.read()
val = $@
if val:
if isinstance(val, list):
print('\n'.join(map(str, val)))
else:
print(val)
"
}
# exec a python snippet and print the result, stdin is streamed per line and assigned to "i"
# >> echo -e 'a\nb\nc\n' | pys 'f"{i.upper()}:{j}"'
# A:0
# B:1
# C:2
pys() {
python -c "
$PY_IMPORTS
j = 0
while True:
i = sys.stdin.readline().strip()
if not i:
break
val = $@
if val:
if isinstance(val, list):
print(' '.join(map(str, val)))
else:
print(val)
j += 1
"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.