{"id":361,"date":"2019-08-05T22:26:37","date_gmt":"2019-08-05T13:26:37","guid":{"rendered":"http:\/\/localhost:8000\/?p=361"},"modified":"2021-01-18T02:22:57","modified_gmt":"2021-01-17T17:22:57","slug":"python","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2019\/08\/python.html","title":{"rendered":"python\u306eTips"},"content":{"rendered":"
\u4ed5\u4e8b\u3067python\u3092\u89e6\u308b\u6a5f\u4f1a\u304c\u4eca\u5f8c\u5897\u3048\u305d\u3046\u306a\u306e\u3067\u3001python\u306b\u95a2\u3059\u308b\u30e1\u30e2\u3092\u305f\u3081\u3066\u3044\u304f\u4e88\u5b9a\u3067\u3059\u3002<\/p>\n
pyenv\u3092\u4f7f\u3063\u3066python\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059<\/p>\n
# pyenv\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\nbrew install pyenv\npyenv -v\n> pyenv 1.2.13\n\n# \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u53ef\u80fd\u306apython\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u78ba\u8a8d\npyenv install --list\n\n# \u7279\u5b9a\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306epython\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\npyenv install 3.7.4\n\n# \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u6e08\u307f\u306epython\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u78ba\u8a8d\npyenv versions\n> * system (set by \/Users\/rinoguchi\/.pyenv\/version)\n> 3.7.4\n\n# ~\/.bash_profile\u306b\u4ee5\u4e0b\u306e\u8a2d\u5b9a\u3092\u8ffd\u52a0\uff08syms\u3068autocompletion\u306e\u6709\u52b9\u5316\uff09\necho 'export PYENV_ROOT="$HOME\/.pyenv"' >> ~\/.bash_profile\necho 'export PATH="$PYENV_ROOT\/bin:$PATH"' >> ~\/.bash_profile\necho -e 'if command -v pyenv 1>\/dev\/null 2>&1; then\\n eval "$(pyenv init -)"\\nfi' >> ~\/.bash_profile\nsource ~\/.bash_profile\n\n# python\u30b3\u30de\u30f3\u30c9\u306e\u53c2\u7167\u5148\n# - python\u30b3\u30de\u30f3\u30c9\u3082pip\u30b3\u30de\u30f3\u30c9\u3082.pyenv\/shims\u914d\u4e0b\u3092\u898b\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u304a\u308a\n# - \u5b9f\u4f53\u306fbash\u3067\u3001\u6700\u7d42\u7684\u306b\u306f\u6307\u5b9a\u3055\u308c\u305f\u30d0\u30fc\u30b8\u30e7\u30f3\u306e.pyenv\/versions\u914d\u4e0b\u3092\u898b\u306b\u3044\u3063\u3066\u304f\u308c\u308b\u307d\u3044\nwhich python\n> \/Users\/rinoguchi\/.pyenv\/shims\/python\nwhich pip\n> \/Users\/rinoguchi\/.pyenv\/shims\/pip\n\n# \u30b0\u30ed\u30fc\u30d0\u30eb\u3067\u7279\u5b9a\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u4f7f\u7528\u3059\u308b\u3088\u3046\u6307\u5b9a\npyenv global 3.7.4 # .pyenv\/version\u306b\u30d0\u30fc\u30b8\u30e7\u30f3\u756a\u53f7\u304c\u8a18\u8f09\u3055\u308c\u308b\npyenv versions\n> system (set by \/Users\/rinoguchi\/.pyenv\/version)\n> * 3.7.4\n\n# \u7279\u5b9a\u306e\u30d5\u30a9\u30eb\u30c0\u914d\u4e0b\u3060\u3051\u7279\u5b9a\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u4f7f\u7528\u3059\u308b\u3088\u3046\u6307\u5b9a\n# - \u5b9f\u884c\u3057\u305f\u30d5\u30a9\u30eb\u30c0\u76f4\u4e0b\u306b.pyenv-version\u30d5\u30a1\u30a4\u30eb\u4f5c\u6210\u3055\u308c\u308b\n# - \u3053\u306e\u30d5\u30a1\u30a4\u30eb\u304c\u3053\u306e\u30d5\u30a9\u30eb\u30c0\u914d\u4e0b\u3067\u306f\u30d0\u30fc\u30b8\u30e7\u30f3\u56fa\u5b9a\u3055\u308c\u308b\n# - git\u30ea\u30dd\u30b8\u30c8\u30ea\u306bcommit\u3059\u308b\u306e\u3082\u3042\u308a\ncd hoge\/fuga\npyenv local 2.7.3\n\n# pyenv local\u3092\u89e3\u9664\npyenv local --unset\n# .pyenv-version\u30d5\u30a1\u30a4\u30eb\u524a\u9664\u3055\u308c\u308b<\/code><\/pre>\nPython\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u7ba1\u7406\u304a\u3088\u3073\u4eee\u60f3\u74b0\u5883\u7ba1\u7406\uff08poetry\uff09<\/h2>\n
\u3053\u308c\u307e\u3067pip + virtualenv\uff08venv\uff09\u3092\u4f7f\u3063\u3066\u3044\u307e\u3057\u305f\u304c\u3001poetry\u306b\u5909\u66f4\u3057\u307e\u3057\u305f\u3002\u8a73\u7d30\u306f\u5225\u8a18\u4e8b\u306b\u3057\u305f\u306e\u3067\u305d\u3061\u3089\u3092\u53c2\u7167\u304f\u3060\u3055\u3044\u3002
\npoetry\u3067\u30d1\u30c3\u30b1\u30fc\u30b8\u30fb\u4eee\u60f3\u74b0\u5883\u3092\u7ba1\u7406<\/a><\/p>\nPython\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u7ba1\u7406\uff08pip\uff09<\/h2>\n
python\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u7ba1\u7406\u30b7\u30b9\u30c6\u30e0pip\u3092\u5229\u7528\u3059\u308b\u3002\u3053\u308c\u306fpython 3.4\u4ee5\u964d\u306fpython\u672c\u4f53\u3068\u4e00\u7dd2\u306binstall\u3055\u308c\u307e\u3059\u3002
\n\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u5148\u306f\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u3060\u3068python\u672c\u4f53\u914d\u4e0b\u306e\/lib\/pythonX.Y\/site-packages<\/code>\u30d5\u30a9\u30eb\u30c0\u306a\u306e\u3067\u3001\u540c\u4e00\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u4f7f\u3046\u5834\u5408\u306b\u306f\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u305f\u30d1\u30c3\u30b1\u30fc\u30b8\u306f\u5171\u6709\u3055\u308c\u307e\u3059\u3002<\/p>\n# \u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\npip install numpy\n\n# \u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u524a\u9664\u3059\u308b\npip uninstall numpy\n\n# \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u6e08\u307f\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u78ba\u8a8d\u3059\u308b\npip list\n\n# \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u5bfe\u8c61\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\uff08requirements.txt\uff09\u3067\u7ba1\u7406\u3059\u308b\nvi requirements.txt\n\n# \u73fe\u5728\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u6e08\u307f\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u5f62\u5f0f\u3067\u8868\u793a\u3059\u308b\uff08\u3053\u308c\u3092requirements.txt\u306b\u30b3\u30d4\u30fc\u3059\u308b\uff09\npip freeze\n> pylint==2.3.1\n> pyspark==2.4.3\n> numpy==1.17.0\n\n# \u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306b\u3057\u305f\u304c\u3063\u3066\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\npip install -r requirements.txt\n\n# \u7279\u5b9a\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\n# - -t\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u53ef\u80fd\u3060\u304c\u3001import\u3059\u308b\u969b\u306b\u306f\u30e2\u30b8\u30e5\u30fc\u30eb\u691c\u7d22\u30d1\u30b9\u3092\u89e3\u6c7a\u304c\u5fc5\u8981\u306b\u306a\u308b\u306e\u3067\u304a\u3059\u3059\u3081\u3057\u306a\u3044\u3002\npip install -t my_packages numpy<\/code><\/pre>\nPython\u306e\u4eee\u60f3\u74b0\u5883\u69cb\u7bc9\uff08virtualenv\uff09<\/h2>\n
virtualenv\u3067\u9694\u96e2\u3055\u308c\u305fPython\u306e\u4eee\u60f3\u74b0\u5883\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002
\n\u5177\u4f53\u7684\u306b\u306f\u3001python\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u5168\u4f53\u304c\u6307\u5b9a\u3057\u305f\u30d5\u30a9\u30eb\u30c0\u306b\u30b3\u30d4\u30fc\u3055\u308c\u3001\u30b3\u30d4\u30fc\u3055\u308c\u305f\u30d5\u30a1\u30a4\u30eb\u3092\u5229\u7528\u3057\u3066python\u30b3\u30de\u30f3\u30c9\u3084pip\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u5f62\u306b\u306a\u308a\u307e\u3059\u3002
\n\u305d\u306e\u305f\u3081\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u56fa\u6709\u306epython\u30d0\u30fc\u30b8\u30e7\u30f3\u7ba1\u7406\u3001\u30d1\u30c3\u30b1\u30fc\u30b8\u7ba1\u7406\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n
# \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u30d5\u30a9\u30eb\u30c0\u306b\u79fb\u52d5\u3059\u308b\ncd {project_folder}\n\n# pyenv\u3067python\u304a\u3088\u3073pip\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u6307\u5b9a\u3059\u308b\npyenv local 3.7.4\n\n# \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\npip install --upgrade virtualenv\n\n# \u4eee\u60f3\u74b0\u5883\u69cb\u7bc9\n# - env\u306f\u5225\u306e\u540d\u524d\u3067\u3082OK\u3002env\u30d5\u30a9\u30eb\u30c0\u306bpython\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u5168\u4f53\u306e\u30b3\u30d4\u30fc\u304c\u4f5c\u6210\u3055\u308c\u308b\nvirtualenv env\n\n# \u4eee\u60f3\u74b0\u5883\u6709\u52b9\u5316\n# - PATH\u306e\u4e00\u756a\u6700\u521d\u306b\u3001`env\/bin`\u3092\u8ffd\u52a0\u3057\u3066\u3044\u308b\nsource env\/bin\/activate\n\n# python\u3084pip\u30b3\u30de\u30f3\u30c9\u304cenv\u914d\u4e0b\u306epython\u3092\u53c2\u7167\u3057\u3066\u304f\u308c\u308b\u3088\u3046\u306b\u306a\u308b\nwhich python\n> {project_folder}\/env\/bin\/python\n\n# \u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\n# - \u3053\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u306f\u4ed6\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u306f\u5f71\u97ff\u3092\u4e0e\u3048\u306a\u3044\npip install -r requirements.txt\n\n# \u4eee\u60f3\u74b0\u5883\u7121\u52b9\u5316\n# - PATH\u304b\u3089`env\/bin`\u3092\u524a\u9664\u3057\u3066\u3044\u308b\ndeactivate<\/code><\/pre>\n\u30ed\u30fc\u30ab\u30eb\u958b\u767a\u74b0\u5883\u3067\u306e\u74b0\u5883\u5909\u6570\u306e\u8a2d\u5b9a<\/h2>\n
\u30ed\u30fc\u30ab\u30eb\u958b\u767a\u74b0\u5883\u3067\u306e\u74b0\u5883\u5909\u6570\u7ba1\u7406\u306f\u3044\u308d\u3093\u306a\u3084\u308a\u65b9\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u3061\u3089\u306f\u5225\u8a18\u4e8b\u306b\u3057\u307e\u3057\u305f\u3002
\npython\u306b\u304a\u3051\u308b\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u3067\u306e\u74b0\u5883\u5909\u6570\u306e\u8a2d\u5b9a<\/a><\/p>\n\u578b\u30d2\u30f3\u30c8\u306b\u3064\u3044\u3066<\/h2>\n
\u578b\u30d2\u30f3\u30c8\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u9759\u7684\u578b\u4ed8\u8a00\u8a9e\u306e\u3088\u3046\u306a\u611f\u899a\u3067\u5b9f\u88c5\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3053\u3061\u3089\u306f\u5225\u8a18\u4e8b\u306b\u3057\u307e\u3057\u305f\u3002
\npython\u306e\u578b\u30d2\u30f3\u30c8<\/a><\/p>\n\u5171\u901a\u30e2\u30b8\u30e5\u30fc\u30eb\u306eimport<\/h2>\n
\u89aa\u5144\u5f1f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u3042\u308b\u5171\u901a\u30e2\u30b8\u30e5\u30fc\u30eb\u3092import\u3059\u308b\u5834\u5408\u3001\u7279\u5225\u306a\u5bfe\u5fdc\u3092\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u3061\u3089\u306f\u5225\u8a18\u4e8b\u306b\u3057\u307e\u3057\u305f\u3002
\npython\u306b\u304a\u3051\u308b\u89aa\/\u5144\u5f1f\u968e\u5c64\u306e\u30e2\u30b8\u30e5\u30fc\u30ebimport\u306e\u65b9\u6cd5<\/a><\/p>\n\u5909\u6570\u30b9\u30b3\u30fc\u30d7<\/h2>\n
\u3053\u3061\u3089<\/a>\u306e\u8a18\u4e8b\u3067\u52c9\u5f37\u3055\u305b\u3066\u3044\u305f\u3060\u3044\u305f\u306e\u3067\u3059\u304c\u3001python\u306e\u30b9\u30b3\u30fc\u30d7\u306f4\u6bb5\u968e\u3042\u308b\u3088\u3046\u3067\u3059\u3002for<\/code>\u3084if<\/code>\u306b\u30d6\u30ed\u30c3\u30af\u30b9\u30b3\u30fc\u30d7\u304c\u7121\u3044\u3053\u3068\u306b\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n\n- \u30d3\u30eb\u30c8\u30a4\u30f3\u30b9\u30b3\u30fc\u30d7\n
\nprint<\/code> int<\/code> str<\/code> len<\/code> range<\/code> \u306e\u3088\u3046\u306a\u7d44\u307f\u8fbc\u307f\u95a2\u6570\u306e\u30b9\u30b3\u30fc\u30d7\u3067\u3001\u4e00\u756a\u5916\u5074\u306e\u30b9\u30b3\u30fc\u30d7\u3002\u3069\u3053\u304b\u3089\u3067\u3082\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b<\/li>\nif<\/code>\u3084for<\/code>\u306e\u3088\u3046\u306a\u69cb\u6587\u30ad\u30fc\u30ef\u30fc\u30c9\u306f\u4e88\u7d04\u8a9e\u306a\u306e\u3067\u3001\u3053\u308c\u306b\u306f\u5f53\u305f\u3089\u306a\u3044<\/li>\n<\/ul>\n<\/li>\n- \u30b0\u30ed\u30fc\u30d0\u30eb\u30b9\u30b3\u30fc\u30d7\n
\n- python\u30d5\u30a1\u30a4\u30eb\u5185\u3067\u6709\u52b9\u306a\u30b9\u30b3\u30fc\u30d7\u3002\u30e2\u30b8\u30e5\u30fc\u30eb\u30b9\u30b3\u30fc\u30d7\u3068\u3082\u3044\u3046\u307f\u305f\u3044\u3002\u5225\u30d5\u30a1\u30a4\u30eb\u3001\u5225\u30e2\u30b8\u30e5\u30fc\u30eb\u304b\u3089\u306f\u30a2\u30af\u30bb\u30b9\u3067\u304d\u306a\u3044<\/li>\n<\/ul>\n<\/li>\n
- \u30a8\u30f3\u30af\u30ed\u30fc\u30b8\u30f3\u30b0\u30b9\u30b3\u30fc\u30d7\n
\n- \u95a2\u6570\u306e\u5185\u5074\u306b\u95a2\u6570\u3092\u5b9a\u7fa9\u3057\u305f\u969b\u306b\u3001\u5185\u5074\u306e\u95a2\u6570\u30b9\u30b3\u30fc\u30d7\u304b\u3089\u898b\u305f\u3068\u304d\u306b\u3001\u5916\u5074\u306e\u95a2\u6570\u30b9\u30b3\u30fc\u30d7\u3092\u30a8\u30f3\u30af\u30ed\u30fc\u30b8\u30f3\u30b0\u30b9\u30b3\u30fc\u30d7\u3068\u3044\u3046\u3089\u3057\u3044\u3002\u30af\u30ed\u30fc\u30b8\u30e3\u3067\u5229\u7528\u3055\u308c\u308b\u3002<\/li>\n<\/ul>\n<\/li>\n
- \u30ed\u30fc\u30ab\u30eb\u30b9\u30b3\u30fc\u30d7\n
\n- \u95a2\u6570\u30b9\u30b3\u30fc\u30d7\u3002\u305d\u306e\u95a2\u6570\u306e\u5916\u5074\u304b\u3089\u306f\u30a2\u30af\u30bb\u30b9\u3067\u304d\u306a\u3044<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n
\u30af\u30ed\u30fc\u30b8\u30e3<\/h2>\n
python\u306e\u95a2\u6570\u306f\u7b2c\u4e00\u7d1a\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u6271\u3046\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067\u3001\u30af\u30ed\u30fc\u30b8\u30e3\u3092\u5b9a\u7fa9\u3067\u304d\u307e\u3059\u3002\u30af\u30ed\u30fc\u30b8\u30e3\u3068\u306f\u3001wikipedia\u306b\u3088\u308b\u3068<\/p>\n
\n\u5f15\u6570\u4ee5\u5916\u306e\u5909\u6570\u3092\u5b9f\u884c\u6642\u306e\u74b0\u5883\u3067\u306f\u306a\u304f\u3001\u81ea\u8eab\u304c\u5b9a\u7fa9\u3055\u308c\u305f\u74b0\u5883\uff08\u9759\u7684\u30b9\u30b3\u30fc\u30d7\uff09\u306b\u304a\u3044\u3066\u89e3\u6c7a\u3059\u308b\u3053\u3068\u3092\u7279\u5fb4\u3068\u3059\u308b\u3002<\/p>\n<\/blockquote>\n
\u3068\u306e\u3053\u3068\u3067\u3059\u3002\u81ea\u5206\u306e\u7406\u89e3\u3067\u306f\u3001\u81ea\u8eab\u304c\u5b9a\u7fa9\u3055\u308c\u305f\u5916\u5074\u306e\u30ed\u30fc\u30ab\u30eb\u30b9\u30b3\u30fc\u30d7\u306e\u5909\u6570\u3092\u4f7f\u7528\u3059\u308b\u95a2\u6570<\/code>\u3068\u3044\u3046\u30a4\u30e1\u30fc\u30b8\u3067\u3059\u3002
\n\u30af\u30ed\u30fc\u30b8\u30e3\u306e\u4f8b\u3067\u3088\u304f\u3042\u308bcounter\u3092\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\nfrom typing import Callable\n\ndef counter():\n cnt: int = 0\n def __count():\n nonlocal cnt # \u3053\u306e\u5ba3\u8a00\u306b\u3088\u3063\u3066\u30a8\u30f3\u30af\u30ed\u30fc\u30b8\u30f3\u30b0\u30b9\u30b3\u30fc\u30d7\u306e\u5909\u6570\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\n cnt += 1\n return cnt\n return __count # \u3053\u3053\u3067__count\u95a2\u6570\u3092\u8fd4\u5374\u3057\u3066\u3044\u308b\n\nfnc: Callable = counter() # \u3053\u3053\u3067\u95a2\u6570\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d7\u3051\u53d6\u3063\u3066\u3044\u308b\nprint(fnc()) # \u3053\u306e\u95a2\u6570\u306f\u305d\u306e\u5916\u5074\u306e\u30ed\u30fc\u30ab\u30eb\u30b9\u30b3\u30fc\u30d7\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u5909\u6570\uff08cnt\uff09\u3092\u53c2\u7167\u3057\u3066\u3044\u308b\n# -> 1\nprint(fnc())\n# -> 2<\/code><\/pre>\nVS\u30b3\u30fc\u30c9\u8a2d\u5b9a<\/h2>\n\u578b\u30d2\u30f3\u30c8\u3092\u5229\u7528\u3057\u305f\u9759\u7684\u30b3\u30fc\u30c9\u30c1\u30a7\u30c3\u30af<\/h3>\n
VS\u30b3\u30fc\u30c9\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u3001\u578b\u30d2\u30f3\u30c8\u3092\u6307\u5b9a\u3057\u305f\u9759\u7684\u30b3\u30fc\u30c9\u30c1\u30a7\u30c3\u30af\u306f\u884c\u3063\u3066\u304f\u308c\u306a\u3044\u306e\u3067\u3001mypy<\/code>\u306a\u3069\u306e\u30c4\u30fc\u30eb\u3092\u5c0e\u5165\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
\n\u5177\u4f53\u7684\u306b\u306fsettings.json\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002\u3053\u306e\u8a2d\u5b9a\u3092\u3044\u308c\u3066\u304a\u304f\u3068\u3001\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u30c0\u30a4\u30a2\u30ed\u30b0\u304c\u51fa\u3066\u304f\u308b\u306e\u3067\u305d\u3053\u304b\u3089\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n{\n "python.linting.lintOnSave": true,\n "python.linting.mypyEnabled": true,\n}<\/code><\/pre>\nlinter\u304a\u3088\u3073formatter\u306e\u8a2d\u5b9a<\/h3>\n\n- linter\u306f
flake8<\/code>\u3001formatter\u306fautopep8<\/code>\u3092\u63a1\u7528<\/li>\n- \u3069\u3061\u3089\u3082python\u306e\u30b3\u30fc\u30c9\u898f\u7d04\u3067\u3042\u308bpep8\u306b\u6e96\u62e0\u3057\u3066\u3044\u308b\u306e\u3067\u76f8\u6027\u306f\u826f\u3044\u306f\u305a<\/li>\n
max-line-length<\/code>\u306e\u8a2d\u5b9a\u306f\u4e21\u65b9\u3067\u5408\u308f\u305b\u308b\u5fc5\u8981\u304c\u3042\u308b<\/li>\n<\/ul>\n\u5177\u4f53\u7684\u306b\u306fsettings.json\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u307e\u3057\u305f\u3002<\/p>\n
{\n "python.linting.lintOnSave": true,\n "python.linting.pylintEnabled": false,\n "python.linting.flake8Enabled": true,\n "python.linting.flake8Args": [\n "--ignore=W293, W504",\n "--max-line-length=150",\n "--max-complexity=20"\n ],\n "editor.formatOnSave": true,\n "python.formatting.provider": "autopep8",\n "python.formatting.autopep8Args": [\n "--aggressive",\n "--aggressive",\n "--max-line-length=150",\n ],\n}<\/code><\/pre>\n\u30c6\u30b9\u30c8\u306b\u3064\u3044\u3066<\/h2>\n
\u6a19\u6e96\u30e2\u30b8\u30e5\u30fc\u30eb\u306eunittest<\/a>\u3092\u4f7f\u3063\u3066\u30b7\u30f3\u30d7\u30eb\u306b\u30c6\u30b9\u30c8\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3053\u3061\u3089\u306e\u8a18\u4e8b\u3067\u4f7f\u3044\u65b9\u3092\u307e\u3068\u3081\u307e\u3057\u305f\u3002
\n\u6a19\u6e96\u30e2\u30b8\u30e5\u30fc\u30ebunittest\u3067python\u306e\u30c6\u30b9\u30c8\u3092\u66f8\u304f<\/a><\/p>\n\u5b9f\u88c5Tips<\/h2>\n\u30bf\u30d7\u30eb \u21c4 Array<\/h3>\n# \u30bf\u30d7\u30eb -> Array\ntuple = (hoge, fuga) = ('xxx', 'yyy')\nlist(tuple)\n# > ['xxx', 'yyy']\n\n# Array -> \u30bf\u30d7\u30eb\narray = ['xxx', 'yyy']\ntuple(array)\n# > ('xxx', 'yyy')<\/code><\/pre>\n1\u884c\u3067if\u6587\u3092\u8a18\u8f09\u3059\u308b<\/h3>\nhoge = get_hoge()\n# 1\u884c\nreturn None if hoge == '' else hoge # else\u304c\u306a\u3044\u3068\u69cb\u6587\u30a8\u30e9\u30fc\u306b\u306a\u308b\n\n# \u3053\u308c\u3068\u540c\u3058\u610f\u5473\nif hoge == '':\n return None\nelse:\n return hoge<\/code><\/pre>\n\u30ea\u30b9\u30c8\u3092N\u4ef6\u6bce\u306b\u5206\u5272<\/h3>\n
more-itertools<\/a> \u3092\u4f7f\u3046\u306e\u304c\u826f\u3055\u305d\u3046<\/p>\nfrom more_itertools import chunked\narray = [0, 1, 2, 3, 4, 5, 6, 7, 8]\nlist(chunked(array, 4))\n# > [[0, 1, 2, 3], [4, 5, 6, 7], [8]]<\/code><\/pre>\n\u90e8\u5206\u4e00\u81f4\u6587\u5b57\u5217\u3092\u62bd\u51fa<\/h3>\nimport re\nstring = '\u5c71\u62ec\u5f27\u5185\u306e\u6587\u5b57\u5217\u3092\u62bd\u51fa\u3057\u307e\u3059 <hogefuga>'\nmatch = re.search(r'<(.+)>', string)\nif match:\n print(match[0])\n print(match[1])\n# > <hogefuga>\n# > hogefuga<\/code><\/pre>\n\u6587\u5b57\u5217\u5185\u306e\u6587\u5b57\u5217\u306e\u4f4d\u7f6e\u3092\u691c\u7d22<\/h3>\ntext = '112233445511223344551122334455'\n# \u958b\u59cb\u4f4d\u7f6e\u3001\u7d42\u4e86\u4f4d\u7f6e\u3092\u6307\u5b9a\u3057\u306a\u3044\u2192\u4e00\u756a\u6700\u521d\u306b\u4e00\u81f4\u3057\u305f\u4f4d\u7f6e\ntext.find('11')\n# > 0\n# \u958b\u59cb\u4f4d\u7f6e\u3092\u6307\u5b9a\u2192\u958b\u59cb\u4f4d\u7f6e\u4ee5\u964d\u3067\u4e00\u756a\u6700\u521d\u306b\u4e00\u81f4\u3057\u305f\u4f4d\u7f6e\ntext.find('11', 10)\n# > 10\n# \u958b\u59cb\u4f4d\u7f6e\u3068\u7d42\u4e86\u4f4d\u7f6e\u3092\u6307\u5b9a\u2192\u7bc4\u56f2\u5185\u3067\u4e00\u756a\u6700\u521d\u306b\u4e00\u81f4\u3057\u305f\u4f4d\u7f6e\ntext.find('11', 10, 11)\n# > -1\ntext.find('11', 10, 30)\n# > 10\n\n# rfind\u3092\u4f7f\u3046\u3068\u3001\u53f3\u5074\u304b\u3089\u691c\u7d22\u3059\u308b\u3002\u958b\u59cb\u4f4d\u7f6e\u3084\u7d42\u4e86\u4f4d\u7f6e\u306ffind\u3068\u540c\u3058\u304f\u5de6\u5074\u304b\u3089\u30ab\u30a6\u30f3\u30c8\u3059\u308b\u3002\ntext.rfind('11')\n# > 20\ntext.rfind('11', 10)\n# > 20\ntext.rfind('11', 10, 11)\n# > -1\ntext.rfind('11', 10, 12)\n# > 10<\/code><\/pre>\nlist\u3068set\u3068dict<\/h3>\n\nlist<\/code>\u306f\u91cd\u8907OK\u3067\u9806\u756a\u3042\u308a\u306e\u30ea\u30b9\u30c8\u3002\u57fa\u672c\u306findex\u6307\u5b9a\u304b\u3001loop\u3057\u3066\u53c2\u7167\u3059\u308b<\/li>\nset<\/code>\u306f\u91cd\u8907NG\u3067\u9806\u756a\u306a\u3057\u306e\u30ea\u30b9\u30c8\u3002\u57fa\u672c\u306floop\u3057\u3066\u53c2\u7167\u3059\u308b<\/li>\ndict<\/code>\u306fkey\u306b\u5bfe\u3059\u308bvalue\u3092\u4fdd\u6301\u3059\u308b\u8f9e\u66f8\u3002key\u306f\u91cd\u8907\u3092\u8a31\u3055\u306a\u3044\u3002\u57fa\u672c\u306fkey\u3092\u6307\u5b9a\u3057\u3066\u53c2\u7167\u3059\u308b<\/li>\n<\/ul>\nfrom typing import List, Set, Dict\n\n## list\nl = [] #\u7a7a\u3067\u521d\u671f\u5316\nl = [1, 2, 3] #\u521d\u671f\u5316\nl: List[int] = [1, 2, 3] #\u578b\u30d2\u30f3\u30c8\u4ed8\u304d\u521d\u671f\u5316\nl.append(4) #\u8ffd\u52a0\nl.remove(4) #value\u6307\u5b9a\u524a\u9664\uff08\u540c\u3058\u8981\u7d20\u304c\u8907\u6570\u3042\u3063\u305f\u3089\u6700\u521d\u306e1\u3064\u3060\u3051\u524a\u9664\uff09\nl.clear() #\u5168\u524a\u9664\nl.extend([5, 6]) #list\u306e\u7d50\u5408\u3002\u5143\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u5909\u66f4\u3055\u308c\u308b\u70b9\u306b\u6ce8\u610f\nl.sort() #\u30bd\u30fc\u30c8\u3002\u5143\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u5909\u66f4\u3055\u308c\u308b\u70b9\u306b\u6ce8\u610f\nl.sort(reverse=True) #\u9006\u9806\u30bd\u30fc\u30c8\u3002\u5143\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u5909\u66f4\u3055\u308c\u308b\u70b9\u306b\u6ce8\u610f\nl.sorted() # \u30bd\u30fc\u30c8\u3002\u5143\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u5909\u66f4\u3055\u308c\u306a\u3044\n3 in l #\u5b58\u5728\u78ba\u8a8d\u3002\u5185\u90e8\u7684\u306b\u306f\u8981\u7d20\u306e`__eq__`\u30e1\u30bd\u30c3\u30c9\u304ctrue\u3092\u8fd4\u3059\u304b\u3069\u3046\u304b\u3067\u5224\u65ad\n\n## set\ns = set() #\u7a7a\u3067\u521d\u671f\u5316\u3002`set = {}`\u3060\u3068dict\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u306e\u3067NG\ns = {1, 2, 3, 2, 1} #\u521d\u671f\u5316\ns = set({1, 2, 3, 2, 1}) #\u521d\u671f\u5316\n# > {1, 2, 3}\ns: Set[int] = {1, 2, 3, 2, 1} #\u578b\u30d2\u30f3\u30c8\u4ed8\u304d\u521d\u671f\u5316\ns: Set[int] = set({1, 2, 3, 2, 1}) #\u578b\u30d2\u30f3\u30c8\u4ed8\u304d\u521d\u671f\u5316\n# > {1, 2, 3}\ns.add(4) #\u8ffd\u52a0\ns.remove(4) #\u524a\u9664\ns.clear() #\u5168\u524a\u9664\ns.update({4, 5, 6}) #\u4e00\u62ec\u66f4\u65b0\u3002\u5143\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u66f4\u65b0\u3055\u308c\u308b\ns |= {4, 5, 6} # update\u3068\u540c\u3058\ns.union({5, 6}) #set\u306e\u7d50\u5408\u3002\u5143\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u5909\u66f4\u3055\u308c\u306a\u3044\u70b9\u306b\u6ce8\u610f\n# \u8981\u7d20\u306e\u5b58\u5728\u78ba\u8a8d\nif 3 in s:\n print('3 exists in s')\n# 1\u4ef6\u53d6\u5f97\nnext(iter(s)) # 0\u4ef6\u3060\u3068\u30a8\u30e9\u30fc\nnext(iter(s)) if len(s) > 0 else None # 0\u4ef6\u3092\u8003\u616e\u3059\u308b\u3068\u3053\u3093\u306a\u611f\u3058\ns.pop() # set\u304b\u3089item\u304c\u524a\u9664\u3055\u308c\u308b\u3053\u3068\u306b\u6ce8\u610f\u3002\u3053\u3061\u3089\u30820\u4ef6\u3060\u3068\u30a8\u30e9\u30fc\n\n## dict\nd = dict() # \u521d\u671f\u5316\nd = {'key1': 'aaa', 'key2': 'bbb', 'key3': 'ccc'} # \u521d\u671f\u5316\n# > {'key1': 'aaa', 'key2': 'bbb', 'key3': 'ccc'}\nd: Dict[str, str] = {'key1': 'aaa', 'key2': 'bbb', 'key3': 'ccc'} # \u578b\u30d2\u30f3\u30c8\u4ed8\u304d\u521d\u671f\u5316\n# > {'key1': 'aaa', 'key2': 'bbb', 'key3': 'ccc'}\nd['key1'] # \u5024\u53d6\u5f97\n# > 'aaa'\nd.get('key1') # \u5024\u53d6\u5f97\n# > 'aaa'\nd.get('key99', 'xxx') # \u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3042\u308a\u3067\u5024\u53d6\u5f97\n# > 'xxx'\nd['key4'] = 'ddd' # \u8ffd\u52a0\/\u66f4\u65b0\n# > {'key1': 'aaa', 'key2': 'bbb', 'key3': 'ccc', 'key4': 'ddd'}\nd.update({'key4': 'ddd', 'key5': 'eee'}) # \u4e00\u62ec\u66f4\u65b0\uff08\u7d50\u5408\uff09\n# > {'key1': 'aaa', 'key2': 'bbb', 'key3': 'ccc', 'key4': 'ddd', 'key5': 'eee'}\ndel(d['key3']) #\u524a\u9664\n# > {'key1': 'aaa', 'key2': 'bbb', 'key4': 'ddd', 'key5': 'eee'}\ndict(filter(lambda item: item[0] == 'key1', d.items())) # filter -> dict\n# > {'key1': 'xxx'}\nlist(map(lambda item: item[0], d.items())) # map -> list\n# > ['key1', 'key2', 'key4', 'key5']\nd.clear() #\u5168\u524a\u9664\n\n# set to list\ns: Set[int] = set({1, 2, 3, 2, 1})\nl: List[int] = list(s)\n\n# list to set\nl: List[int] = [1, 2, 3]\ns: Set[int] = set(l)\n<\/code><\/pre>\n__eq__<\/code>\u30e1\u30bd\u30c3\u30c9\u3068__hash__<\/code>\u30e1\u30bd\u30c3\u30c9<\/h5>\n\n__eq__<\/code>\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u540c\u58eb\u306e\u540c\u4e00\u6027\u3092\u5224\u5b9a\u3059\u308b\u305f\u3081\u306e\u30e1\u30bd\u30c3\u30c9\u3067==<\/code>\u3067\u306e\u6bd4\u8f03\u3084list\u306a\u3044\u306e\u5b58\u5728\u78ba\u8a8d\u306a\u3069\u3067\u4f7f\u308f\u308c\u307e\u3059\u3002<\/li>\n__hash__<\/code>\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u540c\u4e00\u6027\u3092\u8868\u3059hash\u5024\u3092\u8fd4\u3059\u30e1\u30bd\u30c3\u30c9\u3067\u3001__eq__<\/code>\u304ctrue\u3092\u8fd4\u3059\u7bc4\u56f2\u306f\u540c\u3058\u5024\u3092\u8fd4\u3059\u3088\u3046\u306b\u5b9f\u88c5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u306fdict\u306ekey\u306a\u3069\u3067\u4f7f\u308f\u308c\u307e\u3059\u3002<\/li>\n<\/ul>\nimport dataclasses\n\n@dataclasses.dataclass(frozen=True)\nclass Test:\n hoge: str\n fuga: str\n def __eq__(self, other):\n return self.hoge == other.hoge # hoge\u3060\u3051\u3067\u540c\u4e00\u6027\u3092\u5224\u5b9a\n def __hash__(self):\n return hash(self.hoge) # hoge\u3060\u3051\u3092\u5143\u306bhash\u3092\u8fd4\u3059\n\ntest1 = Test('a1', 'b1')\ntest2 = Test('a1', 'b2')\ntest3 = Test('a1', 'b3')\ntest4 = Test('a2', 'b3')\n\ntest1 == test2 == test3\n# > True # hoge\u304c\u4e00\u81f4\u3057\u3066\u3044\u308b\u306e\u3067true\ntest3 == test4\n# > False # hoge\u304c\u4e0d\u4e00\u81f4\u306a\u306e\u3067false\n\nl = [test1, test2]\ntest3 in l\n# > True # hoge='a1'\u306e\u8981\u7d20\u304c\u3044\u308b\u306e\u3067True\nl = [test1, test2]\ntest4 in l\n# > False # hoge='a1'\u306e\u8981\u7d20\u304c\u3044\u306a\u3044\u306e\u3067False\n\nd = {test1: 111}\n# > {Test(hoge='a1', fuga='b1'): 111}\nd[test2] = 222\n# > {Test(hoge='a1', fuga='b1'): 222} # test1\u3068test2\u306f\u540c\u3058hash\u3092\u8fd4\u3059\u306e\u3067\u3001\u4e0a\u66f8\u304d\u3055\u308c\u308b\nd[test4] = 444\n# > {Test(hoge='a1', fuga='b1'): 222, Test(hoge='a2', fuga='b3'): 444} # test4\u306f\u5225\u306ehash\u306a\u306e\u3067\u5225\u3067\u767b\u9332\u3055\u308c\u308b\n<\/code><\/pre>\n\u6642\u9593\u3092\u8a08\u6e2c\u3059\u308b<\/h3>\nimport time\n\nstarted_time = time.time()\n# \u4f55\u3089\u304b\u306e\u51e6\u7406\nfinished_time = time.time()\nelapsed_time = finished_time - started_time\nprint("elapsed_time: {} sec".format(elapsed_time))<\/code><\/pre>\n\u5909\u6570\u306e\u30e1\u30e2\u30ea\u30b5\u30a4\u30ba\u3092\u78ba\u8a8d\u3059\u308b<\/h3>\n
sys.getsizeof()<\/code>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\nimport sys\n\ntexts = []\nfor i in range(10000):\n texts.append('{:x=10}'.format(i))\nprint(sys.getsizeof(texts))\n# -> 72<\/code><\/pre>\nStringIO\u3068BytesIO\u3092\u4f7f\u3046<\/h3>\n
\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306f\u3053\u3061\u3089<\/a><\/p>\n\n- StringIO\n
\nfrom io import StringIO\nbuffer: StringIO = StringIO('aaa\\nbbb\\nccc')<\/code><\/pre>\n<\/li>\n<\/ul>\nreadline()\u30671\u4ef6\u305a\u3064<\/h1>\n
line: str = buffer.readline()
\nwhile line:
\nprint(line)
\nline = buffer.readline()<\/p>\n
readlines()\u3067\u307e\u3068\u3081\u3066<\/h1>\n
for line in buffer.readlines():
\nprint(line)<\/p>\n
\n* BytesIO\n```python\nfrom io import BytesIO, TextIOWrapper\nbuffer: BytesIO = getBytesIO() #\u306a\u3093\u3089\u304b\u306e\u51e6\u7406\u3067ByteIO\u3092\u53d6\u5f97\nwrapper: TextIOWrapper = TextIOWrapper(buffer, encoding='utf-8')\n# \u3042\u3068\u306fStringIO\u3068\u540c\u3058\u3084\u308a\u65b9\u3067\u30c6\u30ad\u30b9\u30c8\u3092\u53d6\u5f97\u3067\u304d\u308b<\/code><\/pre>\n\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u4e00\u6c17\u306b\u4f5c\u6210\u3059\u308b<\/h3>\nimport os\ndir_path: str = '\/tmp\/hoge\/fuga'\nos.makedirs(dir_path, exist_ok=True)\n# exist_ok=True\u3092\u6307\u5b9a\u3057\u306a\u3044\u3068\u3001\u3059\u3067\u306b\u5b58\u5728\u3057\u3066\u305f\u3089\u30a8\u30e9\u30fc\u306b\u306a\u308b<\/code><\/pre>\nCSV\u30d5\u30a1\u30a4\u30eb\u306e\u51fa\u529b<\/h3>\n
csv \u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n
\n- CSV\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\n
import csv\nwith open('\/tmp\/hoge\/fuga.csv', 'r') as f:\n reader = csv.reader(f)\n for row in reader:\n # row: List[str]\n print(row)\n# > ['aa1', 'bb1', 'cc1']\n# > ['aa2', 'bb2', 'cc2']\n# > ['aa3', 'bb3', 'cc3']<\/code><\/pre>\n<\/li>\n- CSV\u30d5\u30a1\u30a4\u30eb\u306b\u66f8\u304d\u51fa\u3059\n
import csv\nwith open('\/tmp\/hoge\/fuga.csv', 'w') as f:\n writer = csv.writer(f)\n writer.writerow(['aa1', 'bb1', 'cc1'])\n writer.writerow(['aa2', 'bb2', 'cc2'])\n writer.writerow(['aa3', 'bb3', 'cc3'])<\/code><\/pre>\n<\/li>\n<\/ul>\nunicode\u306e\u307e\u307eexcel\u3067\u3082\u958b\u3051\u308b\u5f62\u3067\u51fa\u529b\u3059\u308b\u5834\u5408\u306f\u3001\u3053\u3061\u3089<\/a>\u3092\u53c2\u8003\u306b\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3059\u308b\u3068\u826f\u3044\u305d\u3046\u3067\u3059\u3002<\/p>\nimport csv\nwith open('\/tmp\/hoge\/fuga.tsv', 'w', 'encoding='utf-16'') as f:\n writer = csv.writer(f, dialect='excel-tab', quoting=csv.QUOTE_ALL)\n writer.writerow(['\u3042\u30421', '\u3044\u30441', '\u3046\u30461'])\n writer.writerow(['\u3042\u30422', '\u3044\u30442', '\u3046\u30462'])\n writer.writerow(['\u3042\u30423', '\u3044\u30443', '\u3046\u30463'])<\/code><\/pre>\n\u3061\u306a\u307f\u306b\u3001encoding\u3092'shift_jis'\u3084'cp932'\u306b\u3059\u308b\u624b\u3082\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u5927\u91cf\u30c7\u30fc\u30bf\u3092\u6271\u3063\u3066\u3044\u308b\u5834\u5408\u3001UnicodeEncodeError<\/code>\u304c\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u3001\u3053\u308c\u306e\u5bfe\u51e6\u3092\u3059\u308b\u306e\u304c\u5927\u5909\u306a\u306e\u3067\u81ea\u5206\u306f\u3084\u3081\u307e\u3057\u305f\u3002<\/p>\nfor\u6587\u3067loop\u51e6\u7406\u304c\u7d42\u308f\u3063\u305f\u5f8c\u306b\u51e6\u7406\u3092\u5b9f\u884c\u3055\u305b\u308b\u65b9\u6cd5<\/h3>\n
for\u6587\u304cloop\u304c\u7d42\u308f\u3063\u305f\u5f8c\u306b\u4f55\u3089\u304b\u306e\u51e6\u7406\u3092\u884c\u3044\u305f\u3044\u5834\u5408\u306f\u3001for-else\u6587\u3092\u4f7f\u3044\u307e\u3059\u3002\u305f\u3060\u3057else\u30d6\u30ed\u30c3\u30af\u306fbreak\u3057\u305f\u5834\u5408\u306b\u306f\u5b9f\u884c\u3055\u308c\u307e\u305b\u3093\u3002
\nbreak\u305b\u305a\u306bloop\u304c\u5168\u3066\u6b63\u5e38\u306b\u7d42\u308f\u3063\u305f\u5834\u5408\u3060\u3051\u4f55\u304b\u51e6\u7406\u3092\u884c\u3044\u305f\u3044\u5834\u5408\u306b\u5229\u7528\u3059\u308b\u306e\u304c\u3044\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n
# loop\u5b8c\u4e86\u5f8c\u306belse\u304c\u5b9f\u884c\u3055\u308c\u308b\nfor i in [1, 2]:\n print(i)\nelse:\n print(9)\n# > 1\n# > 2\n# > 9\n\n# \u9014\u4e2d\u3067break\u3059\u308b\u3068else\u304c\u5b9f\u884c\u3055\u308c\u306a\u3044\nfor i in [1, 2]:\n print(i)\n if i == 2:\n break\nelse:\n print(9)\n# > 1\n# > 2\n\n# loop\u5bfe\u8c61\u304c0\u4ef6\u306e\u5834\u5408\u3082else\u304c\u5b9f\u884c\u3055\u308c\u308b\nfor i in []:\n print(i)\nelse:\n print(9)\n# > 9<\/code><\/pre>\n\u5b9f\u884c\u4e2d\u306e\u95a2\u6570\u540d\u3092\u53d6\u5f97\u3059\u308b<\/h3>\nimport sys\n\ndef hoge():\n print(sys._getframe().f_code.co_name)\n\nhoge()\n# > hoge<\/code><\/pre>\n\u30d5\u30a1\u30a4\u30eb\u306e\u5148\u982d\u306b\u884c\u3092\u8ffd\u52a0\u3059\u308b<\/h3>\n
python\u3067\u306f\u30d5\u30a1\u30a4\u30eb\u306e\u5148\u982d\u306b\u884c\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u306f\u3001\u3059\u3093\u306a\u308a\u3068\u306f\u3067\u304d\u306a\u3044\u305d\u3046\u3067\u3059\u3002
\n\u3057\u3087\u3046\u304c\u306a\u3044\u306e\u3067\u3001\u65e2\u5b58\u306e\u30d5\u30a1\u30a4\u30eb\u306e\u5185\u5bb9\u3092\u53d6\u5f97\u3057\u3066\u5148\u982d\u306b\u884c\u3092\u8ffd\u52a0\u3057\u3066\u3001\u540c\u540d\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u4e0a\u66f8\u304d\u3057\u307e\u3059\u3002<\/p>\n
from typing import List\ndef add_header_line(file_path: str, header: str):\n with open(file_path) as f:\n lines: List[str] = f.readlines()\n lines.insert(0, header + '\\n')\n with open(file_path, mode='w') as f:\n f.writelines(lines)\n\nadd_header('\/hoge\/fuga\/text.txt')<\/code><\/pre>\n\u8f9e\u66f8\uff08dict\uff09\u306emap\u3084filter<\/h3>\n
dict\u306b\u306fkey\u3067\u30a2\u30af\u30bb\u30b9\u3059\u308b\u306e\u304c\u9244\u5247\u3060\u3051\u3069\u3001\u3069\u3046\u3057\u3066\u3082map\u3084filter\u3092\u3084\u308a\u305f\u304f\u306a\u3063\u305f\u3089\u4ee5\u4e0b\u306e\u3088\u3046\u306bitems()<\/code>\u3092\u4f7f\u3046\u3068\u5b9f\u73fe\u3067\u304d\u308b\u3002\u305f\u3060\u3057\u9045\u3044\u3002<\/p>\nfrom typing import Dict, Set\n\n# \u30d9\u30fc\u30b9\u8f9e\u66f8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\nsrc_dict: Dict[str, Set[int]] = {'a': {1, 2, 3}, 'b': {4, 5}, 'c': {6, 7, 8}}\n# > {'a': {1, 2, 3}, 'b': {4, 5}, 'c': {8, 6, 7}}\n\n# map\u306e\u4f8b\nmapped_dict: Dict[str, int] = dict((k, len(v)) for k, v in src_dict.items())\n# > {'a': 3, 'b': 2, 'c': 3}\n\n# filter\u306e\u4f8b\nfiltered_dict_1: Dict[str, Set[int]] = dict((k, v) for k, v in src_dict.items() if len(v) > 2)\nfiltered_dict_2: Dict[str, Set[int]] = dict(filter(lambda kv: len(kv[1]) > 2, src_dict.items()))\n# > {'a': {1, 2, 3}, 'c': {8, 6, 7}}\n<\/code><\/pre>\n\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\uff08Generator\u3068AsyncGenerator\uff09<\/h3>\n
\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306f\u3001\u5024\u3092\u8fd4\u3059\u4ee3\u308f\u308a\u306b\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\uff08\u4e00\u9023\u306e\u5024\u3092\u8fd4\u3059\u7279\u6b8a\u306a\u30a4\u30c6\u30ec\u30fc\u30bf\uff09\u3092\u8fd4\u3059\u95a2\u6570\u3067\u3001yield\u30ad\u30fc\u30ef\u30fc\u30c9\u3092\u542b\u3080\u95a2\u6570\u306f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u3067\u3059\u3002\u5225\u8a18\u4e8b\u3092\u66f8\u3044\u3066\u307e\u3059\u3002
\npython\u306eGenerator\u3068AsyncGenerator\u306e\u4f7f\u3044\u65b9<\/a><\/p>\nsubprocess\u3067\u30b3\u30de\u30f3\u30c9\u5b9f\u884c<\/h3>\n
\u5358\u7d14\u306b\u5b9f\u884c\u3059\u308b\u3060\u3051\u306a\u3089\u3001call<\/code>\u3084run<\/code>\u3092\u4f7f\u3044\u307e\u3059<\/p>\nfrom subprocess import run, CompletedProcess\n\nprocess: CompletedProcess = run(["ls", "-la"], capture_output=True)\nprint(process.stdout.decode('utf-8'))<\/code><\/pre>\n\u51e6\u7406\u7d50\u679c\u3092\u30d1\u30a4\u30d7\u3067\u3064\u306a\u3050\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306bPopen\uff08Process Open\u306e\u7565\u3060\u3068\u601d\u3046\uff09\u3068PIPE\u3068communicate\u3092\u4f7f\u3046\u3088\u3046\u3067\u3059\u3002<\/p>\n
from subprocess import Popen, PIPE\n\np1: Popen = Popen(['ps', 'aux'], stdout=PIPE)\np2: Popen = Popen(['grep', 'python'], stdin=p1.stdout, stdout=PIPE)\n\nstdout, stderr = p2.communicate()\nprint(stdout.decode('utf-8'))<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u4ed5\u4e8b\u3067python\u3092\u89e6\u308b\u6a5f\u4f1a\u304c\u4eca\u5f8c\u5897\u3048\u305d\u3046\u306a\u306e\u3067\u3001python\u306b\u95a2\u3059\u308b\u30e1\u30e2\u3092\u305f\u3081\u3066\u3044\u304f\u4e88\u5b9a\u3067\u3059\u3002 Python\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u7ba1\u7406\uff08pyenv\uff09 pyenv\u3092\u4f7f\u3063\u3066python\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059 # pyenv\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb brew install pyenv pyenv -v > pyenv 1.2.13 # \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u53ef\u80fd\u306apython\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u78ba\u8a8d pyenv install –list # \u7279\u5b9a\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306epython\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb pyenv install 3.7.4 # \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u6e08\u307f\u306epython\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u78ba\u8a8d pyenv versions > * system (set by \/Users\/rinoguchi\/.pyenv\/version) > 3.7.4 # ~\/.bash_profile\u306b\u4ee5\u4e0b\u306e\u8a2d\u5b9a\u3092\u8ffd\u52a0\uff08syms\u3068autocompletion\u306e\u6709\u52b9\u5316\uff09 <\/span>Continue Reading<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/361"}],"collection":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/comments?post=361"}],"version-history":[{"count":1,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/361\/revisions"}],"predecessor-version":[{"id":362,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/361\/revisions\/362"}],"wp:attachment":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media?parent=361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/categories?post=361"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tags?post=361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}