{"id":7754,"date":"2012-08-01T22:51:33","date_gmt":"2012-08-02T02:51:33","guid":{"rendered":"http:\/\/scruss.com\/blog\/?p=7754"},"modified":"2012-08-01T22:51:33","modified_gmt":"2012-08-02T02:51:33","slug":"learning-to-tolerate-python","status":"publish","type":"post","link":"https:\/\/scruss.com\/blog\/2012\/08\/01\/learning-to-tolerate-python\/","title":{"rendered":"learning to tolerate python"},"content":{"rendered":"<p><a href=\"http:\/\/python.org\/\">Python<\/a> is okay, I guess, but there&#8217;s not a hint of music to it. I&#8217;m a dyed-in-the-wool Perl programmer since 4.036 days. When I think of how I&#8217;ll solve a programming problem, I think in Perl (or, more rarely, in PostScript, but I really have to be pretty off-balance to be thinking in stacks). I&#8217;m learning Python because all of the seemingly nifty <a href=\"http:\/\/www.osgeo.org\/\">open source geospatial software<\/a> uses it, and if I&#8217;m to write anything for or about the <a href=\"http:\/\/www.raspberrypi.org\/\">Raspberry Pi<\/a>, it seems that Python is the language they officially support on it.<\/p>\n<p>So I&#8217;m learning Python by porting some of the simple Perl tools I use around here. It&#8217;s painful, not just dealing with the language Fortranesque space-significance, but also physically; I think I put my shoulder out picking up <a href=\"http:\/\/www.oreillynet.com\/pub\/au\/446#ReviewHeader\">Mark Lutz<\/a>&#8216;s giant books on Python. The first program I chose to port matches input lines against known words in the system dictionary file. Here&#8217;s the Perl version:<\/p>\n<pre class=\"brush: perl; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/perl -w\r\n\r\nuse strict;\r\nuse constant WORDLIST =&gt; '\/usr\/share\/dict\/words';\r\n\r\nmy %words;\r\nopen(WORDS, WORDLIST);\r\nwhile () {\r\n    chomp;\r\n    my $word  = lc($_);\r\n    $words{$word}++;\r\n}\r\nclose(WORDS);\r\n\r\n# now read candidate words from stdin\r\nwhile (&lt;&gt;) {\r\n  chomp;\r\n  $_=lc($_);\r\n  print $_,&quot;\\n&quot; if defined($words{$_});\r\n}\r\n\r\nexit;\r\n<\/pre>\n<p>I most recently used this to look for available call signs that \u00e2\u20ac\u201d minus the number \u00e2\u20ac\u201d were real words. The input lines from the <a href=\"http:\/\/apc-cap.ic.gc.ca\/pls\/apc_anon\/query_avail_cs$.startup\">available call sign list<\/a> look like this:<\/p>\n<pre>VA3PHZ\r\nVA3PIA\r\nVA3PID\r\nVA3PIF\r\nVA3PIH\r\n...<\/pre>\n<p>so if I strip out the 3s and run it through the program:<\/p>\n<pre>sed 's\/3\/\/;' va3_avail.txt | .\/callsigncheck.pl<\/pre>\n<p>I get one hit: <dfn title=\"Offering nothing that is stimulating or challenging\">vapid<\/dfn>. Which is now my call sign, <a href=\"http:\/\/qrz.com\/db\/VA3PID\">VA3PID<\/a>. Moohah.<\/p>\n<p>The Python version is much shorter, and I&#8217;m semi-impressed with the nifty little trick in line 5 (aka &#8216;dictionary comprehension&#8217;) which offers some hope for the future of terse, idiomatic code. The <a href=\"http:\/\/docs.python.org\/library\/fileinput.html\">fileinput<\/a> module gives Perlish stdin-or-ARGV[] file input, without which I&#8217;m sunk.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/python\r\nimport fileinput                        # Perl-like file input\r\n\r\n# get our wordlist\r\nwords={w.lower(): 1 for w in open('\/usr\/share\/dict\/words', 'r').read().split()}\r\n\r\n# read through input looking for matching words\r\nfor l in fileinput.input():\r\n    ll=l.lower().rstrip()\r\n    if words.get(ll, 0):\r\n        print(ll)\r\n<\/pre>\n<p>(So far, I&#8217;ve found the <a href=\"http:\/\/pleac.sourceforge.net\/\">PLEAC &#8211; Programming Language Examples Alike Cookbook<\/a> useful in comparing the languages.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python is okay, I guess, but there&#8217;s not a hint of music to it. I&#8217;m a dyed-in-the-wool Perl programmer since 4.036 days. When I think of how I&#8217;ll solve a programming problem, I think in Perl (or, more rarely, in PostScript, but I really have to be pretty off-balance to be thinking in stacks). I&#8217;m [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7],"tags":[187,2540,2510,2286],"class_list":["post-7754","post","type-post","status-publish","format-standard","hentry","category-computers-suck","tag-perl","tag-python","tag-raspberrypi","tag-va3pid"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pQNZZ-214","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/7754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/comments?post=7754"}],"version-history":[{"count":6,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/7754\/revisions"}],"predecessor-version":[{"id":7760,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/7754\/revisions\/7760"}],"wp:attachment":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/media?parent=7754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/categories?post=7754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/tags?post=7754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}