{"componentChunkName":"component---src-templates-post-js","path":"/standardize-your-python/","result":{"data":{"ghostPost":{"id":"Ghost__Post__5f2af1e7915592001e6f18be","title":"Standardize your Python","slug":"standardize-your-python","featured":false,"feature_image":"https://images.unsplash.com/photo-1589009602939-0351d5180c5b?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=2000&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ","excerpt":"Tools and tips for standardizing your team's Python","custom_excerpt":"Tools and tips for standardizing your team's Python","visibility":"public","created_at_pretty":"05 August, 2020","published_at_pretty":"12 August, 2020","updated_at_pretty":"14 August, 2020","created_at":"2020-08-05T17:52:39.000+00:00","published_at":"2020-08-12T18:23:11.000+00:00","updated_at":"2020-08-14T02:04:39.000+00:00","meta_title":null,"meta_description":null,"og_description":null,"og_image":null,"og_title":null,"twitter_description":null,"twitter_image":null,"twitter_title":null,"authors":[{"name":"Zachary Blackwood","slug":"blackary","bio":null,"profile_image":"//www.gravatar.com/avatar/2ffb6bd2316ddbee8cd251ff0dc9dde1?s=250&d=mm&r=x","twitter":null,"facebook":null,"website":"https://blackary.com"}],"primary_author":{"name":"Zachary Blackwood","slug":"blackary","bio":null,"profile_image":"//www.gravatar.com/avatar/2ffb6bd2316ddbee8cd251ff0dc9dde1?s=250&d=mm&r=x","twitter":null,"facebook":null,"website":"https://blackary.com"},"primary_tag":{"name":"Python","slug":"python","description":null,"feature_image":null,"meta_description":null,"meta_title":null,"visibility":"public"},"tags":[{"name":"Python","slug":"python","description":null,"feature_image":null,"meta_description":null,"meta_title":null,"visibility":"public"},{"name":"python standardization","slug":"python-standardization","description":null,"feature_image":null,"meta_description":null,"meta_title":null,"visibility":"public"}],"plaintext":"A little background: I've been a Python developer for just over 5 years. I\nstarted off as a web developer, and have transitioned into an operational data\nscientist in the past couple of years.\n\nI really enjoy programming in Python. I enjoyed learning it on my own, and\nteaching some Python-based intro-to-programming classes in my previous life as a\nHigh School teacher. \n\nOne of the wonderful things about Python is the many different ways that it can\nbe written and used. Just the fact that I was able to transition from web\ndevelopment to data science within the same language is a testament to that\nfact. Different development styles can be used – just writing scripts meant to\nbe run individually, or a whole suite of modules and classes to be imported and\nintegrated together.\n\nBut, one of the frustrating things in working with a team of developers is\nhaving to work with other programmer's styles. Here are a few tips for\nstandardizing your team's Python code.\n\nYou can follow along in the examples by pulling the code from \nhttps://github.com/blackary/standardization\n\nIn this post, I want to give a brief introduction to some of the guides and\ntools which I have found useful for standardizing Python on the teams I've\nworked on. \n\nPEP 8 [https://www.python.org/dev/peps/pep-0008/]\nThe most famous of the PEPs (Python Enhancement Proposals), PEP 8 describes the\nofficial style guidelines which should be followed when writing Python code.\nEven just having everyone on your team read through this document can be a great\nfirst step in standardizing your style. If you have disagreements with some\nparts, and think some pieces will be different, that's OK. Start with PEP 8 and\ntweak what you want to tweak (or ignore what you want to ignore) as a team, and\nrun with it. Or just use it to start a discussion about how you as a team want\nto style your code. \n\nflake8 [https://flake8.pycqa.org/en/latest/]\nOne of the first difficulties in actually applying PEP 8 is that there is a lot\nto remember. It would be nice to have some automated way to check and see if\nyou're actually following the style rules. Enter flake8. \n\nFlake8 is. a linter [https://en.wikipedia.org/wiki/Lint_(software)], which\nchecks your code for PEP 8 style conventions, and also various common mistakes\n(like importing a library but not using it), Syntax Errors, etc.\n\nFor example, given the following code in bad.py: \n\na= \"way waywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywayway too long of a line\"\n\nimport something_unused\n\n6 = x\n\ny = [1,2, 3,\n        4,\n5]\n\n\nRunning flake8 bad.py returns the following errors:\n\n$ flake8 bad.py\nbad.py:1:2: E225 missing whitespace around operator\nbad.py:1:80: E501 line too long (160 > 79 characters)\nbad.py:3:1: E402 module level import not at top of file\nbad.py:5:1: E999 SyntaxError: cannot assign to literal\nbad.py:7:7: E231 missing whitespace after ','\nbad.py:8:9: E127 continuation line over-indented for visual indent\nbad.py:9:1: E128 continuation line under-indented for visual indent\n\n\nThis can be run manually as above, but is even more useful if it's run\nautomatically. For example, when you commit code to a repository, through a \npre-commit hook [https://flake8.pycqa.org/en/latest/user/using-hooks.html], or\nit can be installed as a plug-in on your editor (VS Code\n[https://code.visualstudio.com/docs/python/linting#_specific-linters], vim\n[https://github.com/nvie/vim-flake8], etc.), to add underlines around code which\nviolates styles guides or has a syntax error. \n\nBut, what about cases where PEP 8 has multiple valid formatting options? Enter\nBlack.\n\nBlack [https://github.com/psf/black] [https://github.com/psf/black]\nWhat should you do when there are disagreements which both fit within the PEP 8\nguide? In particular, things like where to place closing brackets or braces,\nsingle-vs-double quotes, etc. Black is a Python formatter which is very\nopinionated, and takes a stand even on cases where PEP 8 leaves several options\nopen.\n\nHere is an example from PEP 8 itself of multiple valid styles:\n\nimport some_function_that_takes_arguments\n\n\nmy_list = [\n    1, 2, 3,\n    4, 5, 6,\n    ]\nmy_list = [\n    1, 2, 3,\n    4, 5, 6,\n]\nresult = some_function_that_takes_arguments(\n    'a', 'b', 'c',\n    'd', 'e', 'f',\n    )\nresult = some_function_that_takes_arguments(\n    'a', 'b', 'c',\n    \"d\", \"e\", \"f\",\n)\n\n\nflake8 valid_options.py yields no output, signaling that this is valid code as\nfar as Flake8 is concerned. However, running it through Black shows how it would\nbe reformatted. Just running black {filename} would actually reformat the file\nin-place, so for demonstration sake, I will use black - which takes code in from\nstandard input and writes to standard output the resulting reformatted result.\n\n$ cat valid_options.py | black -\nmy_list = [\n    1,\n    2,\n    3,\n    4,\n    5,\n    6,\n]\nmy_list = [\n    1,\n    2,\n    3,\n    4,\n    5,\n    6,\n]\nresult = some_function_that_takes_arguments(\"a\", \"b\", \"c\", \"d\", \"e\", \"f\",)\nresult = some_function_that_takes_arguments(\"a\", \"b\", \"c\", \"d\", \"e\", \"f\",)\nreformatted -\nAll done! ✨ 🍰 ✨\n1 file reformatted.\n\n\nAny code that has been formatted by Black should also successfully pass the\nflake8 test.\n\nJust like flake8, Black can be run as a command-line function, installed as an\neditor plugin (VS Code\n[https://code.visualstudio.com/docs/python/editing#_formatterspecific-settings], \nvim [https://github.com/psf/black/blob/master/docs/editor_integration.md#vim],\netc.) or through a pre-commit hook\n[https://black.readthedocs.io/en/stable/version_control_integration.html].\n\nConclusion\nI hope these tools are helpful for you in creating more consistent code for you\nand your team.","html":"<p>A little background: I've been a Python developer for just over 5 years. I started off as a web developer, and have transitioned into an operational data scientist in the past couple of years.</p><p>I really enjoy programming in Python. I enjoyed learning it on my own, and teaching some Python-based intro-to-programming classes in my previous life as a High School teacher. </p><p>One of the wonderful things about Python is the many different ways that it can be written and used. Just the fact that I was able to transition from web development to data science within the same language is a testament to that fact. Different development styles can be used – just writing scripts meant to be run individually, or a whole suite of modules and classes to be imported and integrated together.</p><p>But, one of the frustrating things in working with a team of developers is having to work with other programmer's styles. Here are a few tips for standardizing your team's Python code.</p><p>You can follow along in the examples by pulling the code from <a href=\"https://github.com/blackary/standardization\">https://github.com/blackary/standardization</a></p><p>In this post, I want to give a brief introduction to some of the guides and tools which I have found useful for standardizing Python on the teams I've worked on. </p><h2 id=\"pep-8\"><a href=\"https://www.python.org/dev/peps/pep-0008/\">PEP 8</a></h2><p>The most famous of the PEPs (Python Enhancement Proposals), PEP 8 describes the official style guidelines which should be followed when writing Python code. Even just having everyone on your team read through this document can be a great first step in standardizing your style. If you have disagreements with some parts, and think some pieces will be different, that's OK. Start with PEP 8 and tweak what you want to tweak (or ignore what you want to ignore) as a team, and run with it. Or just use it to start a discussion about how you as a team want to style your code. </p><h2 id=\"flake8\"><a href=\"https://flake8.pycqa.org/en/latest/\">flake8</a></h2><p>One of the first difficulties in actually applying PEP 8 is that there is a lot to remember. It would be nice to have some automated way to check and see if you're actually following the style rules. Enter flake8. </p><p>Flake8 is. a <a href=\"https://en.wikipedia.org/wiki/Lint_(software)\">linter</a>, which checks your code for PEP 8 style conventions, and also various common mistakes (like importing a library but not using it), Syntax Errors, etc.</p><p>For example, given the following code in bad.py: </p><!--kg-card-begin: markdown--><pre><code>a= &quot;way waywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywaywayway too long of a line&quot;\n\nimport something_unused\n\n6 = x\n\ny = [1,2, 3,\n        4,\n5]\n</code></pre>\n<!--kg-card-end: markdown--><p>Running <code>flake8 bad.py</code> returns the following errors:</p><!--kg-card-begin: markdown--><pre><code>$ flake8 bad.py\nbad.py:1:2: E225 missing whitespace around operator\nbad.py:1:80: E501 line too long (160 &gt; 79 characters)\nbad.py:3:1: E402 module level import not at top of file\nbad.py:5:1: E999 SyntaxError: cannot assign to literal\nbad.py:7:7: E231 missing whitespace after ','\nbad.py:8:9: E127 continuation line over-indented for visual indent\nbad.py:9:1: E128 continuation line under-indented for visual indent\n</code></pre>\n<!--kg-card-end: markdown--><p>This can be run manually as above, but is even more useful if it's run automatically. For example, when you commit code to a repository, through a <a href=\"https://flake8.pycqa.org/en/latest/user/using-hooks.html\">pre-commit hook</a>, or it can be installed as a plug-in on your editor (<a href=\"https://code.visualstudio.com/docs/python/linting#_specific-linters\">VS Code</a>, <a href=\"https://github.com/nvie/vim-flake8\">vim</a>, etc.), to add underlines around code which violates styles guides or has a syntax error. </p><figure class=\"kg-card kg-image-card\"><img src=\"https://res-3.cloudinary.com/hfhiab0iy/image/upload/q_auto/v1/ghost-blog-images/Screen-Shot-2020-08-13-at-10.03.41-PM.png\" class=\"kg-image\"></figure><p>But, what about cases where PEP 8 has multiple valid formatting options? Enter Black.</p><h2 id=\"black\"><a href=\"https://github.com/psf/black\">B<a href=\"https://github.com/psf/black\">lack</a></a></h2><p>What should you do when there are disagreements which both fit within the PEP 8 guide? In particular, things like where to place closing brackets or braces, single-vs-double quotes, etc. Black is a Python formatter which is very opinionated, and takes a stand even on cases where PEP 8 leaves several options open.</p><p>Here is an example from PEP 8 itself of multiple valid styles:</p><!--kg-card-begin: markdown--><pre><code>import some_function_that_takes_arguments\n\n\nmy_list = [\n    1, 2, 3,\n    4, 5, 6,\n    ]\nmy_list = [\n    1, 2, 3,\n    4, 5, 6,\n]\nresult = some_function_that_takes_arguments(\n    'a', 'b', 'c',\n    'd', 'e', 'f',\n    )\nresult = some_function_that_takes_arguments(\n    'a', 'b', 'c',\n    &quot;d&quot;, &quot;e&quot;, &quot;f&quot;,\n)\n</code></pre>\n<!--kg-card-end: markdown--><p><code>flake8 valid_options.py</code> yields no output, signaling that this is valid code as far as Flake8 is concerned. However, running it through Black shows how it would be reformatted. Just running <code>black {filename}</code> would actually reformat the file in-place, so for demonstration sake, I will use <code>black -</code> which takes code in from standard input and writes to standard output the resulting reformatted result.</p><!--kg-card-begin: markdown--><pre><code>$ cat valid_options.py | black -\nmy_list = [\n    1,\n    2,\n    3,\n    4,\n    5,\n    6,\n]\nmy_list = [\n    1,\n    2,\n    3,\n    4,\n    5,\n    6,\n]\nresult = some_function_that_takes_arguments(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;, &quot;f&quot;,)\nresult = some_function_that_takes_arguments(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;, &quot;f&quot;,)\nreformatted -\nAll done! ✨ 🍰 ✨\n1 file reformatted.\n</code></pre>\n<!--kg-card-end: markdown--><p>Any code that has been formatted by Black should also successfully pass the flake8 test.</p><p>Just like flake8, Black can be run as a command-line function, installed as an editor plugin (<a href=\"https://code.visualstudio.com/docs/python/editing#_formatterspecific-settings\">VS Code</a>, <a href=\"https://github.com/psf/black/blob/master/docs/editor_integration.md#vim\">vim</a>, etc.) or through a <a href=\"https://black.readthedocs.io/en/stable/version_control_integration.html\">pre-commit hook</a>.</p><h2 id=\"conclusion\">Conclusion</h2><p>I hope these tools are helpful for you in creating more consistent code for you and your team.</p>","url":"https://blackary-blog.herokuapp.com/standardize-your-python/","canonical_url":null,"uuid":"554d498f-1418-47c0-be6a-2116c0e536a8","page":null,"codeinjection_foot":null,"codeinjection_head":null,"codeinjection_styles":null,"comment_id":"5f2af1e7915592001e6f18be","reading_time":4}},"pageContext":{"slug":"standardize-your-python"}},"staticQueryHashes":["2358152166","2561578252","2731221146","3001408369","4145280475"]}