{"id":4793,"date":"2013-05-10T14:34:24","date_gmt":"2013-05-10T22:34:24","guid":{"rendered":"http:\/\/www.chesnok.com\/daily\/?p=4793"},"modified":"2013-05-10T14:35:33","modified_gmt":"2013-05-10T22:35:33","slug":"til-formatting-search_path-and-colorcolumn","status":"publish","type":"post","link":"https:\/\/www.chesnok.com\/daily\/2013\/05\/10\/til-formatting-search_path-and-colorcolumn\/","title":{"rendered":"TIL: Formatting, search_path and colorcolumn"},"content":{"rendered":"<p>The last six months have involved a lot more writing of code than the previous couple of years.<\/p>\n<p>I&#8217;ve been tweeting little things I learn on a daily basis and thought I&#8217;d look back on this week.<\/p>\n<h1>format()<\/h1>\n<p>A reocurring problem with report writing is getting numbers formatted properly for the occassion. I discovered &#8216;format&#8217; in Python this week:<\/p>\n<pre><code>print \"{0:.2f}%\".format(float(1)\/3 * 100)\r\n<\/code><\/pre>\n<p>That prints out a float to 2 decimal places. I looked around and <a href=\"http:\/\/www.diveintopython.net\/native_data_types\/formatting_strings.html\">Dive Into Python<\/a> has similar syntax, but without the format() function. So, the equivalent would be:<\/p>\n<pre><code>print \"blah %.2f\" % (float(1) \/ 3 * 100)\r\n<\/code><\/pre>\n<p>So, why use one over the other? A user on <a href=\"http:\/\/stackoverflow.com\/questions\/5082452\/python-string-formatting-vs-format\">StackOverflow<\/a> suggested that compatibility with 2.5 might drive a person to use &#8216;%&#8217; over &#8216;format()&#8217;, but otherwise, the poster suggested that format() is the cleaner looking and more flexible choice.<\/p>\n<h1>set search_path = bixie<\/h1>\n<p>I&#8217;m working on a new schema for a <a href=\"http:\/\/xor.lonnen.com\/2013\/04\/11\/error-reporting.html\">project<\/a>. We&#8217;re rolling out a prototype quickly, so we&#8217;re going to house it in our existing production database for now. To keep things easy to clean up, <a href=\"http:\/\/twitter.com\/lxt\">Laura<\/a> suggested that we put things into a <a href=\"http:\/\/www.postgresql.org\/docs\/current\/static\/sql-createschema.html\">separate schema<\/a>. For managing our database models, I&#8217;ve switched to using SQLAlchemy, and also alembic for migrations. This made it super easy to specify that I wanted all the Bixie related tables in their own schema:<\/p>\n<pre><code>class BixieCrash(DeclarativeBase):                                              \r\n    __table_args__ = {'schema': 'bixie'}                                        \r\n    __tablename__ = 'crashes'\r\n<\/code><\/pre>\n<p>And that was it.<\/p>\n<p>Then, to avoid having to add &#8216;bixie.&#8217; to all the table paths in test queries, I put this command into the tests:<\/p>\n<pre><code> cursor.execute(\"\"\" SET search_path TO bixie \"\"\")\r\n<\/code><\/pre>\n<p>I imagine there are some other ways to handle this. We&#8217;re not really using the ORM for anything other than schema loading, so I&#8217;ll probably add that to our connection initialization code for the new app. Then developers can write their queries as without any concerns about being in the correct schema.<\/p>\n<p>And I&#8217;ll glow just a little bit about deploying alembic on stage!<\/p>\n<p><script src=\"https:\/\/gist.github.com\/selenamarie\/f9c7a56544b924a4520f.js\"><\/script><\/p>\n<h1>set colorcolumn=80<\/h1>\n<p>I&#8217;ve been trying to write prettier Python. Today&#8217;s micro-effort was figuring out how display a vertical line to tell me when I exceed the 80 character width.  The proper command to add to .vimrc is:<\/p>\n<pre><code>:set colorcolumn=80\r\n<\/code><\/pre>\n<p>Which looks something like:<\/p>\n<p><a href=\"http:\/\/www.chesnok.com\/daily\/wp-content\/uploads\/2013\/05\/Screenshot-from-2013-05-10-152620.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.chesnok.com\/daily\/wp-content\/uploads\/2013\/05\/Screenshot-from-2013-05-10-152620-300x168.png\" alt=\"colorcolumn in action\" width=\"300\" height=\"168\" class=\"aligncenter size-medium wp-image-4796\" srcset=\"https:\/\/www.chesnok.com\/daily\/wp-content\/uploads\/2013\/05\/Screenshot-from-2013-05-10-152620-300x168.png 300w, https:\/\/www.chesnok.com\/daily\/wp-content\/uploads\/2013\/05\/Screenshot-from-2013-05-10-152620-1024x575.png 1024w, https:\/\/www.chesnok.com\/daily\/wp-content\/uploads\/2013\/05\/Screenshot-from-2013-05-10-152620-520x292.png 520w, https:\/\/www.chesnok.com\/daily\/wp-content\/uploads\/2013\/05\/Screenshot-from-2013-05-10-152620.png 1366w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The last six months have involved a lot more writing of code than the previous couple of years. I&#8217;ve been tweeting little things I learn on a daily basis and thought I&#8217;d look back on this week. format() A reocurring &hellip; <a href=\"https:\/\/www.chesnok.com\/daily\/2013\/05\/10\/til-formatting-search_path-and-colorcolumn\/\">Continue reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[407],"tags":[],"class_list":["post-4793","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts\/4793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/comments?post=4793"}],"version-history":[{"count":6,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts\/4793\/revisions"}],"predecessor-version":[{"id":4802,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts\/4793\/revisions\/4802"}],"wp:attachment":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/media?parent=4793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/categories?post=4793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/tags?post=4793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}