Last active
April 12, 2024 22:38
-
-
Save nntrn/9a673de29dbc3a464de94a0f327f3592 to your computer and use it in GitHub Desktop.
python oneliners
This file contains 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/env bash | |
### ALIAS ########################################################################################## | |
# Notes: | |
# Most aliases accept both standard input and command-line arguments | |
# Examples: | |
# $ echo ':hello world' | urlquote | |
# $ urlquote ':hello world' | |
echo 'hisa jlaskla' | python -c 'import sys,urllib.parse as ul; print(ul.quote_plus(ul.quote_plus(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1], "")))' | |
alias urlquote='python -c "import sys,urllib.parse as ul; print(ul.quote_plus(ul.quote_plus(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1])))"' | |
alias json2yaml='python -c "import yaml,json,sys; print(yaml.dump(json.load(sys.stdin)))"' | |
alias htmldecode='python -c "import html,sys; [print(html.unescape(l), end=\"\") for l in sys.stdin]"' | |
alias pyrandom='python -c "import random,sys; print(str(random.randint( int(sys.argv[1]) if len(sys.argv) > 2 else 0 , int(sys.argv[2]) if len(sys.argv) > 2 else int(sys.argv[1]) )))"' | |
python -c 'import sys,yaml,json; print(yaml.dump(json.loads(sys.stdin.read()), default_flow_style=False, sort_keys=False))' | |
python -c 'import sys,yaml,json; print(json.dumps(json.loads(sys.stdin.read()), sort_keys=False, indent=2))' | |
python -c 'import sys,yaml,json; print(json.dumps(yaml.load(sys.stdin.read(),Loader=yaml.FullLoader), sort_keys=False))' | |
python -c 'import sys,yaml,json; print(json.dumps(yaml.load(sys.stdin.read(),Loader=yaml.FullLoader), sort_keys=False, indent=2))' | |
python -c 'import sys,yaml,json; yaml.safe_load(sys.stdin.read())' | |
python -c 'import sys,json;json.loads(sys.stdin.read())' | |
python -c "import crypt; print(crypt.crypt(input('clear-text password: '), crypt.mksalt(crypt.METHOD_SHA512)))" | |
python -c 'import sys,urllib.request; f=urllib.request.urlopen(sys.stdin.read()); print(f.read().decode("utf-8"))' | |
### HTML ########################################################################################### | |
# unescape | |
echo '<h1 class="title">hello!<h1>' | | |
python -c 'import html,sys;[print(html.unescape(i), end="") for i in sys.stdin]' | |
# escape | |
echo '<h1 class="title">hello!<h1>' | | |
python -c 'import html,sys;[print(html.escape(i), end="") for i in sys.stdin]' | |
echo "https://nntrn.github.io/sshell" | | |
python -c 'import sys,urllib.request; f=urllib.request.urlopen(sys.stdin.read()); | |
print(f.read().decode("utf-8"))' | |
### BASE64 ######################################################################################### | |
python -m base64 -e <<<"sample string" | |
python -m base64 -d <<<"dGhpcyBpcyBlbmNvZGVkCg==" | |
# $ python -m base64 -e <<<"sample string" | |
# c2FtcGxlIHN0cmluZwo= | |
# | |
# $ python -m base64 -d <<<"dGhpcyBpcyBlbmNvZGVkCg==" | |
# sample string | |
### PRETTY PRINT ################################################################################### | |
curl -s "https://jsonplaceholder.typicode.com/posts" | | |
python -c "import sys, json; data=json.load(sys.stdin); print([d['title'] for d in data])" | |
# python -c "import sys, json; print(json.load(sys.stdin)[0])" | |
python -c "import yaml,json,sys; print(yaml.dump(json.load(sys.stdin)))" | |
# $ str='{"ice_cream_flavors": ["vanilla","chocolate","strawberry"]}' | |
# $ echo $str | python -c "import yaml,json,sys; print(yaml.dump(json.load(sys.stdin)))" | |
# ice_cream_flavors: | |
# - vanilla | |
# - chocolate | |
# - strawberry | |
### DATA ENCRYPTION & DECRYPTION ################################################################### | |
# print certificates | |
python -m certifi -c | |
python -c 'import crypt; print(crypt.crypt("This is my Password", "$1$SomeSalt$"))' | |
python -c 'import hashlib,sys; print(hashlib.sha256(sys.stdin.read().encode()).hexdigest())' | |
#--------------------------------------------------------------------------------------------------# | |
# $ python -c 'import crypt; print(crypt.crypt("This is my Password", "$1$SomeSalt$"))' | |
# $1$SomeSalt$UqddPX3r4kH3UL5jq5/ZI. | |
# | |
# $ echo -n "saltedpassword" | python -c "import sys; print(sys.stdin.read().encode())" | |
# b'saltedpassword' | |
# | |
# $ echo "..." | python -c 'import hashlib,sys; print(hashlib.sha256(sys.stdin.read().encode()).hexdigest())' | |
# a00c3b59d5e9e2d8ad78e29258e13b5cf4807919ebb5a523c199e8da1cda91b9 | |
# | |
# $ python -c 'import base64,os;print(base64.encodebytes(os.urandom(32)).decode().rstrip())' | |
# c1SIp1bkfiJHCxspNlnNN9PGifUpBUrSlwjsLhCL7dE= | |
# | |
# $ python -c "import random,string; print(''.join(random.sample(string.ascii_letters, 25)))" | |
# ZdQYvojmIHkablJBhLONUrqyE | |
#--------------------------------------------------------------------------------------------------# | |
python -c "import base64,os;print(base64.encodebytes(os.urandom(32)).decode().rstrip())" | |
python -c "import random,string; print(''.join(random.sample(string.ascii_letters, 25)))" | |
### CONVERTING DATA TYPES | |
# YAML TO JSON | |
alias yaml2json='python -c "import sys,yaml,json; print(json.dumps(yaml.safe_load( open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin )))"' | |
# USAGE: | |
# $ cat file.yml | yaml2json | |
# or | |
# $ yaml2json file.yml | |
#--------------------------------------------------------------------------------------------------# | |
# JSON TO YAML | |
#--------------------------------------------------------------------------------------------------# | |
# Options: | |
# default_style=None | |
# default_flow_style=False | |
# canonical=None | |
# indent=None | |
# width=None | |
# allow_unicode=None | |
# line_break=None | |
# encoding=None | |
# explicit_start=None | |
# explicit_end=None | |
# version=None | |
# tags=None | |
# sort_keys=True | |
# | |
# # Flow style: | |
# !!python/object/apply:models.Person [John, Doe] | |
# | |
# # Block style: | |
# !!python/object/apply:models.Person | |
# - John | |
# - Doe | |
cat file.json | | |
python -c 'import sys,yaml,json; print(yaml.dump( json.loads(sys.stdin.read()) ))' | |
# or with options | |
cat file.json | | |
python -c 'import sys,yaml,json; print(yaml.dump(json.loads(sys.stdin.read()), <OPTIONS-HERE> ))' | |
# $ python -c "import yaml; print(yaml.dump([1,2,3], default_flow_style=False ))" | |
# - 1 | |
# - 2 | |
# - 3 | |
# | |
# $ python -c "import yaml; print(yaml.dump([1,2,3], default_flow_style=True ))" | |
# [1, 2, 3] | |
# | |
# $ python -c "import yaml; print(yaml.dump([1,2,3], canonical=True ))" | |
# --- | |
# !!seq [ | |
# !!int "1", | |
# !!int "2", | |
# !!int "3", | |
# ] | |
### SYSTEM ######################################################################################### | |
python -m site | |
python -m site --user-site | |
python -m profile <(echo 'import this') | |
python -c "import site; print(site.getsitepackages())" | |
# $ python -m site | |
# sys.path = [ | |
# '/Users/annie/Library/Python/3.9', | |
# '/Library/Frameworks/Python.framework/Versions/3.9/lib/python39.zip', | |
# '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9', | |
# '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload', | |
# '/Users/annie/Library/Python/3.9/lib/python/site-packages', | |
# '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages', | |
# ] | |
# USER_BASE: '/Users/annie/Library/Python/3.9' (exists) | |
# USER_SITE: '/Users/annie/Library/Python/3.9/lib/python/site-packages' (exists) | |
# ENABLE_USER_SITE: True | |
# | |
# $ python -m site --user-site | |
# /Users/annie/Library/Python/3.9/lib/python/site-packages | |
# | |
# $ python -c "import site; print(site.getsitepackages())" | |
# ['/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages'] | |
# $ python -m profile <(echo 'import this' ) | |
# 1253 function calls (1252 primitive calls) in 0.033 seconds | |
# | |
# Ordered by: standard name | |
# | |
# ncalls tottime percall cumtime percall filename:lineno(function) | |
# 1 0.000 0.000 0.006 0.006 11:1(<module>) | |
# 3 0.000 0.000 0.000 0.000 :0(__exit__) | |
# 1 0.000 0.000 0.000 0.000 :0(_fix_co_filename) | |
# 5 0.000 0.000 0.000 0.000 :0(acquire_lock) | |
# 2 0.000 0.000 0.000 0.000 :0(allocate_lock) | |
# 104 0.000 0.000 0.000 0.000 :0(chr) | |
# 1 0.000 0.000 0.000 0.000 :0(endswith) | |
# 2/1 0.000 0.000 0.006 0.006 :0(exec) | |
# 3 0.000 0.000 0.000 0.000 :0(extend) | |
# 3 0.000 0.000 0.000 0.000 :0(from_bytes) | |
# 3 0.000 0.000 0.000 0.000 :0(fspath) | |
### COMPRESSION #################################################################################### | |
DIR=directory_name | |
FILE=plain.txt | |
python -m zipfile -c $DIR.zip $DIR | |
python -m tarfile -c $FILE.tar $FILE | |
#################################################################################################### | |
python -m pygments -g file.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment