Python http://blog.samat.org/taxonomy/term/8/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 gpxsplitter: Split GPX files with their waypoints http://blog.samat.org/2011/02/16/gpxsplitter-Split-GPX-files-with-their-waypoints <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://wiki.samat.org/GpxSplitter">gpxsplitter</a> splits multi-track <span class="caps">GPX</span> files, containing waypoints, into individual one-track <a href="http://www.topografix.com/gpx.asp"><span class="caps">GPX</span> files</a> with their respective&nbsp;waypoints.</p> <p><span class="caps">GPX</span> files containing multiple tracks and waypoints jumbled together are produced on export by many <span class="caps">GPS</span> units, particularly <span class="caps">MTK</span> chipset-based devices such as the Qstarz Q1000 and Transystem i-Blue 474. Separating tracks and their associated waypoints was a headache until gpxsplitter came along. It&#8217;s meant to be run first-thing after downloading data from your unit via gpsbabel or mtkbabel. It&#8217;s a quick little script written in Python 2.x, with dependencies on mxDateTime and&nbsp;lxml.</p> <p>You can get it from the <a href="http://gitorious.org/samatjain/gpxsplitter">gpxsplitter repository on gitorious</a>, and the <a href="http://wiki.samat.org/GpxSplitter">GpxSplitter wiki page</a> is the one-stop place that will collect information about&nbsp;it.</p> <p>I thought about turning this into a web service, where users can upload their <span class="caps">GPX</span> files and have them split, but I&#8217;d like to know the demand for such a service before writing it. Ideally, gpxsplitter should be part of gpsbabel or something… but yeah, I&#8217;ll save the <span class="caps">XML</span> parsing in C for a very, very rainy&nbsp;day.</p> <p>There are probably any number of bugs. If you find one, please <a href="http://samat.org/contact.html">let me know</a>—and send a testcase&nbsp;too!</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/Python" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Python</a></div><div class="field-item odd"><a href="/tag/OpenStreetMap" typeof="skos:Concept" property="rdfs:label skos:prefLabel">OpenStreetMap</a></div></div></div> Wed, 16 Feb 2011 06:54:52 +0000 Samat Jain 169 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 GPG public key signing post-party automation with KMail http://blog.samat.org/2007/02/18/gpg-public-key-signing-post-party-automation-with-kmail <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>This past <a href="https://wiki.ubuntu.com/TheUbucon">Ubucon</a>&#8217;s key signing party was my first key signing party. One thing I noticed&#8211;signing keys after a <a href="http://en.wikipedia.org/wiki/Key_signing_party">key signing party</a> is a boring repetitive task. Summarized from the <a href="https://wiki.ubuntu.com/KeySigningParty">Ubuntu wiki entry on typical key signing post-party protocol</a>:</p> <ol> <li>Retrieve all public keys of key signing party participants, using gpg –-recv-key <keyID></li> <li>Compare the hardcopy fingerprint from the keysigning party to the fingerprint of the retrieved public keys, using gpg –-fingerprint <keyID></li> <li>Sign the key, using gpg –-sign <keyID> Send the signed key back, either by <ul> <li>E-mail: export the key, then e-mail it to the key owner, using gpg –-export -a <keyID> | mail -s “Your signed key”&nbsp;user@example.com</li> <li>Key server: send the key to a public keyserver, using gpg –send-keys <keyID></li> </ul></li> </ol> <p>This is incredibly monotonous—and people have to wonder why <a href="http://en.wikipedia.org/wiki/Web_of_trust">Web of Trust-based encryption</a> is not more&nbsp;popular?</p> <p>The <a href="http://debaday.debian.net/2007/02/18/signing-party-complete-toolkit-for-efficient-key-signing/">Debian signing-party package</a> provides the utility caff to automate some of this. It&#8217;s not very friendly to “desktop” users,&nbsp;however:</p> <ul> <li>it&#8217;s a <acronym title="Command Line Interface"><span class="caps">CLI</span></acronym>&nbsp;application</li> <li>it requires a local <acronym title="Mail Transfer Agent"><span class="caps">MTA</span></acronym> (/usr/sbin/sendmail in particular), or an “open” <span class="caps">SMTP</span> server, with no support for authenticated <span class="caps">SMTP</span> or&nbsp;<span class="caps">SMTP</span>/<span class="caps">SSL</span></li> <li>the configuration file syntax is Perl and confusing; there are also few examples on the&nbsp;Internet</li> </ul> <p>You could add authenticated <span class="caps">SMTP</span> or <span class="caps">SMTP</span>/<span class="caps">SSL</span> support to the script, but having to know how to hack Perl definitely disqualifies caffe from being a desktop-friendly&nbsp;application.</p> <p>So, I hacked together <a href="/sites/blog.samat.org/files/key-signing-party-batch-process-via-kmail.py.txt">my own key signing party script</a> in Python that would send signed keys back to people via KMail. To use it, create a text file listing all key IDs you wish to sign, one per line. Pipe the contents of this list into the&nbsp;script:</p> <p><code> cat list-of-ids.txt | key-signing-party-batch-process-via-kmail.py </code></p> <p>The script will download each key, ask you to verify the fingerprint, and then sign it. It then will open a KMail composer window, pre-filled with the key owner&#8217;s e-mail address, a friendly template message (customizable in the script), and attached key. Review each e-mail to make sure it is kosher, and click send. Other than continuing to be a <span class="caps">CLI</span> program, I think this is much friendlier&#8211;the only manual work done is the creation of list of keys to sign, comparing fingerprints (this could be automated, but it seems in the spirit of the Web of Trust-based systems not to), and clicking send in a familiar desktop e-mail&nbsp;client.</p> <p>Now for some&nbsp;notes&#8230;</p> <p>It uses the <a href="http://en.wikipedia.org/wiki/DCOP"><span class="caps">DCOP</span> automation</a> features of <span class="caps">KDE</span>&#8217;s Kmail to send messages. You could similarly use Evolution and D-Bus, but I don&#8217;t use Evolution so I can&#8217;t contribute that bit of functionality. Mozilla&#8217;s Thunderbird unfortunately does not yet support any kind of automation features (as far as I know, anyway), so you&#8217;re completely out of luck if you use&nbsp;it.</p> <p><span class="caps">DCOP</span> with Python is a complete, utter, pain. The easy way to drag-and-drop boiler-plate code with kdcop did not work, as it appears the APIs have changed. A problem with <span class="caps">KDE</span>/Python dcopext&#8217;s module and multiple identically-named-functions sealed the deal for me and I gave up trying to use <span class="caps">DCOP</span> with Python, and instead settled for a hack of using the shell instead. I&#8217;m looking forward the one Linux desktop <acronym title="Inter-Process Communication"><span class="caps">IPC</span></acronym> protocol to rule them all, <a href="http://en.wikipedia.org/wiki/D-Bus">D-Bus</a>, to debut in&nbsp;<span class="caps">KDE4</span>.</p> <p>My script does not provide all the functionality of caffe. It, for example, does not encrypt the messages and their keys back to their owners. There doesn&#8217;t appear to be an easy way to do this with KMail and <span class="caps">DCOP</span>, so it&#8217;s a feature that will have to&nbsp;wait.</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/Linux" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Linux</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/KDE" typeof="skos:Concept" property="rdfs:label skos:prefLabel">KDE</a></div><div class="field-item odd"><a href="/tag/Crypto" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Crypto</a></div></div></div> Mon, 19 Feb 2007 03:24:32 +0000 Samat Jain 137 at http://blog.samat.org T-Mobile WiFi Hotspot login script http://blog.samat.org/2006/07/29/t-mobile-wifi-hotspot-login-script <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://hotspot.t-mobile.com/">T-Mobile&#8217;s WiFi Hotspot service</a>, thankfully, forgoes a proprietary authentication mechanism for a solution that while cross platform (i.e. it works with Linux), can be extremely annoying. On opening a web browser and attempting to go to any website, you&#8217;re required to login on an <span class="caps">SSL</span>-protected website with your account username and password before you can use the connection. If your web browser automatically tries to open many pages on startup, such as when you&#8217;re using the <a href="https://addons.mozilla.org/firefox/436/">Session Saving extension for Firefox</a>, you get T-Mobile&#8217;s Hotspot login page in every tab&#8211;extremely&nbsp;annoying!</p> <p>I&#8217;ve written a small Python script that can login programatically without use of a web&nbsp;browser.</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/Linux" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Linux</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/Wireless" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Wireless</a></div></div></div> Sat, 29 Jul 2006 06:53:05 +0000 Samat Jain 118 at http://blog.samat.org A quick shell include for setting paths for programs installed in non-traditional locations http://blog.samat.org/2006/06/28/a-quick-shell-include-for-setting-paths-for-programs-installed-in-non-traditional-locations <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 <a href="http://www.linuxfromscratch.org/blfs/view/stable/introduction/beyond.html">page in the Beyond Linux from Scratch manual</a> describes environment variables that should be set when installing software in a non-traditional location (e.g. your home directory).</p> <p>I've written a sh/bash include that can be included from .bashrc to set these variables, as well as PYTHON_PATH for separately installed Python libraries:</p> <p><code><br /> #!/bin/bash</code></p> <p>PREFIX=$HOME/usr</p> <p>export PATH="$PREFIX/bin:$PATH"<br /> export PYTHONPATH="$PREFIX/lib/python2.4/site-packages:$PYTHON_PATH"<br /> export MANPATH="$PREFIX/man:$MANPATH"<br /> export INFOPATH="$PREFIX/info:$INFOPATH"<br /> export LD_LIBRARY_PATH="$PREFIX/lib:$LD_LIBRARY_PATH"<br /> export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig:$PKG_CONFIG_PATH"<br /> export CPPFLAGS="-I$PREFIX/includes $CPPFLAGS"<br /> export LDFLAGS="-L$PREFIX/lib $LDFLAGS"<br /></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/Linux" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Linux</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/Bash" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Bash Shell</a></div></div></div> Wed, 28 Jun 2006 15:34:05 +0000 Samat Jain 115 at http://blog.samat.org A take on Drupal's taxonomy system from the Plone/Python camp http://blog.samat.org/2006/06/02/a-take-on-drupals-taxonomy-system-from-the-plone-python-camp <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>Over at the Plone Blog is the article <a href="http://theploneblog.org/blog/archive/2006/05/31/death-and-taxonomies">Death and Taxonomies</a>; it reviews <a href="http://drupal.org/">Drupal&#8217;s</a> taxonomy (aka category)&nbsp;system.</p> <p>The author basically comes to the conclusion I have, a conclusion that is shared by many in the Drupal community but not so much outside of it: <strong>Drupal&#8217;s taxonomy system is <em>amazing</em></strong>.</p> <p>For most people and their uses, it is completely overengineered and complicated to use. This fits into Drupal&#8217;s marketing stance that it is a content management framework rather than just a <acronym title="content management system"><span class="caps">CMS</span></acronym>. It is very generic, and with some custom programming can be adapted to anything&#8211;the possibilities are limitless. With many web development projects (that I don&#8217;t want to write in <span class="caps">PHP</span>) I think about, I wish I had the facility of Drupal&#8217;s taxonomy&nbsp;system.</p> <p>Of course, there are problems, which the review goes into: there are too many hierarchal relationships in Drupal, all competing with each other. There is the menu system, the book module, and hierarchal taxonomies. The key to being a Drupal master is know when to use which and how to use them, something I&#8217;ve definitely not mastered. And that is part of the problem&#8211;why should you need&nbsp;to?</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/Python" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Python</a></div><div class="field-item odd"><a href="/tag/Drupal" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Drupal</a></div></div></div> Fri, 02 Jun 2006 10:23:13 +0000 Samat Jain 106 at http://blog.samat.org Calculating bandwidth from a combined-format web server log http://blog.samat.org/2006/05/25/calculating-bandwidth-from-a-combined-format-web-server-log <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>Given a combined web server access log, such as the ones generated by Apache, it can be useful to know the total amount of data transfer of all requests in that log. This task is simple: extract the field listing the number of bytes sent for a request, and add them all up. For something so simple, there is an odd lack of examples or pre-made scripts that do this. Or, at least, I couldn’t find&nbsp;any.</p> <p>I wrote my solution, calculate-data-transfer.py, in&nbsp;Python:</p> <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/Python" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Python</a></div><div class="field-item odd"><a href="/tag/Apache" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Apache</a></div></div></div> Thu, 25 May 2006 09:03:21 +0000 Samat Jain 101 at http://blog.samat.org A better way to separate Apache log files by virtual host domains http://blog.samat.org/2006/05/23/a-better-way-to-separate-apache-log-files-by-virtual-host-domains <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>Apache&#8217;s &#8220;combined&#8221; log format is one the most common log formats used in <a href="http://httpd.apache.org/docs/2.0/logs.html#accesslog">access logging</a>, containing useful fields such as referrer and user agent. Unfortunately, it does not contain a field listing the the virtual host for whom a request was formed. With Apache, this is easily rectified by defining a custom logging format and post-processing logs to maintain compatibility. </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/Python" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Python</a></div><div class="field-item odd"><a href="/tag/Apache" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Apache</a></div></div></div> Tue, 23 May 2006 08:02:00 +0000 Samat Jain 99 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 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 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