ï¼spaceaero2 [CC BY 3.0], ã¦ã£ãã¡ãã£ã¢ã»ã³ã¢ã³ãºããï¼
ããã«ã¡ã¯ãã¨ã ã¹ãªã¼ã»ã¨ã³ã¸ãã¢ãªã³ã°Gã»åºç¤éçºãã¼ã å°æ¬ã§ãã
WEBãµã¤ã㯠RailsãSpringãªã©ã®ãæ¬ä½é¨åãã ãã§ã¯å®çµãã¾ãããã¬ãã¼ãä½æã»ãã¼ã¿æ´æ°ãªã©ã®ç´°ããå¦çãå¿ è¦ã§ãã
éå»ã«ã¯ããããç¨éã«ã¯Bashããã使ããã¾ãããããããBashã¯è½ã¨ãç©´ãå¤ãã£ãããã¯ã©ã¹ãªã©ã®æ½è±¡åæ©è½ããªãã£ãããã¦ãè¦æ¨¡ã大ãããªãã¨è¾ããªãã¾ãã
ããã§ãBashã®ä»£æ¿åè£ã«æããã®ãPythonã§ããã¨ã ã¹ãªã¼ã§ããã¤ã¦ã¯Bashã使ã£ã¦ãã¾ããããç¾å¨ã¯æ°è¦æ¡ä»¶ã«ã¯Pythonãæ¨å¥¨ãã¦ãã¾ãã
ããããå®éã«Pythonã§æ¸ãç´ããã¨ããã¨ç´é¢ããã®ãã
ãã®å¦çãPythonã§ã©ãæ¸ãã°ããã®ãåãããªã Bashãªãï¼è¡ã§æ¸ããã®ã«ãï¼ï¼ ããBashã§ããã¡ãã
ã¨ããåé¡ã§ãã
ç¹ã«ãã¡ã¤ã«ã¾ããã»å¤é¨ã³ãã³ãã¾ããã¯ãBashã§ããæ±ãä¸ã«ãPythonã®é²åã«ãã£ã¦éå»ã«å¦ãã æ¹æ³ã Obsolete ã«ãªã£ã¦ããæ··ä¹±ããã¡ã§ããããã§ãããããå¦çãææ°ã®Pythonã§å®è£ ããæ¹æ³ã®è³æãä½æãã¾ããã®ã§ãç´¹ä»ãã¾ãã
- ãã®å¦çãPythonã§ã©ãæ¸ãï¼
- æ¨æºåºåã»æ¨æºã¨ã©ã¼åºå
- ãã¡ã¤ã«é¢ä¿
- å¤é¨ã³ãã³ã
- åç´ã«å®è¡ãã
- å¤é¨ã®ã³ãã³ããå®è¡ããæ¨æºåºåãåãåã:
- ç°å¢å¤æ°ãã«ã¬ã³ããã£ã¬ã¯ããªãå¤æ´ãã
- ãªãã¤ã¬ã¯ãã使ã
- ãã¤ãã使ã
- spawn â wait ï¼å¤é¨ã³ãã³ããèµ·åããçµäºãå¾ ã¤ï¼
- ã·ã§ã«ãå®è¡ãããå±éºï¼ï¼ã
- æå»é¢ä¿
- æååé¢ä¿
- ã³ãã³ãã©ã¤ã³å¼æ°
- çµäºæã®å¦çï¼ã·ã°ãã«ãtrapãã
- HTTPãªã¯ã¨ã¹ãï¼curl ã wget ã®ä»£æ¿ï¼
- ã¨ã³ã¸ãã¢ãåéãã¦ãã¾ãï¼
ãã®å¦çãPythonã§ã©ãæ¸ãï¼
ã¹ã¯ãªããç³»ã®ããããå¦çãææ°ã®Pythonã§å®è£ ããæ¹æ³ã説æãã¾ããä¸é¨ã対æ¯ã®ããã«Bashã®ã³ã¼ããæ¸ãã¦ãã¾ãã
ãªãããã®èª¬æã§ã¯ã§ããã ãæ¨æºã©ã¤ãã©ãªã®ç¯å²ã§æ¸ãã¦ãã¾ãã æ¨æºã©ã¤ãã©ãªã§ãæ©è½çã«ååãªãã¨ãå¤ãããã§ãããµã¼ããã¼ãã£ã»ã©ã¤ãã©ãªã使ããã¨ããã¨ãã©ã¤ãã©ãªã®é¸å®ãã©ã¤ãã©ãªãã¤ã³ã¹ãã¼ã«ããæé ãã¢ãããã¼ãããã¨ãã®å½±é¿ããªã©èãããã¨ãå¢ãã¦ãã¾ãã¾ãã
ã¾ããããããå¦çããã®è³æã«æ¸ãã¦ããªãã¨ãã¯ãã¾ãå ¬å¼ããã¥ã¡ã³ããå½ãã£ã¦ãã ããã大æµã®ã¿ã¹ã¯ã¯ãããç¨ã®æ¨æºã©ã¤ãã©ãªãè¦ã¤ããã¯ãã§ãã
æ¨æºåºåã»æ¨æºã¨ã©ã¼åºå
echo "HELLO" # æ¨æºåºåã«ã¡ãã»ã¼ã¸ echo 'ERROR!' >&2 # æ¨æºã¨ã©ã¼ã«ã¡ãã»ã¼ã¸
ã©ã¡ããçµã¿è¾¼ã¿ã®printé¢æ°ã使ãã¾ã:
print("HELLO") # æ¨æºåºåã«ã¡ãã»ã¼ã¸ import sys print("ERROR!", file=sys.stderr) # æ¨æºã¨ã©ã¼ã«ã¡ãã»ã¼ã¸
ãã ããããã§ã¡ãã»ã¼ã¸ã®ç¨®é¡ãèæ
®ãã¦ãã ãããé²æãå¦ççµæãªã©ã®äººéãèªãããã®ã¡ãã»ã¼ã¸ã¯ print
ã§ã¯ãªãloggingã§åºåãã¦ãããæ¹ãããããããã¾ãããå¾ã
ãæå»ããã¡ã¤ã«åã追å ãããããã°ã¬ãã«ã®æå®ãã§ããããã§ãã
import logging logger = logging.getLogger(__name__) logger.error("ERROR!") # => ERROR!
åæè¨å®ã§ã¯warn
以ä¸ã®ãã°ããã®ã¾ã¾ï¼æå»ã»ãã¡ã¤ã«åãä»å ããï¼æ¨æºã¨ã©ã¼åºåã«åºã¾ããã¤ã¾ããprint(x, file=sys.stderr)
ã¨åçã§ãã
ã¾ããlogging.basicConfig
ã§ãã°ã¬ãã«ã»ãã©ã¼ããããå¤æ´ã§ãã¾ãã
logging.basicConfig( level=logging.INFO, format="%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s", )
ãã¡ã¤ã«é¢ä¿
ãã¹ã®æä½
Bashã§ã¯ãã¹ã¯åºæ¬çã«ã¯ã"/" åºåãã®æååãã§ãã
name="$(basename "/foo/bar/baz.txt")" # => baz.txt ãã¡ã¤ã«å name="$(dirname "/foo/bar/baz.txt")" # => /foo/bar/ 親ãã£ã¬ã¯ããªãã¹ fullpath="/foo/bar/${name}" # => /foo/bar/baz.txt ãã¹ã®é£çµ
Pythonã§ããã¹ãæååã¨ãã¦æ±ãã¢ã¸ã¥ã¼ã«ã¯ãã(os.path)ã®ã§ãããpathlibã使ãæ¹ãã¢ãã³ã§ãã
pathlib
ã§ã¯ãä¸åº¦æååãPath
ã§ã©ãããã¦æä½ãã¾ãã
import pathlib p = pathlib.Path("/foo/bar/baz.txt") # æååãPathã§ã©ãããå®éã«ã¯Pathã§ã¯ãªããOSæ¯ã®ãµãã¯ã©ã¹ã§ã©ããããã p.name # => baz.txt ãã¡ã¤ã«å p.parent # => PosixPath('/foo/bar') 親ãã£ã¬ã¯ããªãã¹ dirpath = pathlib.Path("/foo/bar/") fullpath = dirpath / "baz.txt" # => PosixPath('/foo/bar/baz.txt') ãã¹ã®é£çµ
pathlib.Path
㯠str
ã§æååã«æ»ãã¾ããã¾ããsubprocess.run
ãªã©ã«ãã®ã¾ã¾æ¸¡ãã¾ãã
import pathlib import subprocess fullpath = pathlib.Path("/foo/bar/baz.txt") str(fullpath) # '/foo/bar/baz.txt' æååã«æ»ã subprocess.run(['cat', fullpath]) # æ¨æºã©ã¤ãã©ãªã«ãã®ã¾ã¾æ¸¡ãã
ãã«ããç°å¢å¤æ°ãå«ã¾ãããã¹ãæ±ã
Pythonã®pathlibã§ããããã¼ã ãã£ã¬ã¯ããªã表ã ~
ãç°å¢å¤æ° $var
ã¯æ示çã«å±éããªããã°ãªãã¾ããï¼Unixã§ã¯ "~" ã¨ã "$var" ã¨ããååã®ãã£ã¬ã¯ããªãããã®æ°ã«ãªãã°ä½ããã®ã§ï¼ã
import pathlib bad = pathlib.Path("~/Donwloads/report.txt") # ~ ã¯èªåå±éãããªãï¼ f = pathlib.Path("~/Donwloads/report.txt").expanduser() g = pathlib.Path("${WORK_DIR}/input.txt").expandvars()
ãã¡ã¤ã«ã®èªã¿æ¸ã
Pythonã®åæ©ãªã®ã§ãããã¦è§£èª¬ã¯ä¸è¦ã§ãããã
import pathlib path = pathlib.Path('foo.txt') with open(path, 'r', encoding="utf-8") as f: for line in f: # line ãå¦ç with open(path, 'w', encoding="utf-8") as f: print("å 容", file=f) with path.open('r') as f: # Pathã®ã¡ã½ããã使ã£ã¦éããã¨ãã§ããã # ...
ãã ããencoding
ã§æåã³ã¼ããæå®ãããã¨ã¯å¿ããªãã§ãã ãããencoding
ãçç¥ãããã¨ã¯åºæ¥ã¾ãããç°å¢å¤æ°ãªã©ã«ãã£ã¦ã¯å¥ã®æåã³ã¼ãã«ãªã£ã¦ãã¾ãããããã¾ããã
è¡æ°ãæ°ããï¼wc -l
ï¼
ãã®ããã«ãsum
ã¨å
å
表è¨ãçµã¿åãããã®ãä¸çªå¹çãè¯ãããã§ãã
with path.open('rb') as f: count = sum(1 for line in f)
ãªããopen
ã«ã¯ 'rb'
ï¼ãã¤ããªã¢ã¼ãã§èªã¿è¾¼ã¿ï¼ãæå®ãã¦ãã ããã
'r'
ï¼ããã¹ãã¢ã¼ãã§èªã¿è¾¼ã¿ï¼ã ã¨ããã¡ã¤ã«ãæåååã«ãã³ã¼ãããï¼ç¡é§ãªï¼å¦çãçºçããã®ã§ã¨ã¦ãé
ããªãã¾ãã
ãã¡ã¤ã«ã®åæ
pathlib.Path.glob
ã§ã¯ã¯ã¤ã«ãã«ã¼ãã使ãã¾ãã
import pathlib dir = pathlib.Path('/tmp') for file in dir.glob('tmp.*'): # ãã¡ã¤ã«ãå¦çãã # fileã¯æååã§ã¯ãªããpathlib.Pathã§ãããã¨ã«æ³¨æ
ãã¡ã¤ã«ã®æ å ±ï¼åå¨ç¢ºèªã»ä½ææ¥æï¼
ãã®ä»ããã¡ã¤ã«å¨ãã®è²ã
ãªæ
å ±åå¾ããpathlib.Path
ã®ã¡ã½ããã§ã§ãã¾ãã
import pathlib f = pathlib.Path('/bin/bash') f.exists() # åå¨ç¢ºèª f.is_file() # ãã¡ã¤ã«ï¼ f.is_dir() # ãã£ã¬ã¯ããªï¼ f.stat().st_ctime # ä½ææ¥æ f.stat().st_mtime # æ´æ°æ¥æ f.stat().st_atime # ã¢ã¯ã»ã¹æ¥æ
移åã»åé¤
ãã¡ã¤ã«ã®ç§»åã»åé¤ãpathlib.Path
ã®ã¡ã½ããã§ã§ãã¾ãã
import pathlib path_from = pathlib.Path('.bash_history') path_to = pathlib.Path('/tmp/.bash_history') path_from.rename(path_to) # 移å path_from.unlink() # åé¤
ã³ãã¼
ãã¡ã¤ã«ã®ã³ãã¼ã¯ï¼ãªããï¼pathlibããã¯ã§ãã¾ãããshutilã®é¢æ°ããè¡ãã¾ãã
import shutil import pathlib path_from = pathlib.Path('.bash_history') path_to = pathlib.Path('/tmp/.bash_history') shutil.copy2(path_from, path_to) # ã³ãã¼
ãªããshutil ã os ã«ã¯ãã¡ã¤ã«é¢ä¿ã®é¢æ°ãå¤æ°å®ç¾©ããã¦ãã¾ãããã©ã®é¢æ°ãå¼æ°ã« pathlib.Path ã渡ããã¨ãã§ãã¾ãã
å¤é¨ã³ãã³ã
subprocess.run ã使ãã¾ãã
Pythonã«ã¯å¤é¨ã³ãã³ãå®è¡æ©è½ãæ°æ§ããããããã®ã§ããã3.7ããç»å ´ããsubprocess.run
ã¯æ±ºå®çã§ããsubprocess.run
1ã¤ã§å
¨ã¦ã®ã·ãã¥ã¨ã¼ã·ã§ã³ã«å¯¾å¿ã§ãã¾ãã
ãªããä¸å¹¸ã«ãã¦3.6以åã®Pythonã使ãå ´å㯠subprocess.call
ã subprocess.check_call
ã§ä»£ç¨ãã¾ãã
åç´ã«å®è¡ãã
import subprocess subprocess.run(['sl', '-h'], check=True)
ãªããåºæ¬çã«ã¯å¸¸ã«check=True
ãæå® ãã¾ããçµäºã¹ãã¼ã¿ã¹ã non-0 ã®å ´åã«ä¾å¤ãçºçããã¹ã¯ãªããå
¨ä½ãã¨ã©ã¼çµäºããããã§ãï¼Bash㧠set -e
ã¨ããæã®ããã«ï¼ã
ä¸æ¹ãcheck=False
ãæå®ããï¼ã¾ãã¯check
ãæå®ããªãï¼ã¨çµäºã³ã¼ãã .returncode
ã§åå¾ã§ãã¾ãã
import subprocess r = subprocess.run(['false'], check=False) r.returncode # => 3
å¤é¨ã®ã³ãã³ããå®è¡ããæ¨æºåºåãåãåã:
stdout=subprocess.PIPE
ãæå®ããã¨æ¨æºåºåããã£ããã£ã§ããåæ§ã«æ¨æºã¨ã©ã¼ãstderr
ã§ãã£ããã£ã§ãã¾ãã
import subprocess r = subprocess.run(['echo', 'ä¸ç'], check=True, stdout=subprocess.PIPE) r.stdout # => b'\xe4\xb8\x96\xe7\x95\x8c\n' 'ä¸ç\n'ãUTF-8ã§ã¨ã³ã³ã¼ããããã® r.stdout.decode(sys.getfilesystemencoding()) # => 'ä¸ç\n'
ãªãã.stdout
㯠æååã§ã¯ãªããã¤ãåã§ãããã¨ã«æ³¨æãã¦ãã ããã
æååã¨ãã¤ãåã¯æ··ãããã¨ã¯ã§ãã¾ãããæ示çã«ãã³ã¼ããã¦ãã ããã
ãã®éãããã¦ãã®å ´åã¯ãã¤ãåã®ã¨ã³ã³ã¼ãã¯UTF-8ã«ãªã£ã¦ãã¾ããããããå¤ãç°å¢ãªã©ã§ã¯UTF-8ã§ã¯ãªããã¨ãããã決ãæã¡ã¯ã§ãã¾ãããæåã³ã¼ã㯠sys.getfilesystemencoding()
ã§åå¾ãã¦ãã ããã
ç°å¢å¤æ°ãã«ã¬ã³ããã£ã¬ã¯ããªãå¤æ´ãã
追å ã®å¼æ°ã§ç°å¢å¤æ°ãã«ã¬ã³ããã£ã¬ã¯ããªãå¤æ´ãããã¨ãã§ãã¾ãã
env = dict(os.environ) # Pythonã¹ã¯ãªããã®ç°å¢å¤æ°ãã³ãã¼ env['DB_HOST'] = 'localhost' # ç°å¢å¤æ°ãå¤æ´ cwd = pathlib.Path('/') subprocess.run(['setup.sh'], cwd=cwd, env=env)
ãªãã¤ã¬ã¯ãã使ã
subprocess.run
ã®stdin
, stdout
, stderr
ã«ã¯ãã¡ã¤ã«ãªãã¸ã§ã¯ãã渡ããã¨ãã§ãã¾ãã
import subprocess import os.path fi = open(os.path.expanduser('~/.bash_history')) fo = open('p.txt', 'wb') subprocess.run(['grep', 'python[23]'], stdin=fi, stdout=fo) # p.txt ã«æ¤ç´¢çµæãåºåããã
ãã¤ãã使ã
cat .bash_history | grep python
subprocess.Popen
ã使ãã¾ãã.Popen
㯠.run
ã¨ã»ã¼åãå¼æ°ãåãåãã¾ãããå®è¡ãå¾
ã¡ã¾ããã
p1 = subprocess.Popen(["cat", '.bash_history'], stdout=subprocess.PIPE) p2 = subprocess.Popen(["grep", "python"], stdin=p1.stdout, stdout=subprocess.PIPE) p1.stdout.close() output = p2.communicate()[0] # history ãã 'python' ãå«ãè¡ãæ¤ç´¢ããçµæ p2.returncode # grep ã®çµäºã³ã¼ã
spawn â wait ï¼å¤é¨ã³ãã³ããèµ·åããçµäºãå¾ ã¤ï¼
éãå¦çã並åå®è¡ããã¨ããªã©ãå¤é¨ã³ãã³ããè¤æ°èµ·åï¼spawnï¼ããæå¾ã«å ¨ã¦ã®ã³ãã³ãã®çµäºãå¾ ã¤ãã¨ãããã¾ãã
/path/to/heavy-sub-process1 & pid1=$! /path/to/heavy-sub-process2 & pid2=$! wait $pid1 wait $pid2
ãã®å ´åã subprocess.Popen
ã使ãã¾ãã
import subprocess p1 = subprocess.Popen(['/path/to/heavy-sub-process1']) p2 = subprocess.Popen(['/path/to/heavy-sub-process2']) p1.wait() p2.wait()
ãªããspawnããå¤é¨ã³ãã³ãã®åºåãåãåã£ãããå¤é¨ã³ãã³ãã®å®è¡ä¸ã«å¥ã®å¦çãå®è¡ããããªãããããã¾ãããããã¯éåæå¦çã®ä¸çã«è¶³ãè¸ã¿è¾¼ãäºã«ãªãã¾ãã®ã§ãsubprocess
ã§ã¯ãªãasyncio
ã使ã£ã¦ãã ããã
ã·ã§ã«ãå®è¡ãããå±éºï¼ï¼ã
subprocess.run
ã¯å¤é¨ã³ãã³ããã·ã§ã«ãä»ããã«å®è¡ãã¾ãã
åã«å¼æ°ã«ãã¤ãããªãã¤ã¬ã¯ããæ¸ãã¦ãåä½ãã¾ããã
subprocess.run('echo Hello > /dev/null') # => FileNotFoundError: [Errno 2] No such file or directory: 'echo Hello > /dev/null'
ãã㧠shell=True
ãæå®ããã¨ãå¼æ°ãã·ã§ã«ãä»ãã¦å®è¡ãããã¨ãã§ãã¾ãã
subprocess.run('echo Hello > /dev/null', shell=True)
ããããshell=True
ã¯ï¼¯ï¼³ã³ãã³ãã¤ã³ã¸ã§ã¯ã·ã§ã³ã®åå ã«ãªãå±éºã§ãã
ã¾ãããã¨ãå®éã«ã¯èå¼±æ§ã«ã¯ãªããªãï¼ã¦ã¼ã¶ã¼ã®å
¥åã.run
ã«æ¸¡ãã¦ããªãï¼ã¨ãã¦ããããªãã®ã¹ã¯ãªãããå¼ãç¶ãã 人ãéçãã§ãã¯ãã¼ã«ã«ã¯åãããªãããããã¾ãããããé¿ããæ¹ãç¡é£ã§ãã
æå»é¢ä¿
Unixæå» <=> æåå表è¨ã®æå»ã®å¤æã¨ãçµéç§æ°ã®ç®åºã¯ããè¡ãã¨æãã¾ãã
date +%s # => 1540277405 ç¾å¨ã®Unixæå» date -d @1540277405 +%FT%T # => 2018-10-23T15:50:05 Unixæå»ãæååã«å¤æ start="$(date +%s)" # ä½ãæéããããå¦çããã echo "$(( $(date +%s) - $start ))" # => 42 ããã£ãç§æ°
Pythonã§ã¯datetimeã使ãã¾ãã datetime - datetime ã¯ãç§ã§ãããªç§ã§ããªã timedelta ã§ãããã¨ã«æ³¨æãã¦ãã ããã
from datetime import datetime, timedelta # datetimeã¯æ¥æããtimedeltaã¯çµéæéã表ã epoch = datetime.now().timestamp() # => 1540277405.379158 ç¾å¨ã®Unixæå»ï¼å°æ°ï¼ datetime.fromtimestamp(1540277405).strftime('%FT%T') # => '2018-10-23T15:50:05' start = datetime.now() # ä½ãæéããããå¦çããã duration = datetime.now() - start # datetime - datetime 㯠timedelta print(duration / timedelta(seconds=1)) # çµéæéãæ°å¤åã«ããã«ã¯ãå¥ã® timedeltaã§å²ã # => 42.680422 ããã£ãç§æ°ï¼å°æ°ï¼ print(duration.total_seconds()) # ããã§ãOK # => 42.680422 ããã£ãç§æ°ï¼å°æ°ï¼
ä»ã«ãæ§ã ãªæ©è½ãããã¾ãã詳ããã¯ããã¥ã¡ã³ããåç §ãã¦ãã ããã
æååé¢ä¿
æååã¸ã®å¼åãè¾¼ã¿
message="ä¸ç!" echo "Hello ${message}" # => Hello ä¸ç!
ä»ã®Pythonã§ã¯ f-string ã使ã£ã¦ãã»ã¨ãã©åããã¨ãã§ãã¾ãã
message='ä¸ç!' print(f"Hello {message}") # => Hello ä¸ç!
ããã« f-string å
ã® {...}
ã«ã¯å¤æ°ã ãã§ãªããå¼ã§ããã°ã©ããªãã®ãæ¸ããã¨ãã§ãã¾ãã
print(f"1 + 2 = {1 + 2}") # => 1 + 2 = 3
ãã¢ããã¥ã¡ã³ã
cat <<EOS > report.txt ã¬ãã¼ã æ¥ä»: ${date} EOS
Python ã«ã¯ãã¢ããã¥ã¡ã³ãã¯ããã¾ããããè¤æ°è¡æååï¼3éã®å¼ç¨ç¬¦ã§å²ãï¼ãããã¾ãã
ã¾ããtextwrap.dedent
ã¯è¡é ã®ç©ºç½ãåé¤ãã¦ãããã®ã§ãæååã®ä¸èº«ãã¤ã³ãã³ããã¦æ¸ããã¨ãã§ãã¾ãã
import textwrap report = textwrap.dedent(f""" ã¬ãã¼ã æ¥ä»: {date} """)
ã³ãã³ãã©ã¤ã³å¼æ°
ã³ãã³ãã©ã¤ã³å¼æ°ã¯ãsys.argv
ã§ãªã¹ãã¨ãã¦åãåãã¾ãã
import sys sys.argv # => ['a.py', 'input.txt', '-o', 'output.txt']
ã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ãå¦çãããã¨ã㯠argparse ã使ãã®ãæ¨æºã§ããgnu-getopt ã©ã¤ã¯ãª getopt ãããã¾ãããå¾ã ãèãã¦argparseã«ãã¦ããã¾ãããã
argparseã®ä½¿ç¨ä¾ï¼å ¬å¼ããã¥ã¡ã³ãããï¼:
import argparse parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator') parser.add_argument('--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers (default: find the max)') args = parser.parse_args() print(args.accumulate(args.integers))
argparse ã¯é«æ©è½ã§ããè¤éã§ããã
- ãã¼ãµã¼ã
argparse.ArgumentParser
ã§çæ - é常ã®å¼æ°ã
.add_argument('hoge')
ãã§å®ç¾© - ãªãã·ã§ã³å¼æ°ã
.add_argument('--hoge')
ã§å®ç¾© - çµæã
.parse_args()
ã§åå¾
ã¨ãããã¨ã ãè¦ããã°ãä»ã¯å¿ è¦ã«å¿ãã¦ããã¥ã¡ã³ããèªãã°ãªãã¨ããªãã¾ãã
çµäºæã®å¦çï¼ã·ã°ãã«ãtrapãã
ä¸æãã¡ã¤ã«ãåé¤ãããããã®ã«ä½¿ãã¾ãã
# ã´ãæé¤ç¨ã® trap å¦çãæå®ããã tf="temp.$$" trap 'echo "trapped."; rm -f $tf; exit 1' 1 2 3 15
åã«çµäºæã«ä½ãã®å¦çããããã®ãªããatexit ã使ãã®ãç°¡åã§ãã
import atexit import os tf = '/path/to/tempfile' @atexit.register def cleanup(): os.remove(tf)
signal ã使ãã°ãOSã®ã·ã°ãã«ããã³ããªã³ã°ã§ããã®ã§ããã使ãæ©ä¼ã¯å°ãªãã§ãããï¼å¤§ä½ãatexitã®ãããªé«æ°´æºã®ä»£æ¿ã©ã¤ãã©ãªãããã¯ãï¼ã
HTTPãªã¯ã¨ã¹ãï¼curl ã wget ã®ä»£æ¿ï¼
åã«ãã¡ã¤ã«ããã¦ã³ãã¼ãããããAPIã«POSTããã ããªããcurlãwgetãsubprocess.run
ã§å®è¡ããã ãã§ããããããã¾ããã
ã¾ãããµã¼ããã¼ãã£ã®HTTPã¯ã©ã¤ã¢ã³ãã¨ãã¦ã¯Requestsãæåã§ãå ¬å¼ããã¥ã¡ã³ãã§ãæ¨å¥¨ãã¦ãã¾ãã1
ãããããµã¼ããã¼ãã£ã®ã©ã¤ãã©ãªã使ããã¨ããæ¨æºã©ã¤ãã©ãªã®urllib.request.urlopen ã§ãããªãç°¡æ½ã«æ¸ããã¨ãã§ãã¾ãã
# URLããGETã§ãªã¯ã¨ã¹ãããã¬ã¹ãã³ã¹ããã£ãåºåãã import urllib.request import urllib.parse params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) url = f"http://www.musi-cal.com/cgi-bin/query?{params}" with urllib.request.urlopen(url) as f: print(f.read().decode('utf-8'))
ã¾ããurllib.request
ã§ã¯ãPOSTã§ãªã¯ã¨ã¹ããããããããã·ãèªè¨¼è¶ãã«ãªã¯ã¨ã¹ããããã¨ãåºæ¥ã¾ããå²æãã¾ãã
ã¨ã³ã¸ãã¢ãåéãã¦ãã¾ãï¼
åºç¤éçºãã¼ã ã§ã¯ãã¡ã¼ã«ã³ã³ã·ã§ã«ã¸ã¥ä»¥å¤ã«ãã ä¼å¡èªè¨¼åºç¤ãªã©ã®ã¨ã ã¹ãªã¼å ¨ä½ãæ¯ããæ§ã ãªã·ã¹ãã ãéçºã»éç¨ãã¦ãã¾ãã
ã¾ããäºæ¥ããä¸æ©å¼ããç«ã¡ä½ç½®ã«ããã®ã§ãæ°æè¡å°å ¥ãéçºããã¼ã®æ¹åãªã©ããªã¼ããã¦ãããã¨æã£ã¦ãã¾ãã ï¼æè¿ã¯Dockerãå°å ¥ããJenkinsããGitlab-CIã¸ã®ç§»è¡ãªã©ãè¡ãã¾ãããï¼
ä¸ç·ã«åã仲éãåéä¸ã§ãããæ°è»½ã«ãåãåãããã ããã
-
ãããªã« Requests ãåªãã¦ãããªããã©ããã¦æ¨æºã©ã¤ãã©ãªã«åãè¾¼ãã§ãã¾ããªããã ãã¨æãã®ã§ããã»ã»ã»ã↩