tag:blogger.com,1999:blog-15333560986513717532024-03-27T23:44:19.690-07:00Type and FlowSoftware and Science with Carson MyersAnonymoushttp://www.blogger.com/profile/06696288141767642051noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-1533356098651371753.post-10398523514294755662011-06-05T01:00:00.001-07:002012-04-25T13:30:42.612-07:00Python Decorator with Optional Keyword Arguments<style type="text/css">
td.linenos { background-color: #f0f0f0; padding-right: 10px; }
span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }
pre { line-height: 125%; }
body .hll { background-color: #ffffcc }body .c { color: #008000 } /* Comment */
body .err { border: 1px solid #ff0000 } /* Error */
body .k { color: #0000ff } /* Keyword */
body .cm { color: #008000 } /* Comment.Multiline */
body .cp { color: #0000ff } /* Comment.Preproc */
body .c1 { color: #008000 } /* Comment.Single */
body .cs { color: #008000 } /* Comment.Special */
body .ge { font-style: italic } /* Generic.Emph */
body .gh { font-weight: bold } /* Generic.Heading */
body .gp { font-weight: bold } /* Generic.Prompt */
body .gs { font-weight: bold } /* Generic.Strong */
body .gu { font-weight: bold } /* Generic.Subheading */
body .kc { color: #0000ff } /* Keyword.Constant */
body .kd { color: #0000ff } /* Keyword.Declaration */
body .kn { color: #0000ff } /* Keyword.Namespace */
body .kp { color: #0000ff } /* Keyword.Pseudo */
body .kr { color: #0000ff } /* Keyword.Reserved */
body .kt { color: #2b91af } /* Keyword.Type */
body .s { color: #a31515 } /* Literal.String */
body .nc { color: #2b91af } /* Name.Class */
body .nd { color: #0000ff; font-weight: bold } /* Name.Decorator */
body .ow { color: #0000ff } /* Operator.Word */
body .sb { color: #a31515 } /* Literal.String.Backtick */
body .sc { color: #a31515 } /* Literal.String.Char */
body .sd { color: #a31515 } /* Literal.String.Doc */
body .s2 { color: #a31515 } /* Literal.String.Double */
body .se { color: #a31515 } /* Literal.String.Escape */
body .sh { color: #a31515 } /* Literal.String.Heredoc */
body .si { color: #a31515 } /* Literal.String.Interpol */
body .sx { color: #a31515 } /* Literal.String.Other */
body .sr { color: #a31515 } /* Literal.String.Regex */
body .s1 { color: #a31515 } /* Literal.String.Single */
body .ss { color: #a31515 } /* Literal.String.Symbol */
</style> <br>
Decorators in Python are extremely useful tools. This syntactic sugar is an integral part of standard python code, and can be custom-built to make your own code more succinct and awesome.<br>
<br>
I've used decorators to mark functions as deprecated, make them curryable, make sure a user is authenticated (when that method was a render method in a Twisted resource), and more. Recently I was using them to add a little bit of magic to a collection of methods that I needed to use a lot, that had a lot of repeated code in them. I needed to sometimes set up the executing environment of those functions a little differently than usual though, using keyword options that I would pass to the decorator. I've seen decorators used this way before, so I figured it would be easy. Uhm, not so much.<br>
Decorators are a little confusing to people who haven't written them before. The idea is simple:<br>
<div class="highlight">
<pre><span class="nd">@decorator</span>
<span class="k">def</span> <span class="nf">function</span><span class="p">():</span>
<span class="k"><span style="color: black;"> </span>pass</span>
<span class="k">def</span> <span class="nf">function</span><span class="p">():</span>
<span class="k">pass</span>
<span class="n">function</span> <span class="o">=</span> <span class="n">decorator</span><span class="p">(</span><span class="n">function</span><span class="p">)</span></pre>
</div>
A decorator is basically just a function (it can also be done with a class) which takes a function as its argument, and returns a function. The decorator in the example above might have been:<br>
<div class="highlight">
<pre><span class="k">def</span> <span class="nf">decorator</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">inner</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">print</span> <span class="s">"This is a decorated function!"</span>
<span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="k">return</span> <span class="n">inner</span>
<span class="nd">@decorator</span>
<span class="k">def</span> <span class="nf">function</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k">print</span> <span class="n">x</span>
<span class="n">function</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="c">#> This is a decorated function!</span>
<span class="c">#> 42</span>
</pre>
</div>
The decorator takes a function. It creates a new, "inner" function and closes the passed function. In the inner function, we print something, then call the original function. You can see this in action in this example, where calling the decorated function results in the text "This is a decorated function!" being printed.<br>
A decorator can also take arguments:<br>
<a href="http://typeandflow.blogspot.com/2011/06/python-decorator-with-optional-keyword.html#more">Read more »</a>Anonymoushttp://www.blogger.com/profile/06696288141767642051noreply@blogger.com15tag:blogger.com,1999:blog-1533356098651371753.post-51199354619454791692011-05-13T11:04:00.001-07:002011-05-13T11:04:18.758-07:00Multiple, Recursive file Search/Replace and Corrupting Your Repository<p>Today I was working on a project of mine, and I needed to rename a function. The function name was fine when I wrote it, but I needed to make a function with some very similar functionality. Naturally I broke it down, abstracted the old function and created a few new ones which called it in slightly different ways. Anyways, that's not important. What's important is I needed to rename a function that's used all over the place in a package.</p> <p>Now, I'm developing this aforementioned project in a linux environment, using emacs. While I can edit proficiently with emacs, I'm not an M-x butterfly using guru by any means. So I thought I'd just do a recursive search and replace using find and sed:</p> <p><font face="Consolas">find ./ –type i | xargs sed –i 's/dict_format/dict_to_object/'</font></p> <p>I found it online somewhere. I figured, "what's the harm? My project is under version control at BitBucket, and I'll learn some bash skills." So I ran the command, did an <font face="Consolas">hg diff</font><font face="Arial"> and saw that it worked well. I was pleased. "Time to commit these changes," I thought.</font></p> <p><font face="Consolas">carson@vmdev:~/py/aforementioned_project@ hg ci –m "Renamed that pesky function"<br>abort: index data/.../format.py.i is corrupted!</font></p> <p>Dear lord, no!</p> <p>Of course, I knew what happened. "What's an index file, anyway? I'll just change it back." It turns out an index file is a binary file—since I couldn't update to an earlier revision due to the corrupted index, I cloned a different copy of the repository and took a look. There is no human-readable text in there whatsoever. No word as to how the new name of my function wound up smudged in there.</p> <p>"Oh well," I said, "I have this new index file, let's see what happens if I just copy it into my .hg." Well, that doesn't work.</p> <p>"OH WELL," I said, "I'll just delete everything, and use this new clone." Except that I had been committing to my local repo, and hadn't bothered to push the changes to BitBucket before I did the search/replace. D'oh!</p> <p>I only lost about an hour and a half of work, but it could have been much worse. I've gone several days without pushing before. I realize there are probably several things I could have done to rectify the situation without losing the work. Such as the obvious one, copying my changed files to the new clone and re-committing them. I'm sure there's more solutions that are more technical, involving some hg-fu, but I'm not aware of them at this point.</p> <p>My lesson is this: push often! Learn to use your IDE to do automated refactoring for you, and make sure it ignores your .hg/.git/.svn directory. Things like this happen to everyone, at some point, but it still <em>sucks</em>.</p> <p>The worst part is, there weren't even any changes that needed to be made in the top-level directory of my project. I could have just run the command in the affected package and avoided this whole catastrophe. Oh well. We live, we learn.</p> Anonymoushttp://www.blogger.com/profile/06696288141767642051noreply@blogger.com16tag:blogger.com,1999:blog-1533356098651371753.post-63513963168044971562011-04-13T16:39:00.001-07:002011-04-13T18:14:50.657-07:00Writing a Bare-Minimum PHP Application<p>PHP has a pretty bad rep for being cluttered, inconsistent, and ugly. Why would any language use <font face="Courier New">\</font><font face="Arial"> as a namespace delimiter? Or –</font><font face="Courier New">></font><font face="Arial"> for dereferencing object members? Well, because, </font><font face="Courier New">.</font><font face="Arial"> is reserved strictly for string concatenation… HA! But although PHP has many shortcomings, it remains the most ubiquitous language for web development today. Perhaps one of the most distressing things about PHP is that it makes it <em>very </em>easy to write bad<em> code</em>. The gentle learning curve assures that PHP is heavily used by beginners and is the server-side language of choice for almost every shared web hosting service. It also assures that there is a lot of bad, <em>bad</em> code out there.</font></p> <p>When I first learned PHP, I remember wondering why anyone would want to write a class or a function when you could drop a bunch of PHP inside a bunch of HTML and get instant gratification. I wrote a fan-finder application for a fan site that my friends and I ran at the time this way. Users of our fan site would write a little bio and upload a picture and they would be added to an interactive world map of all users who used the service (a few hundred). There was no database, everything was saved into a pipe-delimited text file, which needed to be read and written by nearly every page in this application. It got completely out of control! The same code for reading/writing the data file existed in at <em>least</em> twelve different places, all written slightly differently. It was riddled with bugs. I barely managed to keep it alive—I actually had to maintain the data file by hand at times. I learned very quickly how important it was to keep my code minimal and clean. Some languages attempt to force you to do this, to a degree—but PHP made it so easy to do everything the wrong way.</p> <p>Nowadays I would just use one of the many excellent PHP frameworks out there, such as <a href="http://codeigniter.com">Code Igniter</a>. But sometimes it's nice to build everything yourself. So I spent some time designing an ultra-light, barebones PHP app which I would consider to be the minimum required amount of structure for a small PHP application, in order for it to be able to survive and evolve. This is intended mainly for PHP beginners--my "framework" is by no means an optimal solution, and many other designs would be sufficient also, but here's what I would do:</p><a href="http://typeandflow.blogspot.com/2011/04/writing-bare-minimum-php-application.html#more">Read more »</a>Anonymoushttp://www.blogger.com/profile/06696288141767642051noreply@blogger.com6tag:blogger.com,1999:blog-1533356098651371753.post-34915272343763714102011-04-06T20:59:00.001-07:002011-04-06T21:01:57.929-07:00Vectors, Projectiles, and More: Doing Physics in .net Part III<p>Having just started getting into C#, I can already say that it’s a pleasure to work with – especially when used with its tightly coupled IDE, Visual Studio. Of course, being a physics/astronomy/computer science geek, I got straight to work doing some projectile physics in C# to see how it turned out. To get myself started on this blog, I’m going to write a couple of posts about doing physics in .net:</p> <ul> <li><a href="http://typeandflow.blogspot.com/2011/02/units-of-measure-doing-physics-in-net.html">Units of Measure: Doing Physics in .net Part I</a> <li><a href="http://typeandflow.blogspot.com/2011/02/physics-library-doing-physics-in-net.html">Physics Library: Doing Physics in .net Part II</a> <li>Vectors, Projectiles, and More: Doing Physics in .net Part III</li> <p>In <a href="http://typeandflow.blogspot.com/2011/02/physics-library-doing-physics-in-net.html">part 2</a> I started out an F# physics library containing the units we want to use, and some unit-safe math functions called Units.fs (note the <a href="http://typeandflow.blogspot.com/2011/02/physics-library-doing-physics-in-net.html">edit added to part 2 regarding the sqrt function</a> – you don't need to write one, F# has its own built-in, unit-safe <font face="Courier New">sqrt</font><font face="Arial">). This will be used by the rest of the library and should remain the first source file in your project (in Visual Studio, the order the files appear in is the order they are compiled in. Now we want to start fleshing out our library and making it into something useful. I'll start by moving the <em>whole</em> projectile class into the F# library.</font></p> <p>But wait. I'm pretty much just writing this whole thing over again in F#, right? The point of this is to interoperate between the two languages, after all. Really, this is the proper thing to do – we're going to write the projectile <em>physics</em> as a class in F#. The class can be <em>extended</em> by C# or other .net languages if you want to add qualitative properties to the projectile (such as, making a red ball that you want to send flying through the air – you just attach the projectile physics to your otherwise normal red ball object).</p> <p>So here's our C# class, translated into F#:</p><pre class="code"><span style="color: green">//Projectiles.fs
</span><span style="color: blue">#light
namespace </span>FsPhysicsDemo
<span style="color: blue">module </span>Projectiles =
[<Literal>]
<span style="color: blue">let </span>g = -9.808<m/s^2>
<span style="color: blue">open </span>Projectiles
<span style="color: blue">open </span>UnitSafeFunctions
<pre class="code"><span style="color: blue">type </span>Projectile =
<span style="color: blue">val mutable </span>xi : float<m>
<span style="color: blue">val mutable </span>yi : float<m>
<span style="color: blue">val mutable </span>vxi : float<m/s>
<span style="color: blue">val mutable </span>vyi : float<m/s>
<span style="color: blue">new</span>() = {
xi = 0.0<m>; yi = 0.0<m>;
vxi = 0.0<m/s>; vyi = 0.0<m/s>;
}
<span style="color: blue">member </span>this.xf (t : float<s>) =
this.xi + this.vxi * t
<span style="color: blue">member </span>this.yf (t : float<s>) =
this.yi + this.vyi * t + g * sqr t
</pre></pre>
<p>Here we've moved our <font face="Courier New">g </font><font face="Arial">constant out of the class, and into a module. As for the </font><font face="Courier New">Projectile</font><font face="Arial"> class, you see that we opened the </font><font face="Courier New">Projectiles</font><font face="Arial"> and </font><font face="Courier New">UnitSafeFunctions</font><font face="Arial"> modules just above them. This is because we use the </font><font face="Courier New">g</font><font face="Arial"> constant, and our unit-safe square function in the last member function in the class. We don't have to open anything in order to use the units however, since those weren't defined in a module, but rather just in the namespace (our </font><font face="Courier New">Projectile</font><font face="Arial"> class is also defined at namespace level – types can be in a namespace).</font></p>
<p>That's not bad, now we have our C# class, but a little more condensed and unit-aware. We're not really harnessing much power from functional programming right now though—but what more can we do? Well, those <font face="Courier New">x <font face="Arial">and</font> y </font><font face="Arial">values all belong together. In C# you'd probably use the </font><font face="Courier New">Point</font><font face="Arial"> class in </font><font face="Courier New">System.Drawing</font><font face="Arial">, or make your own class. In a functional programming language (or one that supports them nicely, such as Python) you could use a Tuple:</font></p></li></li></ul><a href="http://typeandflow.blogspot.com/2011/04/vectors-projectiles-and-more-doing.html#more">Read more »</a>Anonymoushttp://www.blogger.com/profile/06696288141767642051noreply@blogger.com1tag:blogger.com,1999:blog-1533356098651371753.post-42552107901085026212011-02-23T21:31:00.001-08:002011-04-06T22:30:04.481-07:00Physics Library: Doing Physics in .net Part II<p>Having just started getting into C#, I can already say that it’s a pleasure to work with – especially when used with its tightly coupled IDE, Visual Studio. Of course, being a physics/astronomy/computer science geek, I got straight to work doing some projectile physics in C# to see how it turned out. To get myself started on this blog, I’m going to write a couple of posts about doing physics in .net:</p> <ul> <li><a href="http://typeandflow.blogspot.com/2011/02/units-of-measure-doing-physics-in-net.html">Units of Measure: Doing Physics in .net Part I</a> <li>Physics Library: Doing Physics in .net Part II <li><a href="http://typeandflow.blogspot.com/2011/04/vectors-projectiles-and-more-doing.html">Vectors, Projectiles, and More: Doing Physics in .net Part III</a></li> <p>In <a href="http://typeandflow.blogspot.com/2011/02/units-of-measure-doing-physics-in-net.html" target="_blank">part 1</a> I went over the benefits of F#’s units of measure over doing dimensionless math with C# (and most other languages). But if your project isn’t already in F#, it’s a little hard to benefit from it. Luckily, it’s easy to work between languages in .net managed code. You can build the F# library using the <a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=effc5bc4-c3df-4172-ad1c-bc62935861c5&displaylang=en">standalone installer and Visual Studio 2010 Shell</a>. This is the F# code I left off with:</p><pre class="code"><span style="color: blue">#light
namespace </span>FsPhysicsDemo
<span style="color: blue">module </span>Physics =
[<Measure>] <span style="color: blue">type </span>m
[<Measure>] <span style="color: blue">type </span>s
<span style="color: blue">let </span>xf (xi : float<m>) (v : float<m/s>) (a : float<m/s^2>) (t : float<s>) =
xi + v * t + 0.5 * a * t * t
<span style="color: blue">let </span>v (a : float<m/s^2>) (t : float<s>) =
a * t
<span style="color: blue">let </span>deltaV (vi : float<m/s>) (vf : float<m/s>) =
vf - vi
</pre>
<p>This works fine as a small library, but it’s a little annoying to work with. When the math was just done in C#, we had the benefit of it being object oriented—the parameters of the equation were all stored in the object—whereas with this code each parameter needs to be passed to a function. What makes it even worse is that we <em>already have an object oriented projectile in C#,</em> and then we go ahead and throw away the strengths of that setup:</p></li></li></ul><a href="http://typeandflow.blogspot.com/2011/02/physics-library-doing-physics-in-net.html#more">Read more »</a>Anonymoushttp://www.blogger.com/profile/06696288141767642051noreply@blogger.com0tag:blogger.com,1999:blog-1533356098651371753.post-4320029275223418802011-02-20T10:32:00.000-08:002011-12-08T20:45:38.301-08:00Units of Measure: Doing Physics in .net Part I<p>Having just started getting into C#, I can already say that it’s a pleasure to work with – especially when used with its tightly coupled IDE, Visual Studio. Of course, being a physics/astronomy/computer science geek, I got straight to work doing some projectile physics in C# to see how it turned out. To get myself started on this blog, I’m going to write a couple of posts about doing physics in .net.</p> <ul> <li>Units of Measure: Doing Physics in .net Part I <li><a href="http://typeandflow.blogspot.com/2011/02/physics-library-doing-physics-in-net.html">Physics Library: Doing Physics in .net Part II</a> <li><a href="http://typeandflow.blogspot.com/2011/04/vectors-projectiles-and-more-doing.html">Vectors, Projectiles, and More: Doing Physics in .net Part III</a> <h5></h5></li> <p>For part one, I’ll do a brief intro into F#’s units of measure.</p> <p>So, when I got started on this C# projectile stuff, it started looking a little like this:</p><pre class="code"><span style="color: blue">namespace </span>CsPhysicsDemo
{
<span style="color: blue">class </span><span style="color: #2b91af">Projectile
</span>{
<span style="color: blue">public const double </span>g = -9.80;
<span style="color: blue">public double </span>xi;
<span style="color: blue">public double </span>yi;
<span style="color: blue">public double </span>vxi;
<span style="color: blue">public double </span>vyi;
<span style="color: blue">public double </span>xf(<span style="color: blue">double </span>t)
{
<span style="color: blue">return </span>xi + vxi * t;
}
<span style="color: blue">public double </span>yf(<span style="color: blue">double </span>t)
{
<span style="color: blue">return </span>yi + vyi * t + 0.5 * <span style="color: #2b91af">Projectile</span>.g * t;
}
}
}</pre>
<p>Nice. The variables stand for “x initial,” “velocity x initial,” etc. This compiles and works fine – but guess what. There’s a mistake in there. Can you spot it? It’s not easy. the <font face="Courier New">yf()</font><font face="Arial"> function is supposed to return the vertical position of the projectile at a given time – instead it returns its velocity, plus its initial displacement. Which, of course, doesn’t make sense. The problem here isn’t just that we forgot to square </font><font face="Consolas">t</font><font face="Arial">, but that – despite the strongly typed nature of C# – there’s no type checking for what <em>kind</em> of </font><font face="Consolas">double </font><font face="Arial">we’ve created in this math operation.</font></p>
</li></li></ul><a href="http://typeandflow.blogspot.com/2011/02/units-of-measure-doing-physics-in-net.html#more">Read more »</a>Anonymoushttp://www.blogger.com/profile/06696288141767642051noreply@blogger.com6