Programming http://blog.samat.org/taxonomy/term/5/0 en Changes that caught my eye in Python 3.2 http://blog.samat.org/2011/02/22/Changes-that-caught-my-eye-in-Python-32 <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>Python 3.2 was released a few days ago. Reading the <a href="http://docs.python.org/dev/whatsnew/3.2.html">What&#8217;s New document</a>, some of the stuff that caught my&nbsp;eye…</p> <p><a href="http://docs.python.org/dev/library/argparse.html"><code>argparse</code></a> is a new command-line option parsing module that replaces <a href="http://docs.python.org/dev/library/optparse.html"><code>optparse</code></a>. I found optparse, now deprecated, a pain to use—argparse looks a lot better. argparse can be used in older programs today with a third-party module (E.g. <a href="http://packages.debian.org/squeeze/python-argparse">argparse Debian package</a>). I actively avoided adding command-line parsing to my applications because optparse was such a pain—something now much less of a&nbsp;problem.</p> <p>The new <a href="http://docs.python.org/dev/library/concurrent.futures.html"><code>concurrent.futures</code> module</a> seems to be a port of <a href="http://download.oracle.com/javase/6/docs/api/java/util/concurrent/package-summary.html">Java&#8217;s <code>java.util.concurrent</code> package</a>. I haven&#8217;t looked into it yet, but with the <abbr title="Global Interpreter Lock"><span class="caps">GIL</span></abbr>, how many people <i>really</i> care about Python threads? The simple examples given can easily be done with <a href="http://docs.python.org/dev/library/multiprocessing.html">Python&#8217;s <code>multiprocessing</code> module</a>, for both processes <i>and</i> threads, though without the Java paradigm. The threading module also has a new <a href="http://docs.python.org/dev/library/threading.html#threading.Barrier"><code>threading.Barrier</code> class</a>.</p> <p><a href="http://www.python.org/dev/peps/pep-3333"><abbr title="Web Server Gateway Interface"><span class="caps">WSGI</span></abbr> for Python 3</a> appears to be finalized. I&#8217;m looking forward to finally porting my <span class="caps">WSGI</span> web applications to Python 3. The <a href="http://docs.python.org/dev/library/email.html"><code>email</code> module</a> also received the same makeover that <span class="caps">WSGI</span> needed for Python&nbsp;3.</p> <p><a href="http://docs.python.org/dev/library/xml.etree.elementtree.html">ElementTree</a> has been updated to v1.3. Not that I care—lxml&#8217;s namespace handling is much better (which is to say, it&#8217;s just terrible as opposed to near unusable, as is with pure&nbsp;ElementTree).</p> <p>Quite possibly the most badass addition to Python 3.2: the <a href="http://docs.python.org/dev/library/functools.html#functools.lru_cache"><code>functools.lru_cache</code> decorator</a>. I use this pattern <i>all the time</i>, but have never gotten around to making it a generic class or decorator I could reuse across applications. With Python 3.2, I no longer need to. The idea: decorate a slow-performing function (where it makes sense) with functools.lru_cache, and Python will instantly and easily <a href="http://en.wikipedia.org/wiki/Memoize">memoize</a> the return values of that function making subsequent calls to the function&nbsp;faster.</p> <p><a href="http://docs.python.org/dev/library/html.html#html.escape"><code>html.escape()</code></a> is a simple function that escapes <span class="caps">HTML</span> markup for you with the appropriate <span class="caps">HTML</span>/<span class="caps">XML</span> entities (I forget how I used to do this usually; something in one of the <span class="caps">CGI</span> utils, I think? Or maybe stuff in my template&nbsp;engine…).</p> <p>This is just from a quick reading of the release notes—it&#8217;s very likely I missed something. What&#8217;d I miss? And what do you like that&#8217;s new in Python&nbsp;3.2?</p> </div></div></div><div class="field field-name-taxonomy-vocabulary-1 field-type-taxonomy-term-reference field-label-inline clearfix"><div class="field-label">Topic:&nbsp;</div><div class="field-items"><div class="field-item even"><a href="/tag/Programming" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Programming</a></div><div class="field-item odd"><a href="/tag/Python" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Python</a></div></div></div> Wed, 23 Feb 2011 01:30:03 +0000 Samat Jain 171 at http://blog.samat.org Python-like tuple unpacking for PHP http://blog.samat.org/2008/10/29/python-tuple-unpacking-php <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>Python provides a neat way for functions to return multiple arguments via &#8220;tuple unpacking&#8221;. For&nbsp;example:</p> <p><pre><code class="python">def blah: return ('one',&nbsp;'two')</p> <p>rval_1, rval_2 = blah() </code></pre></p> <p>The same can be done in <span class="caps">PHP</span> relatively easily via the <a href="http://php.net/list">list construct</a>:</p> <p><pre><code class="php">function blah() { return array('one', 'two');&nbsp;}</p> <p>list($rval_1, $rval_2) = blah(); </code></pre></p> </div></div></div><div class="field field-name-taxonomy-vocabulary-1 field-type-taxonomy-term-reference field-label-inline clearfix"><div class="field-label">Topic:&nbsp;</div><div class="field-items"><div class="field-item even"><a href="/tag/Programming" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Programming</a></div><div class="field-item odd"><a href="/tag/Python" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Python</a></div><div class="field-item even"><a href="/tag/PHP" typeof="skos:Concept" property="rdfs:label skos:prefLabel">PHP</a></div></div></div> Wed, 29 Oct 2008 21:55:47 +0000 Samat Jain 148 at http://blog.samat.org jQuery: the new defacto Javascript web framework http://blog.samat.org/2008/10/02/jquery-the-new-defacto-javascript-web-framework <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>News from a couple days ago: both <a href="http://jquery.com/blog/2008/09/28/jquery-microsoft-nokia/">Microsoft and Nokia are now including the jQuery Javascript framework as part of their development kits</a>. That is: jQuery will be part of <a href="http://www.asp.net/ajax/">Microsoft&#8217;s <span class="caps">ASP</span>.<span class="caps">NET</span> <span class="caps">AJAX</span> framework</a> and be available for use in applications written for <span class="caps">ASP</span>.<span class="caps">NET</span>; and jQuery will also be distributed on millions of Nokia&nbsp;phones.</p> <p>Defacto standards, I believe, are a good way to inform the development of real standards. Standards developed the other way around, at least in the tech industry, have had a habit of taking a very long time to reach end consumers&#8230; for example, how many decades has it taken for your average web user to gain access to a fully <span class="caps">CSS2</span>-compliant web browser? How many more decades will it take for <span class="caps">OASIS</span>&#8217;s OpenDocument format to supplant Microsoft Word and its *.doc&nbsp;files?</p> <p>Hopefully, this is the beginning of a path that will lead to jQuery&#8217;s inclusion into the Javascript language, as well as initiatives that will improve jQuery&#8217;s&nbsp;performance.</p> <p>I like the fact that Microsoft and Nokia are not trying to reinvent the wheel, and roll their own Javascript frameworks. Sun did this with <a href="http://java.sun.com/javaee/javaserverfaces/">Java Server Faces</a>. A frequent lament with <span class="caps">JSF</span> is that it&#8217;s nearly impossible to customize any of the widgets. There is too much complex, custom Javascript, and the adoption of the frameworks used makes figuring out how to work with them&nbsp;difficult.</p> <p>Also, as <a href="http://tirania.org/blog/archive/2008/Oct-01.html">others have noted</a>, this is the first time Microsoft itself is distributing an open-source project with one of their products. A sign of things to&nbsp;come?</p> </div></div></div><div class="field field-name-taxonomy-vocabulary-1 field-type-taxonomy-term-reference field-label-inline clearfix"><div class="field-label">Topic:&nbsp;</div><div class="field-items"><div class="field-item even"><a href="/tag/Programming" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Programming</a></div><div class="field-item odd"><a href="/tag/Software" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Software</a></div><div class="field-item even"><a href="/tag/Microsoft" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Microsoft</a></div><div class="field-item odd"><a href="/tag/Java" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Java</a></div><div class="field-item even"><a href="/tag/jQuery" typeof="skos:Concept" property="rdfs:label skos:prefLabel">jQuery</a></div><div class="field-item odd"><a href="/tag/Javascript" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Javascript</a></div></div></div> Thu, 02 Oct 2008 16:38:47 +0000 Samat Jain 142 at http://blog.samat.org Python 2.5 alpha released http://blog.samat.org/2006/04/05/python-2-5-alpha-released <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p><a href="http://www.python.org/download/releases/2.5/">Python 2.5&#8217;s first alpha has been released</a>. From a quick look at the <a href="http://docs.python.org/dev/whatsnew/whatsnew25.html">What&#8217;s New in Python 2.5 document</a>, what I thought was interesting&#8230; </div></div></div><div class="field field-name-taxonomy-vocabulary-1 field-type-taxonomy-term-reference field-label-inline clearfix"><div class="field-label">Topic:&nbsp;</div><div class="field-items"><div class="field-item even"><a href="/tag/Programming" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Programming</a></div><div class="field-item odd"><a href="/tag/Python" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Python</a></div></div></div> Thu, 06 Apr 2006 00:00:00 +0000 Samat Jain 84 at http://blog.samat.org Computer Organization and Design For More Practice Problem 2.28 http://blog.samat.org/2006/02/16/computer_organization_and_design_for_more_practice_problem_2_28 <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>I&#8217;m using <a href="http://books.elsevier.com/companions/1558606041/">Computer Organization and Design, 3rd ed</a> (David Patterson and John Hennessy) for an electrical engineering course (that I have to take to graduate) in computer systems architecture. Assigned as homework, from the <i>For More Practice</i> section, is problem 2.28: </div></div></div><div class="field field-name-taxonomy-vocabulary-1 field-type-taxonomy-term-reference field-label-inline clearfix"><div class="field-label">Topic:&nbsp;</div><div class="field-items"><div class="field-item even"><a href="/tag/Programming" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Programming</a></div><div class="field-item odd"><a href="/tag/Academia" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Academia</a></div></div></div> Thu, 16 Feb 2006 08:37:10 +0000 Samat Jain 71 at http://blog.samat.org PHP and passing non-variables by reference http://blog.samat.org/2005/09/14/php_and_passing_non-variables_by_reference <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>In <span class="caps">PHP</span> 5.0.5, they&#8217;ve now made it a <em>requirement</em> that things that are passed by reference must now explicitly be a variable. You would think this kind of behavior is obvious, but apparently it&#8217;s been allowed for all versions of <span class="caps">PHP</span> previous. Appararently without even warnings. You&#8217;ll get an&nbsp;error:</p> <p><code> Fatal error: Only variables can be passed by reference ... </code></p> <p>So you may think, where is this useful? Consider something short and concise&nbsp;like:</p> <p><code type="php"> $only_element_we_care_about = array_pop(explode($seperator, $string)); </code></p> <p>You cannot do this now. The return value of a function is not a &#8220;variable&#8221; and cannot be passed by reference; a temporary variable needs to be used&nbsp;instead:</p> <p><code type="php"> $tempory_variable = explode($seperator, $string); $only_element_we_care_about = array_pop($temporary_variable); </code></p> <p>Yes, there are other ways to do the above, but that isn&#8217;t the point. The fix is not difficult, but it is a total complete pain to go back to legacy code and fix things like&nbsp;this.</p> <p>Remind me to find another web programming&nbsp;language.</p> </div></div></div><div class="field field-name-taxonomy-vocabulary-1 field-type-taxonomy-term-reference field-label-inline clearfix"><div class="field-label">Topic:&nbsp;</div><div class="field-items"><div class="field-item even"><a href="/tag/Programming" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Programming</a></div><div class="field-item odd"><a href="/tag/PHP" typeof="skos:Concept" property="rdfs:label skos:prefLabel">PHP</a></div></div></div> Wed, 14 Sep 2005 06:03:00 +0000 Samat Jain 55 at http://blog.samat.org Looking at Python web development frameworks http://blog.samat.org/2005/07/25/looking_at_python_web_development_frameworks <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>I&#8217;ve a lot of interactive websites I&#8217;ve been wanting to do lately. After learning <a href="http://www.python.org">Python</a> earlier this year, I&#8217;m wanting to write all these projects in Python versus something conventional like <a href="http://www.php.net"><span class="caps">PHP</span></a>. I&#8217;ve come to the conclusion that web development with Python sucks; it&#8217;s no wonder it&#8217;s not being used very much. There are too many frameworks, many doing similar things in different ways, or doing totally dissimilar things in totally different ways. I’ve been spending the last week reviewing several different frameworks and deciding which ones I thought were best. </div></div></div><div class="field field-name-taxonomy-vocabulary-1 field-type-taxonomy-term-reference field-label-inline clearfix"><div class="field-label">Topic:&nbsp;</div><div class="field-items"><div class="field-item even"><a href="/tag/Programming" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Programming</a></div><div class="field-item odd"><a href="/tag/Python" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Python</a></div></div></div> Mon, 25 Jul 2005 20:05:00 +0000 Samat Jain 45 at http://blog.samat.org Today's g++/C++ funny: ofstream's open constructor http://blog.samat.org/2005/07/07/todays_cpp_funny_ofstreams_open_constructor <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>So, I&#8217;m working on a project, and have some code like&nbsp;this:</p> <p><code type="cpp"> string blah="somefile"; ofstream&nbsp;fp;</p> <p>fp.open(blah); </code></p> <p>And it doesn&#8217;t work. Apparently, what you need to do when using ofstream&#8217;s open&nbsp;method:</p> <p><code type="cpp"> fp.open(blah.c_str()); </code></p> <p>Yes, it looks like what it is. Apparently g++&#8217;s iostreams library (gcc 3.2) does not support things that have been part of the C++ standard for years&#8211;like C++ <span class="caps">STL</span> strings&#8230; You have to convert back to a C&nbsp;string.</p> <p>Last I checked this was 2005, not 1999. You&#8217;d have thought someone would have overloaded this by now&#8230; I&#8217;ve only checked this in g++; will check with Intel&#8217;s C++ compiler and Microsoft Visual Studio later, though I&#8217;m not sure whether to expect better. What didn&#8217;t help me to find this out, of course, is g++&#8217;s totally wonderful (sarcasm) compiler error&nbsp;messages&#8230;</p> </div></div></div><div class="field field-name-taxonomy-vocabulary-1 field-type-taxonomy-term-reference field-label-inline clearfix"><div class="field-label">Topic:&nbsp;</div><div class="field-items"><div class="field-item even"><a href="/tag/Programming" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Programming</a></div><div class="field-item odd"><a href="/tag/C" typeof="skos:Concept" property="rdfs:label skos:prefLabel">C</a></div><div class="field-item even"><a href="/tag/Cpp" typeof="skos:Concept" property="rdfs:label skos:prefLabel">C++</a></div></div></div> Thu, 07 Jul 2005 22:43:00 +0000 Samat Jain 41 at http://blog.samat.org Splitting a Sequence into Subsequences with Python http://blog.samat.org/2005/06/05/splitting_a_sequence_into_subsequences_with_python <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>I&#8217;m writing a load balancing algorithm for this cluster I&#8217;m working on, and have a bunch of things in a Python list sequence. So came the issue, how to divide the list into sublists for each node in the cluster to compute? I came up with the&nbsp;algorithm:</p> <p><pre><code type="python">def split_seq_stride(l, n): """Splits a sequence l into a list of subsequences containing at least n elements each, not preserving order. The first few subsequences may contain n+1 elements, containing the last few elements. (Algorithm is good for load balancing)""" r=[] k=len(l)/n [r.append(l[i::k]) for i in range(k)] return r </code></pre></p> <p>which behaves&nbsp;like:</p> <p><pre><code></p> <blockquote> <blockquote> <blockquote> <p>l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print split_seq_stride(l, 3) [[1, 4, 7, 10], [2, 5, 8], [3, 6, 9]] </code></pre></p> </blockquote> </blockquote> </blockquote> <p>Assuming each task is an equal amount of work, this algorithm is a good load balancing algorithm. it prevents any node from having to do any less significant work than the&nbsp;others.</p> <p>Probably useful for someone: what about splitting up a sequence in-order, with the last subsequence containing fewer elements? This code segment does just&nbsp;that:</p> <p><pre><code type="python"> def split_seq(l, n): """Splits a sequence l into a list of subsequences containing at most n elements each, preserving order. The last subsequence may contain less than n elements.""" r=[] [r.append(l[s:s+n]) for s in range(0, len(l), n)] return r </code></pre></p> <p>which behaves&nbsp;like:</p> <p><pre><code></p> <blockquote> <blockquote> <blockquote> <p>l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print split_seq(l, 3) [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]] </code></pre></p> </blockquote> </blockquote> </blockquote> <p>Thanks to yason and others on EFnet&#8217;s #python for&nbsp;pointers.</p> </div></div></div><div class="field field-name-taxonomy-vocabulary-1 field-type-taxonomy-term-reference field-label-inline clearfix"><div class="field-label">Topic:&nbsp;</div><div class="field-items"><div class="field-item even"><a href="/tag/Programming" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Programming</a></div><div class="field-item odd"><a href="/tag/Python" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Python</a></div><div class="field-item even"><a href="/tag/HPC" typeof="skos:Concept" property="rdfs:label skos:prefLabel">High Performance Computing</a></div></div></div> Sun, 05 Jun 2005 22:50:00 +0000 Samat Jain 36 at http://blog.samat.org The semantics of a fork, followed by setsid, and another fork http://blog.samat.org/2005/06/02/the_semantics_of_a_fork_followed_by_setsid_and_another_fork <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>So I came across a piece of code that looked like&nbsp;this:</p> <p><pre><code>if (fork() != 0) exit(0); setsid(); if (fork() != 0) exit(0); </code></pre></p> <p>What each individual system call can easily be looked up and understood (well, maybe not setsid), but what the entire block of code is doing is not clear at&nbsp;all</p> <p>&#8230;and I really can&#8217;t say I understand the mechanics of it all to explain myself. But this <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731">python code segment&#8217;s comments</a> does a good job of what the entire block of code is trying to do—daemonize a process&nbsp;completely.</p> </div></div></div><div class="field field-name-taxonomy-vocabulary-1 field-type-taxonomy-term-reference field-label-inline clearfix"><div class="field-label">Topic:&nbsp;</div><div class="field-items"><div class="field-item even"><a href="/tag/Programming" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Programming</a></div><div class="field-item odd"><a href="/tag/C" typeof="skos:Concept" property="rdfs:label skos:prefLabel">C</a></div></div></div> Thu, 02 Jun 2005 22:34:00 +0000 Samat Jain 30 at http://blog.samat.org