{"id":4179,"date":"2024-04-07T19:54:11","date_gmt":"2024-04-07T10:54:11","guid":{"rendered":"https:\/\/www.python.ambitious-engineer.com\/?p=4179"},"modified":"2024-04-29T19:18:38","modified_gmt":"2024-04-29T10:18:38","slug":"python%e5%ae%9f%e8%b7%b5%e9%96%8b%e7%99%ba%e5%85%a5%e9%96%80-%e3%82%b3%e3%83%9e%e3%83%b3%e3%83%89%e3%83%a9%e3%82%a4%e3%83%b3%e5%bc%95%e6%95%b0%e3%81%ae%e3%83%91%e3%83%bc%e3%82%b9","status":"publish","type":"post","link":"https:\/\/www.python.ambitious-engineer.com\/archives\/4179","title":{"rendered":"Python\u5b9f\u8df5\u958b\u767a\u5165\u9580 \u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u5f15\u6570\u306e\u30d1\u30fc\u30b9"},"content":{"rendered":"<h2>\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u5f15\u6570\u306e\u30d1\u30fc\u30b9<\/h2>\n<p>Python\u30b9\u30af\u30ea\u30d7\u30c8\u3067\u306f\u4eca\u56de\u306e\u3088\u3046\u306b\u5f15\u6570\u3092\u4f34\u3046\u51e6\u7406\u304c\u591a\u3005\u3042\u308a\u307e\u3059\u3002\u3053\u3053\u307e\u3067\u3067\u767b\u5834\u3057\u305f\u30b5\u30f3\u30d7\u30eb\u3067\u4f7f\u7528\u3057\u305fsys\u30e2\u30b8\u30e5\u30fc\u30eb\u306eargv\u3067\u3082\u3044\u3044\u306e\u3067\u3059\u304c\u3001\u5f15\u6570\u306e\u6570\u3092\u691c\u8a3c\u3057\u305f\u308a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u524d\u56de\u307e\u3067\u306b\u4f5c\u6210\u3057\u305f\u30b3\u30fc\u30c9\u306f\u5f15\u6570\u3092\u7701\u7565\u3057\u3066\u5b9f\u884c\u3059\u308b\u3068IndexError\u304c\u767a\u751f\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython main.py\r\n# IndexError: list index out of range\r\n<\/pre>\n<p>\u30e6\u30fc\u30b6\u30fc\u306b\u306f\u4f55\u304c\u4e0d\u8db3\u3057\u3066\u3044\u308b\u306e\u304b\u3068\u3044\u3063\u305f\u60c5\u5831\u3084\u30d8\u30eb\u30d7\u7b49\u3092\u8868\u793a\u3057\u3066\u3042\u3052\u305f\u3044\u3068\u3053\u308d\u3067\u3059\u3002\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306eargparse\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001help\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306e\u8a2d\u5b9a\u3001\u578b\u306e\u6307\u5b9a\u3068\u3044\u3063\u305f\u3053\u3068\u304c\u7c21\u5358\u306b\u5b9f\u88c5\u3067\u304d\u308b\u305f\u3081\u3001\u4eca\u56de\u306f\u3053\u3061\u3089\u3092\u4f7f\u7528\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>argparse\u306e\u7c21\u5358\u306a\u4f7f\u3044\u65b9<\/h3>\n<p>\u307e\u305a\u3001\u7c21\u5358\u306a\u4f7f\u3044\u65b9\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u6307\u5b9a\u3059\u308b\u5f15\u6570\u3092\u5b9a\u7fa9\u3059\u308b\u5834\u5408\u306b\u3064\u3044\u3066\u8003\u3048\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython main.py arg1 arg2 --opt1=val1 --opt2=val2\r\n<\/pre>\n<p>arg1\u3001arg2\u306f\u9806\u756a\u304c\u6c7a\u3081\u3089\u308c\u3066\u3044\u3066\u5fc5\u9808\u3001opt1\u3001opt2\u306f\u4efb\u610f\u3068\u3057\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b9\u30af\u30ea\u30d7\u30c8\u3067\u3001\u4e0a\u306e\u3088\u3046\u306a\u30b3\u30de\u30f3\u30c9\u3092\u5b9a\u7fa9\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport argparse\r\n \r\nparser = argparse.ArgumentParser()\r\n \r\nparser.add_argument(&quot;arg1&quot;)\r\nparser.add_argument(&quot;arg2&quot;)\r\nparser.add_argument(&quot;--opt1&quot;)\r\nparser.add_argument(&quot;--opt2&quot;)\r\nargs = parser.parse_args()\r\n \r\nprint(args.arg1)\r\nprint(args.arg2)\r\nprint(args.opt1)\r\nprint(args.opt2)\r\n<\/pre>\n<p>\u5b9f\u88c5\u306e\u30d5\u30ed\u30fc\u3068\u3057\u3066\u306f\u3001ArgumentParser\u3092\u751f\u6210\u3057\u3001add_argument\u3067\u5f15\u6570\u3092\u8ffd\u52a0\u3057\u3066\u3044\u304f\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002\u4e0a\u306e\u30b3\u30fc\u30c9\u3067\u306f\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u6307\u5b9a\u3057\u3066\u5b9f\u884c\u3059\u308b\u3068\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u5024\u304cprint\u51fa\u529b\u3055\u308c\u308b\u3053\u3068\u304c\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002\u307e\u305f\u3001\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u6307\u5b9a\u306a\u3044\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306ausage\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nusage: optsample.py [-h] [--opt1 OPT1] [--opt2 OPT2] arg1 arg2\r\noptsample.py: error: the following arguments are required: arg1, arg2\r\n<\/pre>\n<p>\u307e\u305f\u3001help\u3082\u81ea\u52d5\u3067\u5b9f\u88c5\u3055\u308c\u307e\u3059\u3002-h\u3092\u6307\u5b9a\u3057\u3066\u5b9f\u884c\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u51fa\u529b\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nusage: main.py [-h] [--opt1 OPT1] [--opt2 OPT2] arg1 arg2\r\n \r\npositional arguments:\r\n  arg1\r\n  arg2\r\n \r\noptional arguments:\r\n  -h, --help   show this help message and exit\r\n  --opt1 OPT1\r\n  --opt2 OPT2\r\n<\/pre>\n<h3>\u30d8\u30eb\u30d7\u306e\u8a2d\u5b9a<\/h3>\n<p>ArgumentParser()\u3067description\u3092\u3001add_argument\u3067help\u3092\u6307\u5b9a\u3059\u308b\u3068\u30b9\u30af\u30ea\u30d7\u30c8\u3068\u5f15\u6570\u306ehelp\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u5148\u307b\u3069\u306e\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4fee\u6b63\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nparser = argparse.ArgumentParser(description=&quot;\u30b5\u30f3\u30d7\u30eb\u30b9\u30af\u30ea\u30d7\u30c8\u3067\u3059\u3002&quot;)\r\n \r\nparser.add_argument(&quot;arg1&quot;, help=&quot;hoge\u3092\u6307\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002&quot;)\r\nparser.add_argument(&quot;arg2&quot;, help=&quot;foo\u3092\u6307\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002&quot;)\r\nparser.add_argument(&quot;--opt1&quot;, help=&quot;fuga\u3092\u6307\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4efb\u610f\u3067\u3059\u3002&quot;)\r\nparser.add_argument(&quot;--opt2&quot;, help=&quot;huga\u3092\u6307\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4efb\u610f\u3067\u3059\u3002&quot;)\r\n<\/pre>\n<p>-h\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u3064\u3051\u3066\u5b9f\u884c\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nusage: main.py [-h] [--opt1 OPT1] [--opt2 OPT2] arg1 arg2\r\n \r\n\u30b5\u30f3\u30d7\u30eb\u30b9\u30af\u30ea\u30d7\u30c8\u3067\u3059\u3002\r\n \r\npositional arguments:\r\n  arg1         hoge\u3092\u6307\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002\r\n  arg2         foo\u3092\u6307\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002\r\n \r\noptional arguments:\r\n  -h, --help   show this help message and exit\r\n  --opt1 OPT1  fuga\u3092\u6307\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4efb\u610f\u3067\u3059\u3002\r\n  --opt2 OPT2  huga\u3092\u6307\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u4efb\u610f\u3067\u3059\u3002\r\n<\/pre>\n<h3>\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306e\u8a2d\u5b9a<\/h3>\n<p>add_argument\u306e\u5f15\u6570\u306bdefault\u3067\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u8a18\u8ff0\u304c\u306a\u3044\u5834\u5408\u3001None\u304c\u8a2d\u5b9a\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nparser.add_argument(&quot;--opt1&quot;, default=10, help=&quot;fuga\u3092\u6307\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002&quot;)\r\n<\/pre>\n<p>\u3053\u308c\u4ee5\u5916\u306b\u3082\u69d8\u3005\u306a\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002\u4ee5\u4e0b\u306b\u7d30\u304b\u304f\u89e3\u8aac\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u5408\u308f\u305b\u3066\u53c2\u8003\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002<br \/>\n<a href=\"https:\/\/www.python.ambitious-engineer.com\/archives\/3023\" title=\"https:\/\/www.python.ambitious-engineer.com\/archives\/3023\">https:\/\/www.python.ambitious-engineer.com\/archives\/3023<\/a><\/p>\n<h2>\u5b9f\u88c5\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u306f\u8ab2\u984c\u306e\u30e1\u30a4\u30f3\u51e6\u7406\u3092ArgumentParser\u3092\u4f7f\u7528\u3057\u305f\u3082\u306e\u306b\u66f8\u304d\u63db\u3048\u305f\u5b9f\u88c5\u4f8b\u3067\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom settings import THRESHOLD\r\nfrom argparse import ArgumentParser\r\nfrom input_data import file2list\r\nfrom aggregate import aggregate\r\nfrom output_data import output_result\r\n\r\n# \u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u5f15\u6570\u8a2d\u5b9a\r\nparser = ArgumentParser()\r\nparser.add_argument(&quot;input_file_path&quot;, help=&quot;\u30c7\u30fc\u30bf\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u3092\u6307\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002&quot;)\r\nargs = parser.parse_args()\r\n\r\ndef main():\r\n    &quot;&quot;&quot; \u30e1\u30a4\u30f3\u51e6\u7406 &quot;&quot;&quot;\r\n\r\n    print(&quot;\u51e6\u7406\u3092\u958b\u59cb\u3057\u307e\u3059\u3002&quot;)\r\n\r\n    # \u5165\u529b\u30c7\u30fc\u30bf\u691c\u8a3c\u30fb\u53d6\u5f97\r\n    data_list = file2list(args.input_file_path)\r\n\r\n    # \u96c6\u8a08\u51e6\u7406\r\n    result_count = aggregate(data_list, THRESHOLD)\r\n\r\n    # \u51fa\u529b\u51e6\u7406\r\n    output_result(result_count)\r\n\r\nif __name__ == '__main__':\r\n    main()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u5f15\u6570\u306e\u30d1\u30fc\u30b9 Python\u30b9\u30af\u30ea\u30d7\u30c8\u3067\u306f\u4eca\u56de\u306e\u3088\u3046\u306b\u5f15\u6570\u3092\u4f34\u3046\u51e6\u7406\u304c\u591a\u3005\u3042\u308a\u307e\u3059\u3002\u3053\u3053\u307e\u3067\u3067\u767b\u5834\u3057\u305f\u30b5\u30f3\u30d7\u30eb\u3067\u4f7f\u7528\u3057\u305fsys\u30e2\u30b8\u30e5\u30fc\u30eb\u306eargv\u3067\u3082\u3044\u3044\u306e\u3067\u3059\u304c\u3001\u5f15\u6570\u306e\u6570\u3092\u691c\u8a3c\u3057\u305f\u308a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002 \u4f8b...<\/p>\n","protected":false},"author":1,"featured_media":4162,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[218],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/4179"}],"collection":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/comments?post=4179"}],"version-history":[{"count":5,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/4179\/revisions"}],"predecessor-version":[{"id":4199,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/4179\/revisions\/4199"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media\/4162"}],"wp:attachment":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media?parent=4179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/categories?post=4179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/tags?post=4179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}