Humrich2020-03-03T04:56:48+00:00http://blog.humrich.usNick HumrichPython Packing Power2016-03-19T00:00:00+00:00http://blog.humrich.us/2016/python-packing-power<p>Not unique to python but still cool is the concept of a tuple. A tuple is basically a list that cant be modified. Tuples are also a tad bit easier to use ah hoc. Lets say I wanted some type of list of objects usually because I want a pair of things. For example, I want to store an item price, and a name together.
Your first option is you could create an object, set the properties on the object, then store the object. But often objects are just too heavy for simple things.
The next option is use a list. Typically with lists, you have to create a list first, then add the objects to it.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">item</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
<span class="n">item</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span>
<span class="n">item</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">'sunglasses'</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">item</span><span class="p">)</span> <span class="c1"># [50, 'sunglasses']
</span><span class="k">print</span><span class="p">(</span><span class="n">item</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="c1"># 50
</span></code></pre></div></div>
<p>Thats a lot of work for what supposed to be an easy solution. Python also has a tuple type. A tuple is also an iterable, so it can be looped over like a list.
Tuples can be specified in line.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">item</span> <span class="o">=</span> <span class="p">(</span><span class="mi">50</span><span class="p">,</span> <span class="s">'sunglasses'</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">item</span><span class="p">)</span> <span class="c1"># (50, 'sunglasses')
</span><span class="k">print</span><span class="p">(</span><span class="n">item</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="c1"># 50
</span></code></pre></div></div>
<p>Really the only difference between a tuple and a list is the <code class="language-plaintext highlighter-rouge">()</code> instead of the <code class="language-plaintext highlighter-rouge">[]</code>, well, and the fact that tuples cant be modified once created. The power of tuples is that you can do them in one line, with any object.
Where tuples get really cool is with automatic packing and unpacking.</p>
<h2 id="pack-your-bags">Pack your bags!</h2>
<p>Python has this cool little thing called “packing”. This is where python automatically packs multiple objects into a Tuple.
Python can pack, but it can also unpack, and it does this all automatically. Packing is usually used when you want to return multiple things.
Since its not really possible for languages to return multiple things (due to how low level programming and call stacks work), python achieves this by “packing” the objects into a tuple.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">get_sunglasses</span><span class="p">():</span>
<span class="k">return</span> <span class="mi">50</span><span class="p">,</span> <span class="s">'sunglasses'</span>
</code></pre></div></div>
<p>Using this syntax, we are essentually returning multiple things. What python is actually doing is packing these two things into a single tuple.
Now the caller of the function can either use a single variable for the return value, in which case they will get a tuple, or they can
use two variables, in which case python will automatically unpack them and assign the tuple values to the variables.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Using a single variable
</span><span class="n">item</span> <span class="o">=</span> <span class="n">get_sunglasses</span><span class="p">()</span>
<span class="k">print</span><span class="p">(</span><span class="n">item</span><span class="p">)</span> <span class="c1"># (50, 'sunglasses')
</span>
<span class="c1"># Using multiple variables, thus causing python to unpack
</span><span class="n">value</span><span class="p">,</span> <span class="n">item_name</span> <span class="o">=</span> <span class="n">get_sunglasses</span><span class="p">()</span>
<span class="k">print</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="c1"># 50
</span><span class="k">print</span><span class="p">(</span><span class="n">item_name</span><span class="p">)</span> <span class="c1"># 'sunglasses'
</span></code></pre></div></div>
<p>Unpacking also works on any type of list/iterable. The only rule is that your number of variables has to either be 1, or match the number of items in the list exactly or you will get a “too many values to unpack” error message.</p>
<h2 id="signatures">Signatures</h2>
<p>Another spot where packing/unpacking can be useful is for function signatures. For example, imagine that an add function takes two arguments, and you happen to have a tuple of arguments. Instead of unpacking the tuples yourself for the function, you can use the unpacking operator <code class="language-plaintext highlighter-rouge">*</code>.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">my_tuple</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span>
<span class="c1"># Without unpacking
</span><span class="n">add</span><span class="p">(</span><span class="n">mytuple</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">mytuple</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="c1"># With unpacking operator
</span><span class="n">add</span><span class="p">(</span><span class="o">*</span><span class="n">mytuple</span><span class="p">)</span>
</code></pre></div></div>
<p>You can also do things the other way around. Lets say you want to change add so that it adds up all the arguments, and takes an infinite amount of arguments. When the <code class="language-plaintext highlighter-rouge">*</code> is used in a function definition, it means to take in a number of arguments, and convert it to a list. For example, our add function would work like this:</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">):</span>
<span class="n">total</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">args</span><span class="p">:</span>
<span class="n">total</span> <span class="o">+=</span> <span class="n">i</span>
<span class="c1"># calling function normally
</span><span class="n">add</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">25</span><span class="p">)</span>
<span class="c1"># Calling it by unpacking
</span><span class="n">numbers</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">25</span><span class="p">)</span>
<span class="n">add</span><span class="p">(</span><span class="o">*</span><span class="n">numbers</span><span class="p">)</span>
</code></pre></div></div>
<p>This is how the print statement works in python3. You can pass in any number of arguments and it will space seperate all arguments for you.</p>
<h2 id="dont-leave-out-the-dictionaries">Dont leave out the dictionaries</h2>
<p>You can also pack and unpack dictionaries by using two stars <code class="language-plaintext highlighter-rouge">**</code>. This can be useful when you want to programatically set the keyword args in a function call. For example:</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># The bad way
</span><span class="k">if</span> <span class="n">color</span><span class="p">:</span>
<span class="n">paint</span><span class="p">(</span><span class="s">'hello'</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">color</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">paint</span><span class="p">(</span><span class="s">'hello'</span><span class="p">)</span>
<span class="c1"># The better way
</span><span class="n">kwargs</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">if</span> <span class="n">color</span><span class="p">:</span>
<span class="n">kwargs</span><span class="p">[</span><span class="s">'color'</span><span class="p">]</span> <span class="o">=</span> <span class="n">color</span>
<span class="n">paint</span><span class="p">(</span><span class="s">'hello'</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</code></pre></div></div>
<p>Another great usecase for this is the <code class="language-plaintext highlighter-rouge">format</code> function for strings. There is a method in python called <code class="language-plaintext highlighter-rouge">locals()</code> that gives you a dictionary of all local variables. When using the <code class="language-plaintext highlighter-rouge">format</code> function on strings, you can just pass in this dictionary for the keyword arguments.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">name</span> <span class="o">=</span> <span class="s">'bob'</span>
<span class="n">greeting</span> <span class="o">=</span> <span class="s">'hello'</span>
<span class="k">print</span><span class="p">(</span><span class="s">'{greeting} {name}'</span><span class="o">.</span><span class="nb">format</span><span class="p">(</span><span class="o">**</span><span class="nb">locals</span><span class="p">()))</span>
</code></pre></div></div>
<p>And just as you can with lists, you can use packing for function definitions as well. For example, if we were going to write our own version of <code class="language-plaintext highlighter-rouge">format</code>, the keyword arguments would need to be dynamic, that is, we dont know what keys are needed, because they can be anything.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">format</span><span class="p">(</span><span class="n">string</span><span class="p">,</span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="n">string</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">'{'</span> <span class="o">+</span> <span class="n">key</span> <span class="o">+</span> <span class="s">'}'</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
<span class="nb">format</span><span class="p">(</span><span class="s">'hello {name} have a good {day}'</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">'bob'</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="s">'tuesday'</span><span class="p">)</span>
</code></pre></div></div>
Args and Kwargs2016-01-22T00:00:00+00:00http://blog.humrich.us/2016/python-kwargs<p>One of the biggest constraints on software engineering is a function signature. A signature is the name and arguments required of a function in order to call it. If a function is highly used in large code base, changing the signature can be a pain. Python can solve a lot of this pain with keyword arguments. Keyword arguments are basically optional arguments, that you specify based on keyname.</p>
<h2 id="refactor-magic">Refactor magic</h2>
<p>Ok lets take a simple example of changing a signature. For this example we will have the following simple function.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">greet</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Hello '</span> <span class="o">+</span> <span class="n">name</span><span class="p">)</span>
</code></pre></div></div>
<p>Now, lets assume we want to add the ability to change the greeting. We could refactor all the current code to pass in the string and a null value. Not only is that painful, but it just smells nasty! In the java world, the normal way to refactor this function is to create a second function, and then the original passes things to the new function. For example:</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">greet</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
<span class="k">return</span> <span class="n">greet_custom</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">greet_custom</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">greeting</span><span class="p">):</span>
<span class="k">if</span> <span class="n">greeting</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">greeting</span> <span class="o">=</span> <span class="s">'Hello '</span>
<span class="k">print</span><span class="p">(</span><span class="n">greeting</span> <span class="o">+</span> <span class="n">name</span><span class="p">)</span>
</code></pre></div></div>
<p>This is better than changing the whole code base, but still pretty nasty; it’s checking for None (null), <em>Yuck!</em> If we keep changing things, we could have lots of signatures for one real function. I have seen code bases in where this goes like 10 functions deep.
With the introduction of keyword arguments we can rewrite this to be much simpler. A keyword arg (commonly referred to as kwarg) can be written by giving the name of the variable and the default value as a key-value pair like such <code class="language-plaintext highlighter-rouge">argname=default</code>. Lets now refactor our example using kwargs.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">greet</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">greeting</span><span class="o">=</span><span class="s">'Hello'</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="n">greeting</span> <span class="o">+</span> <span class="s">' '</span> <span class="o">+</span> <span class="n">name</span><span class="p">)</span>
</code></pre></div></div>
<p>Woa, check that out. We still have an awesome simple function and we dont even have to check for None. <code class="language-plaintext highlighter-rouge">greeting</code> is optional, so the function is completely backwards compatible.</p>
<h2 id="format-it">Format it</h2>
<p>In out previous example, we are concatenating strings. This is not really good practice in python because the objects might not be strings. How do we make the function work on more than just strings? We can use <code class="language-plaintext highlighter-rouge">format</code>. Python strings have a method called <code class="language-plaintext highlighter-rouge">format</code> that replaces all instances of <code class="language-plaintext highlighter-rouge">{}</code> with the arguments provided.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">print</span><span class="p">(</span><span class="s">'This {} a format {} which prints a number: {}'</span><span class="o">.</span><span class="nb">format</span><span class="p">(</span><span class="s">'is'</span><span class="p">,</span> <span class="s">'example'</span><span class="p">,</span> <span class="mi">5</span><span class="p">))</span>
</code></pre></div></div>
<p>notice we passed in multiple arguments, one for each <code class="language-plaintext highlighter-rouge">{}</code>. Now lets fix our function to use <code class="language-plaintext highlighter-rouge">format</code>.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">greet</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">greeting</span><span class="o">=</span><span class="s">'Hello'</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s">'{} {}'</span><span class="o">.</span><span class="nb">format</span><span class="p">(</span><span class="n">greeting</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
</code></pre></div></div>
<p>Looks pretty nice, and we got rid of the stupid <code class="language-plaintext highlighter-rouge">+ ' ' +</code> that always feels so dirty. You probably saw this coming but format can also take kwargs. You can put a identifier name in format by placing it between the brackets. Lets rewrite our example to use keyword args.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">greet</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">greeting</span><span class="o">=</span><span class="s">'Hello'</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s">'{hello} {world}'</span><span class="o">.</span><span class="nb">format</span><span class="p">(</span><span class="n">hello</span><span class="o">=</span><span class="n">greeting</span><span class="p">,</span> <span class="n">world</span><span class="o">=</span><span class="n">name</span><span class="p">)</span>
</code></pre></div></div>
<p>Ok this is a somewhat convoluted example for showing another example, but hey, now you have learned <code class="language-plaintext highlighter-rouge">format</code> and we are ready for some more cool examples.</p>
<h2 id="out-of-order">Out of order</h2>
<p>Since keyword arguments are specified by name, there is no requirement that they be called in order. If we had a function that took three keyword arguments <code class="language-plaintext highlighter-rouge">a</code>, <code class="language-plaintext highlighter-rouge">b</code>, and <code class="language-plaintext highlighter-rouge">c</code> in that order, we could call them in any order we wanted, such as <code class="language-plaintext highlighter-rouge">my_function(c='c', a='a', b='b')</code> and everything would work great.</p>
<p>Another nice feature is that you can use the keyword syntax even for required arguments. So we can call our function with keyword syntax. And since we can, lets do it out of order.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">greet</span><span class="p">(</span><span class="n">geeting</span><span class="o">=</span><span class="s">'Howdy'</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">'Billy'</span><span class="p">)</span>
</code></pre></div></div>
<p>The main reason to do things out of order is mostly just so you dont have to worry or think about what order things go in. It can also improve readability to always use keyword syntax on functions that have multiple arguments.</p>
<h2 id="set-it-free">Set it free</h2>
<p>Now we have one required argument, and one optional argument. We could also make both arguments optional. Keyword arguments have the ability to be called with either
a keyword or not, just like non-optional can be called either way. For example, you could use our greeting string with format without the key-value pair syntax as long as order is maintained.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">print</span><span class="p">(</span><span class="s">'{hello} {world}'</span><span class="o">.</span><span class="nb">format</span><span class="p">(</span><span class="n">greeting</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
</code></pre></div></div>
<p>You probably shouldn’t do this, but you can if you wanted too. What this also means is that we can turn a required argument into an optional one. So now the <code class="language-plaintext highlighter-rouge">name</code> argument can be optional.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">greet</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">'World'</span><span class="p">,</span> <span class="n">greeting</span><span class="o">=</span><span class="s">'Hello'</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s">'{hello} {world}'</span><span class="o">.</span><span class="nb">format</span><span class="p">(</span><span class="n">hello</span><span class="o">=</span><span class="n">greeting</span><span class="p">,</span> <span class="n">world</span><span class="o">=</span><span class="n">name</span><span class="p">)</span>
</code></pre></div></div>
<p>And now we have a beautiful refactored function that doesnt require any arguments. Its just one function, and yet, we can use it in so many ways.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">greet</span><span class="p">(</span><span class="s">'Joe'</span><span class="p">)</span>
<span class="c1"># Hello Joe
</span>
<span class="n">greet</span><span class="p">()</span>
<span class="c1"># Hello World
</span>
<span class="n">greet</span><span class="p">(</span><span class="n">greeting</span><span class="o">=</span><span class="s">'Howdy'</span><span class="p">)</span>
<span class="c1"># Howdy World
</span>
<span class="n">greet</span><span class="p">(</span><span class="s">'Joe'</span><span class="p">,</span> <span class="s">'Howdy'</span><span class="p">)</span>
<span class="c1"># Howdy Joe
</span>
<span class="n">greet</span><span class="p">(</span><span class="n">greeting</span><span class="o">=</span><span class="s">'Howdy'</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">'Joe'</span><span class="p">)</span>
<span class="c1"># Howdy Joe
</span></code></pre></div></div>
Moving to Python2015-05-23T00:00:00+00:00http://blog.humrich.us/2015/moving-to-python<!-- # Moving to Python -->
<p>Currently I have been doing most of my development work in python.
Prior to Python, my main language was Java.
This post is mostly notes of what I learned (not) to do in Python while switching from Java.
This is a list of things I would like my Java minded co-workers to know if they ever worked on the same python packages as myself.
This is in no way a comprehensive guide as I will mostly focus on styling.
I will be making comparisons to Java, yet knowledge of it is not required.</p>
<h2 id="styling-matters">Styling matters</h2>
<p>Anyone who writes collaborative code in Java knows to never to mention where the curly braces go.
Doing so will only aggravate everyone else; No one can decide where things really should go or how things
should be formatted. Most companies have actually made their own style requirements in order to prevent
any arguments from breaking out in the work place.</p>
<p>Python however, does have a strict style guide. There is a common styling that every seasoned python developer follows.
There is even official documentation on python styling: <a href="https://www.python.org/dev/peps/pep-0008/">PEP8</a>.
This post will contain some things which are covered in PEP8. For a further understanding, you could always <a href="https://www.python.org/dev/peps/pep-0008/">go read it</a>.</p>
<h2 id="ditch-the-camel-embrace-the-snake">Ditch the Camel; Embrace the Snake</h2>
<p>The most drastic change from Java for me was the lack of semi-colons and curly braces everywhere.
The next was the naming style. Now, I cringe when names in python code are <em>java-ish</em>.
Here is a list of python naming conventions:</p>
<table>
<thead>
<tr>
<th>Type</th>
<th>Style</th>
</tr>
</thead>
<tbody>
<tr>
<td>Classes</td>
<td>UpperCamelCase</td>
</tr>
<tr>
<td>File Names/modules</td>
<td>lowercase</td>
</tr>
<tr>
<td>functions</td>
<td>snake_case</td>
</tr>
<tr>
<td>variables</td>
<td>snake_case</td>
</tr>
<tr>
<td>Constants</td>
<td>ALL_CAPITALS</td>
</tr>
</tbody>
</table>
<p><strong>Java Example</strong></p>
<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">HelloWorld</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="nc">String</span> <span class="no">HELLO_GREETING</span> <span class="o">=</span> <span class="s">"Hello"</span><span class="o">;</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">sayHello</span><span class="o">(</span><span class="nc">String</span> <span class="n">userName</span><span class="o">)</span> <span class="o">{</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="no">HELLO_GREETING</span> <span class="o">+</span> <span class="s">" "</span> <span class="o">+</span> <span class="n">userName</span><span class="o">);</span></code></pre></figure>
<p><strong>Python Example</strong></p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="k">class</span> <span class="nc">HelloWorld</span><span class="p">():</span>
<span class="n">HELLO_GREETING</span> <span class="o">=</span> <span class="s">'Hello'</span>
<span class="k">def</span> <span class="nf">say_hello</span><span class="p">(</span><span class="n">user_name</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="n">HELLO_GREETING</span><span class="p">,</span> <span class="n">user_name</span><span class="p">)</span>
</code></pre></figure>
<h2 id="file-structure">File Structure</h2>
<p>In python you can practically use any file structure you want, but here is the most common file structure.</p>
<p><strong>Java Example</strong></p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>project/
src/
main/
java/
com/
mycompany/
package/
helloworld
test/
com/
..
resources/
static/
build.script
</code></pre></div></div>
<p><strong>Python Example</strong></p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>project/
bin/
static/
projectname/
package/
helloworld/
test/
setup.py
</code></pre></div></div>
<h2 id="can-i-get-that">Can I Get That?</h2>
<p>If you are familiar with Java at all, you are well aware of using getters and setters.
Using getters and setters is a very good practice for Java and most OOP languages.
The reason why getters and setters are important in Java is so you can add/change behaviour without breaking your API.
For this reason, in Java you are told to keep your properties private;
if others access your properties directly, you are not able to change how the property is used or set.
A common example is adding validation.</p>
<p>For example, imagine I had the the following class in Java:</p>
<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kd">class</span> <span class="nc">Person</span> <span class="o">{</span>
<span class="kd">private</span> <span class="nc">String</span> <span class="n">id</span><span class="o">;</span>
<span class="kd">public</span> <span class="nc">String</span> <span class="nf">getId</span><span class="o">()</span> <span class="o">{</span> <span class="k">return</span> <span class="k">this</span><span class="o">.</span><span class="na">id</span><span class="o">;</span> <span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setId</span><span class="o">(</span><span class="nc">String</span> <span class="n">id</span><span class="o">)</span> <span class="o">{</span> <span class="k">this</span><span class="o">.</span><span class="na">id</span> <span class="o">=</span> <span class="n">id</span><span class="o">;</span> <span class="o">}</span>
<span class="o">}</span>
<span class="c1">// Calling the setter from some other section of code</span>
<span class="nc">Person</span> <span class="n">p</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Person</span><span class="o">();</span>
<span class="n">p</span><span class="o">.</span><span class="na">setId</span><span class="o">(</span><span class="s">"1234"</span><span class="o">);</span></code></pre></figure>
<p>Now, if we wanted to add validation, we just add it to the setter and everything is good.</p>
<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kd">class</span> <span class="nc">Person</span> <span class="o">{</span>
<span class="kd">private</span> <span class="nc">String</span> <span class="n">id</span><span class="o">;</span>
<span class="kd">public</span> <span class="nc">String</span> <span class="nf">getId</span><span class="o">()</span> <span class="o">{</span> <span class="k">return</span> <span class="k">this</span><span class="o">.</span><span class="na">id</span><span class="o">;</span> <span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setId</span><span class="o">(</span><span class="nc">String</span> <span class="n">id</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(!</span><span class="n">id</span><span class="o">.</span><span class="na">matches</span><span class="o">(</span><span class="no">SOME_REGEX</span><span class="o">))</span> <span class="o">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nf">InvalidIDException</span><span class="o">();</span>
<span class="o">}</span>
<span class="k">this</span><span class="o">.</span><span class="na">id</span> <span class="o">=</span> <span class="n">id</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="c1">// Calling the setter from some other section of code</span>
<span class="nc">Person</span> <span class="n">p</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Person</span><span class="o">();</span>
<span class="n">p</span><span class="o">.</span><span class="na">setId</span><span class="o">(</span><span class="s">"1234"</span><span class="o">);</span></code></pre></figure>
<p>If python however you do not need <code class="language-plaintext highlighter-rouge">getId</code> and <code class="language-plaintext highlighter-rouge">setId</code> methods. In fact, please dont ever use them.
You can make all of your attributes public, because you to change how those attributes are set latter if you decide to do so.</p>
<p>The same person class in Python:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="k">class</span> <span class="nc">Person</span><span class="p">():</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">():</span>
<span class="bp">self</span><span class="o">.</span><span class="nb">id</span> <span class="o">=</span> <span class="bp">None</span>
<span class="c1"># Calling the setter from some other section of code
</span><span class="n">p</span> <span class="o">=</span> <span class="n">Person</span><span class="p">()</span>
<span class="n">p</span><span class="o">.</span><span class="nb">id</span> <span class="o">=</span> <span class="s">'1234'</span></code></pre></figure>
<p>If you want to add validation like in the previous example you can add a setter and getter method.</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="k">class</span> <span class="nc">Person</span><span class="p">():</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="nb">id</span> <span class="o">=</span> <span class="bp">None</span>
<span class="o">@</span><span class="nb">property</span>
<span class="k">def</span> <span class="nf">id</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="c1"># This is the getter
</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__id</span>
<span class="o">@</span><span class="nb">id</span><span class="o">.</span><span class="n">setter</span>
<span class="k">def</span> <span class="nf">id</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">id</span><span class="p">):</span> <span class="c1"># This is the setter
</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">SOME_REGEX</span><span class="p">,</span> <span class="nb">id</span><span class="p">):</span>
<span class="k">raise</span> <span class="n">InvalidIDException</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__id</span> <span class="o">=</span> <span class="nb">id</span>
<span class="c1"># Calling the setter from some other section of code
</span><span class="n">p</span> <span class="o">=</span> <span class="n">Person</span><span class="p">()</span>
<span class="n">p</span><span class="o">.</span><span class="nb">id</span> <span class="o">=</span> <span class="s">'1234'</span></code></pre></figure>
<p>Since we can add getters and setters after the fact, there is really no reason to do so initially.
Also, by using python’s <code class="language-plaintext highlighter-rouge">@property</code> decorator, we can still have a clean API without needing to call <code class="language-plaintext highlighter-rouge">p.set_id</code>.</p>
<h2 id="hey-dont-touch-that-its-private">Hey, don’t touch that, it’s private!</h2>
<p>You might be wondering what is up with the <code class="language-plaintext highlighter-rouge">__id</code> that we used in the previous section.
Using two underscores is how to make a method/property private in python.
That being said, Python’s public/protected/private is more of a guideline.
Even if a method is marked protected or private, anyone can still access it (This is actually possible in Java as well).
Python drops all false security pretences and just says, “Don’t touch”.</p>
<table>
<thead>
<tr>
<th style="text-align: center">Access level</th>
<th style="text-align: center">Name</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center">Public</td>
<td style="text-align: center"><code class="language-plaintext highlighter-rouge">name</code></td>
</tr>
<tr>
<td style="text-align: center">Protected</td>
<td style="text-align: center"><code class="language-plaintext highlighter-rouge">_name</code></td>
</tr>
<tr>
<td style="text-align: center">Private</td>
<td style="text-align: center"><code class="language-plaintext highlighter-rouge">__name</code></td>
</tr>
<tr>
<td style="text-align: center">Python Internal</td>
<td style="text-align: center"><code class="language-plaintext highlighter-rouge">__name__</code></td>
</tr>
</tbody>
</table>
<p>Private methods are actually slightly more complicated to access outside the class then protected ones are.
I wont touch on why for now, but because of this, it is typical to use private sparingly.
Typically, most of your class specific methods should just be marked as protected.</p>
Java is Broken2015-03-06T00:00:00+00:00http://blog.humrich.us/2015/java-is-broken<h1 id="java-is-broken">Java is broken</h1>
<p>I was doing some browsing on StackOverflow today when I ran across <a href="http://stackoverflow.com/questions/28908849/unicode-escaped-comments-in-python">this question.</a> In case you cant see it, I will include the relavent pieces.</p>
<h2 id="the-code">The Code</h2>
<p>Run the following code. (It wont do anything dangerous I promise).</p>
<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="nc">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
<span class="n">print</span><span class="o">(</span><span class="s">"Hello"</span><span class="o">);</span>
<span class="cm">/*
* \u002A\u002F\u0070\u0072\u0069\u006E\u0074\u0028\u0022\u0043\u0072\u0075\u0065\u006C\u0022\u0029\u003B\u002F\u002A
*/</span>
<span class="n">print</span><span class="o">(</span><span class="s">"World"</span><span class="o">);</span>
<span class="o">}</span>
<span class="kd">private</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">print</span><span class="o">(</span><span class="nc">String</span> <span class="n">s</span><span class="o">){</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">print</span><span class="o">(</span><span class="n">s</span> <span class="o">+</span> <span class="s">" "</span><span class="o">);</span>
<span class="o">}</span></code></pre></figure>
<h2 id="the-result">The Result</h2>
<p>In case you are too lazy, or just dont trust me, here is the output of the above code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Hello Cruel World
</code></pre></div></div>
<p>Confused? All you have to know is the random stuff inside the block comment is really just unicode version of:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>*/print("Cruel");/*
</code></pre></div></div>
<h2 id="conclusion">Conclusion</h2>
<p>Java parses unicode inside a comment as literal characters and compiles it all together.
This means you could add some pretty nasty code inside of comments, and no one would ever realize its there.</p>
Come in Echo, do you copy?2014-11-13T00:00:00+00:00http://blog.humrich.us/2014/come-in-echo<p>Recently, Amazon released a product called the <a href="http://amazon.com/echo">Echo</a>. While some might argue the</p>
<p>Amazon Echo is just another Siri/Ok Google, I disagree.</p>
<h2 id="so-what-is-echo">So What is Echo?</h2>
<p>Echo is Artificial Intelligence (AI) that attempts to understand and learn human language. You can ask Echo questions in normal English, and it will attempt to give the answer back in normal English. This is a huge step forward from the typical computer experience. In the past, in order to talk to a computer, you needed to learn computer language. Now, the computer is learning our language.</p>
<p>It is worth mentioning that companies have attempted this before. All major tech companies have their own versions of this human/computer interface. They all attempt to understand English and they all attempt to turn technology into your servant. While this technology sounds amazing, it continues to be under utilized.</p>
<h2 id="the-future-as-we-imagined">The Future as we imagined</h2>
<p>30 years ago, when we imagined the future, we didn’t imagine miniature computers in our pockets. We definitely didn’t imagine speaking to little computers. The future as we foresaw dealt with human/computer interaction. We would communicate with a computer much the same way that we communicate with other humans – through everyday speech.
When thinking about what we imagined, systems such as the Enterprise Computer or HAL come to mind; Big all knowing computers that give you information on demand. George Lucas however was the most accurate with his vision. Instead of imagining a big all knowing computer, he imagined a portable computer companion, C-3PO.
C-3PO was a portable computer that you could speak too. He would listen to your question or command, and respond accordingly. Cue Amazon Echo.</p>
<h2 id="forget-the-phone-embrace-the-cloud">Forget the phone, embrace the cloud</h2>
<p>Just a couple months ago, Amazon released the Fire Phone. The recent release of the Echo shows us that Amazon had the technology to add an AI assistant into their phone much like Siri or Ok Google. Amazon however did not put this technology in the phone. They decided instead to take “computing in the cloud” to the next level – your living room.</p>
<p>While AI voice assistants like the Echo have existed for a while, we do not use them. If you are going to pull out your phone, unlock the screen, and speak to it – you might as well just type in your query. The fact of the matter is, voice assistants are error prone. The added convenience of using voice command does not outweigh the frustrations. The Echo will probably be just as error prone as all the others, but it gains a couple points in added convenience. You no longer have to wipe off your hands after eating Cheetos, pry your phone away from an addicted child, or look under the couch cushions to find your phone. Now, whenever you desire a computer assistant, you can just speak.</p>
<h2 id="where-is-r2">Where is R2?</h2>
<p>Amazon’s Echo is currently not all that powerful, but will gain traction in the future. The Echo is currently the lone C-3PO without R2-D2. We all know how annoying and useless C-3PO appears to be whenever R2-D2 is away. The power of the duo is when they work together. C-3PO knows most languages and factual information. R2-D2 can interact with any computer system. Echo will be the new cloud when it starts working with other devices.</p>
<h4 id="example">Example</h4>
<p>In the first Star Wars movie, Luke and Han Solo are stuck in a trash compactor.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Luke Skywalker: 3PO?
C-3PO: We've had some problems...
Luke Skywalker: [interrupting] Will you shut up and listen to me! Shut down all the garbage smashers on the detention level, will ya? Do you copy? Shut down all the garbage smashers on the detention level! Shut down all the garbage mashers on the detention level!
C-3PO: [to R2-D2] No! Shut them *all* down, hurry!
[R2 shuts down the compactors]
Luke Skywalker: What? HAHA! Hey, you did it 3PO!
</code></pre></div></div>
<p>Note the pattern:</p>
<p>Luke gives instruction to C-3PO, who then in turn gives instruction to R2-D2.
Now, imagine this scenario today. (Note: ‘Alexa’ is the keyword to turn on Echo)</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Me: Alexa, set the oven to 350
Echo: [sends command to oven to preheat to 350]
Oven: [sends confirmation to Echo]
Echo: Oven preheating to 350 as requested.
... wait for oven to preheat ...
Echo: Oven is now ready.
</code></pre></div></div>
<h2 id="tldr">TL;DR</h2>
<p>While Echo may use the same technology as Siri and OK Google, it changes the industry in a whole different way, just because of how the technology is presented to the user. Echo will be the first step to the next paradigm shift.</p>