yamlとdocoptを利用した引数指定 [Python]
個人的に,入出力をdocoptを記述し,コンフィグ情報はyamlを使って引数として取り込むのが好きなので,そのスクリプトについてメモしておく.decoptの記述では,空行を設けることが重要なので,必要以上に詰めて書かないこと.
"""Overview: コーディング例を示すためのスクリプト.あるフォルダパスを指定すると,何らかの処理により,1つのファイルが出力される
Usage: ./act.py -d DIR -o OFILE
Arguments: -d,--directory=DIR ディレクトリ名の指定。*.txtファイル全てを入力対象とする -o,--output=OFILE 出力するファイル名を指定する (*.txt)"""
import yamlfrom docopt import docopt
if __name__ == "__main__": args = docopt(__doc__) src_folder = Path(args["--directory"]) dst_folder = Path(args["--output"])
with open("./config.yaml") as f: cfgs = yaml.safe_load(f) debug_flag = cfgs["debug"] splitter = cfgs["splitter"] sep = cfgs["sep"]以下にconfig.yamlの内容を示す.
>> config.yamldebug: False # デバッグするかどうかsplitter: nltk # splitの方法sep: tab # 区切り文字の指定パッケージはpipでインストール
pip install docoptpip install pyyaml