<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xml:base="https://bjorn.now/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Ruby on bjorn.now</title><link>https://bjorn.now/tags/ruby/</link><description>Recent content in Ruby on bjorn.now</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Fri, 18 Jan 2013 16:22:32 +0000</lastBuildDate><atom:link href="https://bjorn.now/tags/ruby/feed.xml" rel="self" type="application/rss+xml"/><item><title>Python and its included batteries</title><link>https://bjorn.now/blog/2013/01/18/python-and-its-included-batteries/?utm_medium=feed</link><pubDate>Fri, 18 Jan 2013 16:22:32 +0000</pubDate><guid>https://bjorn.now/blog/2013/01/18/python-and-its-included-batteries/</guid><description>I’ve for almost a year now been using Python as my day to day language as it’s what is used at my job. I’m starting to come around to really liking most parts of Python now, although there is definitely parts that are way more clunky than I would expect from a language so popular. And especially given how much talk there’s about the included batteries and to a small extent how there seems to bit of dickishness involved with hard core Python people. For an example of the latter just:</description><content:encoded><![CDATA[<p>I&rsquo;ve for almost a year now been using <a href="http://en.wikipedia.org/wiki/Python_(programming_language)">Python</a> as my day to day language as it&rsquo;s
what is used at my job. I&rsquo;m starting to come around to really liking most parts
of Python now, although there is definitely parts that are way more clunky than
I would expect from a language so popular. And especially given how much talk
there&rsquo;s about the <em><a href="http://www.python.org/about/">included batteries</a></em> and to a small extent how there seems
to bit of dickishness involved with hard core Python people. For an example of
the latter just:</p>
<div class=" copy-enabled">
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">$ python  
</span></span><span class="line"><span class="cl">&gt;&gt;&gt; <span class="nb">exit</span>  
</span></span><span class="line"><span class="cl">Use exit<span class="o">()</span> or Ctrl-D <span class="o">(</span>i.e. EOF<span class="o">)</span> to exit</span></span></code></pre></div>
</div>
<p>Clearly the interpreter known what I want to achieve, and at the same time it&rsquo;s
programmed to give of an error message telling me I was doing it wrong. And for
it to work I need to say, &ldquo;pretty please&rdquo;. What annoys me with this is just the
simple fact that someone figured out people were having a problem with this, so
they added in a message instead of just doing what is expected. You just don&rsquo;t
behave in that way in everyday life, you&rsquo;d get slapped.</p>
<h2 id="standard-library">
  Standard library
  <a href="#standard-library" class="heading-anchor" aria-label="Direct link to Standard library"><span aria-hidden="true">#</span></a>
</h2>
<p>Time handling in Python is atrocious, do get it working halfway decently you
need to install at least two external libraries. <a href="http://pytz.sourceforge.net/">pytz</a> and <a href="http://labix.org/python-dateutil">python-dateutil</a>,
and while I can understand why pytz might be a good candidate for being external
(for allowing a higher pace of updates since timezone/<a href="http://en.wikipedia.org/wiki/DST">DST</a> changes happens
every so often in different parts of the world).</p>
<p>Today I just wanted to do a diff between two dates and see the years between
them, so I figured I would use <a href="http://docs.python.org/2/library/datetime.html">timedelta</a> since it sounds like something that
should be part of that. Oh, really, it&rsquo;s not? I can diff weeks but not months or
years? So I ended up using dateutil&rsquo;s <a href="http://labix.org/python-dateutil#head-ba5ffd4df8111d1b83fc194b97ebecf837add454">relativedelta</a> instead.</p>
<p>So far the worst way I&rsquo;ve been bitten was with the built-in e-mail library, this
ended up with me sending a copy of the e-mail addresses of 150 customers by
mistake.</p>
<div class=" copy-enabled">
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">smtplib</span>
</span></span><span class="line"><span class="cl"><span class="kn">from</span> <span class="nn">email.mime.text</span> <span class="kn">import</span> <span class="n">MIMEText</span>
</span></span><span class="line"><span class="cl">  
</span></span><span class="line"><span class="cl"><span class="n">msg</span> <span class="o">=</span> <span class="n">MIMEText</span><span class="p">(</span><span class="s1">&#39;Ho ho ho, merry christmas&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">msg</span><span class="p">[</span><span class="s1">&#39;Subject&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&#34;You&#39;re wished something special!&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">msg</span><span class="p">[</span><span class="s1">&#39;From&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;noreply@otherdomain.tld&#39;</span>
</span></span><span class="line"><span class="cl"><span class="n">msg</span><span class="p">[</span><span class="s1">&#39;To&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;user-1@domain.tld&#39;</span>
</span></span><span class="line"><span class="cl"><span class="n">msg</span><span class="p">[</span><span class="s1">&#39;To&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;user-2@domain.tld&#39;</span>
</span></span><span class="line"><span class="cl">  
</span></span><span class="line"><span class="cl"><span class="nb">print</span> <span class="n">msg</span><span class="o">.</span><span class="n">as_string</span><span class="p">()</span></span></span></code></pre></div>
</div>
<p>Output:</p>
<div class=" copy-enabled">
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-plain" data-lang="plain"><span class="line"><span class="cl">Content-Type: text/plain; charset=&#34;us-ascii&#34;
</span></span><span class="line"><span class="cl">MIME-Version: 1.0
</span></span><span class="line"><span class="cl">Content-Transfer-Encoding: 7bit
</span></span><span class="line"><span class="cl">Subject: You&#39;re wished something special!
</span></span><span class="line"><span class="cl">From: noreply@otherdomain.tld
</span></span><span class="line"><span class="cl">To: user-1@domain.tld
</span></span><span class="line"><span class="cl">To: user-2@domain.tld
</span></span><span class="line"><span class="cl">   
</span></span><span class="line"><span class="cl">Ho ho ho, merry christmas</span></span></code></pre></div>
</div>
<p>Because appending when doing an assignment is just what you&rsquo;d expect. This from
the same people that needs to tell me I can&rsquo;t type <code>exit</code> but I need to type
<code>exit()</code>.</p>
<p>Another thing I wanted to use was a windowing function when iterating over a
list, instead of having it implemented in <a href="http://docs.python.org/2.7/library/itertools.html">itertools</a> the documentation for
itertools explains how I can
<a href="http://docs.python.org/release/2.3.5/lib/itertools-example.html">implement my own</a>.
Why!? If you&rsquo;re taking the time to show of an implementation why should I have
to copy that piece of code into my own codebase? What about <a href="http://www.python.org/dev/peps/pep-0020/">The Zen of Python</a>:</p>
<blockquote>
<p>There should be one&ndash; and preferably only one &ndash;obvious way to do it.</p>
</blockquote>
<h3 id="pep8">
  PEP8
  <a href="#pep8" class="heading-anchor" aria-label="Direct link to PEP8"><span aria-hidden="true">#</span></a>
</h3>
<p>I really like <a href="http://www.python.org/dev/peps/pep-0008/">PEP8</a>, I don&rsquo;t agree with all parts of it but I can change. But
I&rsquo;m really looking forward to when the standard library finally is PEP8
compliant.</p>
<h2 id="documentation">
  Documentation
  <a href="#documentation" class="heading-anchor" aria-label="Direct link to Documentation"><span aria-hidden="true">#</span></a>
</h2>
<p>Which brings me to documentation. For the longest time I couldn&rsquo;t understand why
I had such problems understanding the Python documentation, whether it was the
standard library or any third party library. When I was coding <a href="http://en.wikipedia.org/wiki/Ruby_(programming_language)">Ruby</a> I never
felt this stupid when reading documentation. (But I&rsquo;ve been told I&rsquo;m excused
from feeling stupid when reading the <a href="http://docs.sqlalchemy.org/">SQLAlchemy documentation</a>, apparently it&rsquo;s
not just me there.)</p>
<p>But I think I&rsquo;ve figured it why I find the documentation so hard to understand
now. When I&rsquo;m reading documentation for Ruby I generally have the source code
just one mouse click away. So when I wasn&rsquo;t understanding what the documentation
was saying I just flipped on the source code and I could figure it out.</p>
<figure><img src="/img/2013/Screenshot_2013-01-19_00_30.png"
    alt="Ruby documentation source example">
</figure>

<p>With most Python libraries I&rsquo;ve had to do something similar to this:</p>
<ul>
<li>Dig through to wherever the source is saved</li>
<li>Find the correct version for the documentation I&rsquo;m reading(missed version of source I&rsquo;m reading more than once)</li>
<li>Find exactly where this thing I want to read about is is defined.</li>
<li>Start reading the source</li>
<li>???</li>
<li>Enlightenment.</li>
</ul>
<p>It&rsquo;s a general &ldquo;truth&rdquo; that documentation in general sucks, so it&rsquo;s just better
read the source. But somewhere along the way I stopped doing that, probably
around the same time I started using Rails and was amazed at how good the
documentation was. And without thinking about it I was also already reading the
source code, kind of, by having it readily available with the documentation.</p>
<p>My recommendation is for all of you out there using <a href="http://sphinx-doc.org/">Sphinx</a> for your
documentation needs, please enable
<a href="http://sphinx-doc.org/latest/ext/viewcode.html">sphinx.ext.viewcode</a> to output
the source with your documentation.</p>
<p>The other thing I would like to say is the obvious thing, reading the source is
great. I really started to enjoy using Django when I decided to just use the
documentation to find whatever something was called and then use the source code
to figure out how things work. Source code is almost like a painting, a line of
code can say more than a thousand words. ;)</p>
<h2 id="wrapping-up">
  Wrapping up
  <a href="#wrapping-up" class="heading-anchor" aria-label="Direct link to Wrapping up"><span aria-hidden="true">#</span></a>
</h2>
<p>I&rsquo;m not the first one to feel dismayed about Python, <a href="http://zedshaw.com/">Zed Shaw</a> has a great post
called
<a href="http://zedshaw.com/essays/curing_pythons_neglect.html">Curing Python&rsquo;s Neglect</a>
that is well worth a read. (He mentions Lamson 0.9 in the post and that was
released in 2009. As far as I can tell most of the things in his post is still a
problem.)</p>
<p>As I understand a lot of making Python more streamlined is being put into Python
3, that just hasn&rsquo;t been adopted by the majority of developers yet.</p>
<p>We Ruby people had the same thing with 1.9, and I noticed people really starting
to switch about the same time as <a href="https://rvm.io/">RVM</a> started to be used. All of a sudden there
was a really easy way to test things with any possible combination of Ruby you
wanted.</p>
<p>All around I&rsquo;m really enjoying working with Python, again (Python was the
language I used to replace PHP/Perl way back when, then Python was replaced by
Ruby). I used to really dislike having to import every single line of code I
use, but since I&rsquo;m a sucker for having things overly clear I&rsquo;ve gotten round to
it now. For some reason I never had an issue with the white spaces, I want the
code I read to be consistent. And that&rsquo;s one way of trying to ensure that.</p>
<p>Now that I&rsquo;ve done the virtual variant of screaming out into the night I&rsquo;ll go
and try to be productive instead.</p>
<figure><img src="/img/2013/classy-python.png"
    alt="Classy Python">
</figure>

]]></content:encoded></item></channel></rss>