Python: 25 One-Liners for Productivity¶
Python is the programmer’s Swiss army knife. Here are 25 one-liners for everyday work.
HTTP and Network¶
python3 -m http.server 8080 python3 -c “import socket; print(socket.gethostbyname(socket.gethostname()))”
JSON¶
echo ‘{“a”:1}’ | python3 -m json.tool python3 -c “import csv,json,sys; print(json.dumps(list(csv.DictReader(sys.stdin))))” < data.csv
Generators¶
python3 -c “import uuid; print(uuid.uuid4())” python3 -c “import secrets,string; print(‘’.join(secrets.choice(string.ascii_letters+string.digits) for _ in range(32)))”
Files¶
python3 -c “import os; print(sum(os.path.getsize(os.path.join(d,f)) for d,_,files in os.walk(‘.’) for f in files)/1024**2, ‘MB’)”
Cryptography¶
python3 -c “import hashlib,sys; print(hashlib.sha256(sys.stdin.buffer.read()).hexdigest())”
Date and Time¶
python3 -c “from datetime import datetime; print(datetime.fromtimestamp(1707900000))” python3 -c “import time; print(int(time.time()))”
Web¶
python3 -c “from urllib.parse import quote; print(quote(‘hello world’))”
Base64¶
python3 -c “import base64,sys; print(base64.b64encode(sys.stdin.buffer.read()).decode())”
Math¶
python3 -c “import math; print(math.factorial(20))” python3 -c “a,b=0,1;exec(‘print(a);a,b=b,a+b;’*20)”
Tip¶
Save your most-used one-liners as shell aliases. Python is always available.