Python je švýcarský nůž programátora. Tady je 25 one-linerů pro každodenní práci.
HTTP a síť¶
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
Generátory¶
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)))”
Soubory¶
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’)”
Kryptografie¶
python3 -c “import hashlib,sys; print(hashlib.sha256(sys.stdin.buffer.read()).hexdigest())”
Datum a čas¶
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 čeština’))”
Base64¶
python3 -c “import base64,sys; print(base64.b64encode(sys.stdin.buffer.read()).decode())”
Matematika¶
python3 -c “import math; print(math.factorial(20))”
python3 -c “a,b=0,1;exec(‘print(a);a,b=b,a+b;’*20)”
Tip¶
Uložte nejpoužívanější one-linery jako shell aliasy. Python je vždy k dispozici.