{"id":52,"date":"2015-09-21T17:51:49","date_gmt":"2015-09-21T15:51:49","guid":{"rendered":"http:\/\/mickael.bucas.name\/blog\/?p=52"},"modified":"2019-01-31T12:38:29","modified_gmt":"2019-01-31T11:38:29","slug":"converting-a-python-project-to-pep8-with-shell","status":"publish","type":"post","link":"https:\/\/mickael.bucas.name\/blog\/2015\/09\/converting-a-python-project-to-pep8-with-shell\/","title":{"rendered":"Converting a Python project to PEP8 with Shell"},"content":{"rendered":"<p>When I started Python I didn&#8217;t learn PEP8 immediately so my code was not following the standard format. As I know Bash quite well, I&#8217;ve converted my projects later, with the following commands.<\/p>\n<p>Convert tabs to 4 spaces<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">find . -name '*.py'|xargs pep8|grep W191|cut -d : -f1|xargs sed -i \"s\/\\t\/    \/g\"<\/pre>\n<p>Suppress spaces when they&#8217;re alone on the line<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">find . -name '*.py'|xargs pep8|grep W293|cut -d : -f1|sort -u|xargs sed -i \"s\/^ *$\/\/\"<\/pre>\n<p>Suppress spaces at end of line<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">find . -name '*.py'|xargs pep8|grep W291|cut -d : -f1|sort -u|xargs sed -i \"s\/ *$\/\/\"<\/pre>\n<p>Add spaces after &#8220;:&#8221;, &#8220;,&#8221;<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">find . -name '*.py'|xargs pep8|grep E231|cut -d : -f1|sort -u|xargs sed -i 's\/:\\(&#x5B;^ ]\\)\/: \\1\/g'\nfind . -name '*.py'|xargs pep8|grep E231|cut -d : -f1|sort -u|xargs sed -i 's\/,\\(&#x5B;^ ]\\)\/, \\1\/g'<\/pre>\n<p>Suppress spaces before or after {} [] ()<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">find . -name '*.py'|xargs pep8|grep E201|cut -d : -f1|sort -u|xargs sed -i 's\/{ *\/{\/g'\nfind . -name '*.py'|xargs pep8|grep E201|cut -d : -f1|sort -u|xargs sed -i 's\/\\&#x5B; *\/&#x5B;\/g'\nfind . -name '*.py'|xargs pep8|grep E201|cut -d : -f1|sort -u|xargs sed -i 's\/( *\/(\/g'\nfind . -name '*.py'|xargs pep8|grep E202|cut -d : -f1|sort -u|xargs sed -i 's\/ *}\/}\/g'\nfind . -name '*.py'|xargs pep8|grep E202|cut -d : -f1|sort -u|xargs sed -i 's\/ *]\/]\/g'\nfind . -name '*.py'|xargs pep8|grep E202|cut -d : -f1|sort -u|xargs sed -i 's\/ *)\/)\/g'<\/pre>\n<p>Suppress spaces before &#8220;(&#8220;, &#8220;:&#8221;<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">find . -name '*.py'|xargs pep8|grep E211|cut -d : -f1|sort -u|xargs sed -i 's\/ *(\/(\/g'\nfind . -name '*.py'|xargs pep8|grep E203|cut -d : -f1|sort -u|xargs sed -i 's\/ *:\/:\/g'<\/pre>\n<p>Add a blank line at end of file<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">find . -name '*.py'|xargs pep8|grep W292|cut -d: -f1|while read f; do echo &gt;&gt; $f; done<\/pre>\n<p>Add a blank line before &#8220;def&#8221; or &#8220;class&#8221;, to be run two times<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">find . -name '*.py'|xargs pep8|grep E302|cut -d : -f1|sort -u|xargs sed -i 's\/^def\/\\ndef\/'\nfind . -name '*.py'|xargs pep8|grep E302|cut -d : -f1|sort -u|xargs sed -i 's\/^class\/\\nclass\/'<\/pre>\n<p>Sometimes I don&#8217;t really want to cut long lines, so I keep lines up to 120 characters<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">find . -name '*.py'|xargs pep8 --max-line-length=120<\/pre>\n<p>I think that 80 is to restrictive and based on terminals of the last century or on paper sizes that no one uses any more.<\/p>\n<p>However, I&#8217;ve learned how to cut long lines with chained calls.<\/p>\n<p>Before:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">compte.dernier_solde = SoldeJour.objects.filter(compte_id=compte.id).order_by('-date_solde')&#x5B;0].solde<\/pre>\n<p>After, with parenthesis to tell the Python parser that a single expression is on multiple lines:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">compte.dernier_solde = (\nSoldeJour\n.objects\n.filter(compte_id=compte.id)\n.order_by('-date_solde')&#x5B;0]\n.solde\n)\n<\/pre>\n<p>I&#8217;ve also configured Scite, my favorite editor, for spaces and tabs.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">use.tabs=0\ntabsize=4\nindent.size=4<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When I started Python I didn&#8217;t learn PEP8 immediately so my code was not following the standard format. As I know Bash quite well, I&#8217;ve converted my projects later, with the following commands. Convert tabs to 4 spaces find . -name &#8216;*.py&#8217;|xargs pep8|grep W191|cut -d : -f1|xargs sed -i &#8220;s\/\\t\/ \/g&#8221; Suppress spaces when they&#8217;re&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,10],"tags":[],"class_list":["post-52","post","type-post","status-publish","format-standard","hentry","category-linux","category-python"],"_links":{"self":[{"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/posts\/52","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":5,"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":94,"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/posts\/52\/revisions\/94"}],"wp:attachment":[{"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}