<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Coder Who Says Py - Medium]]></title>
        <description><![CDATA[Technical posts by Brett Cannon - Medium]]></description>
        <link>https://medium.com/coder-who-says-py?source=rss----c65447f85ebf---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*USZGdZSh2VHry0sD.jpeg</url>
            <title>Coder Who Says Py - Medium</title>
            <link>https://medium.com/coder-who-says-py?source=rss----c65447f85ebf---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 14:10:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/coder-who-says-py" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[I have moved over to Svbtle]]></title>
            <link>https://medium.com/coder-who-says-py/i-have-moved-over-to-svbtle-564d52028a65?source=rss----c65447f85ebf---4</link>
            <guid isPermaLink="false">https://medium.com/p/564d52028a65</guid>
            <dc:creator><![CDATA[Brett Cannon]]></dc:creator>
            <pubDate>Sat, 22 Feb 2014 23:57:21 GMT</pubDate>
            <atom:updated>2014-02-22T23:57:17.166Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*h4At0haL8wVm8CN5XFVqRA.png" /></figure><h4>http://nothingbutsnark.svbtle.com/</h4><p>While Medium’s WYSIWYG editor is rather nice, it just isn’t designed for code samples (e.g. there is no way to do &lt;code&gt; tags inline, only block-level &lt;pre&gt; tags). And for someone like me who writes code-heavy blog posts on occasion, it killed it for me.</p><p>Not to say Svbtle is perfect. While the Markdown support is great and even supports some GitHub Extensions (e.g. ```…``` code blocks), Svbtle doesn’t auto-save and the Google+ support isn’t as good so as to tie into Google+authorship. But the ability to simply use Markdown is enough to make me switch.</p><p>So the new blog is at <a href="http://nothingbutsnark.svbtle.com/">http://nothingbutsnark.svbtle.com/</a> . I have emailed the Planet Python folks to update my feed, but I know they get quite busy so it might be a week before it gets updated. And for those of you who <a href="https://plus.google.com/+BrettCannon">follow me on Google+</a> or <a href="https://twitter.com/brettsky">Twitter</a>, you should know that I have moved my ghost.io posts over to Svbtle as well.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=564d52028a65" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coder-who-says-py/i-have-moved-over-to-svbtle-564d52028a65">I have moved over to Svbtle</a> was originally published in <a href="https://medium.com/coder-who-says-py">Coder Who Says Py</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The key challenges in designing a programming language]]></title>
            <link>https://medium.com/coder-who-says-py/the-key-challenges-in-designing-a-programming-language-af0a418b6302?source=rss----c65447f85ebf---4</link>
            <guid isPermaLink="false">https://medium.com/p/af0a418b6302</guid>
            <dc:creator><![CDATA[Brett Cannon]]></dc:creator>
            <pubDate>Sat, 23 Nov 2013 01:01:07 GMT</pubDate>
            <atom:updated>2013-11-23T01:01:07.328Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*SLdj6VSSkqRUHX9EBrkynQ.jpeg" /><figcaption>© OpenDocs</figcaption></figure><h4>or “Why there is no (and never will be) a perfect programming language”</h4><p>Designing a programming language is hard. I have been involved with <a href="http://docs.python.org/devguide/">Python’s development process</a> as an official core developer of the language for a decade over Python’s 24 years in existence and we are still trying to get it right. I have learned a bunch of programming languages to varying levels of expertise to learn from them both as a programmer and a contributor to <a href="http://python.org/">Python</a>: C, C++, <a href="http://clojure.org/">Clojure</a>, <a href="https://www.dartlang.org/">Dart</a>, Forth, <a href="http://golang.org/">Go</a>, <a href="http://www.haskell.org/">Haskell</a>, <a href="http://www.cs.arizona.edu/icon/">Icon</a>, <a href="http://java.com/">Java</a>, <a href="http://www.ecmascript.org/">JavaScript</a>, <a href="http://www.lua.org/">Lua</a>, MIPS assembly, <a href="http://ocaml.org/">OCaml</a>, <a href="http://php.net/">PHP</a>, <a href="https://www.ruby-lang.org/">Ruby</a>, <a href="http://www.scala-lang.org/">Scala</a>, <a href="http://www.schemers.org/">Scheme</a>, <a href="http://www.smlnj.org/">Standard ML</a>, and <a href="http://tcl.tk/">Tcl</a>. None of these langauges are perfect.</p><p>Why is it so bloody hard to design programming language? I think it boils down to two overarching topics and the difficulty of finding the balance between them which will never please everyone all the time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/280/1*bN8SMrH3BVc3FuHDczp7vA.png" /></figure><h3>Maintainability</h3><p>You are either writing code, or you are fixing it. That’s (roughly) the life of a programmer.</p><p>Now this encompasses several bits. One is simply the ease of writing new code. If your language provides an easy way to write good, solid code the first time then you hopefully minimize the amount of time spent maintaining it. This is typically a balance of verbosity; type systems requiring more code and effort for the tradeoff of maintainability of having the compiler do some low-level sanity checks for you.</p><p>After that comes ease of debugging. This typically relates to what your compiler doesn’t check for you statically. How easy is it to work backwards from an exception or crash? How does the language facilitate you verifying what things are correct? How hard is it to write tests (which I’m assuming you write for all your code)?</p><p>And speaking of ease of writing tests, what the language facilitates by providing a standard library also comes into play, at least initially. If you have to write a map-like data structure from scratch or an HTTP header parser in order to make HTTP requests, your productivity is going to be low. This won’t necessarily last, however, as the community around a programming language matures and third-party projects being to flourish and evolve faster than the programming language. But the activeness of the community along with the standard library of the language to help get you going play a role in maintainability (both getting your code written and not having to maintain that code which you use).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*UQzTXhc_cx8TsVN4_uLlFA.png" /></figure><h3>Performance</h3><p>But no matter how easy a programming language makes it to write new code and later debug it, none of it is worth anything if the language is too slow. This is when flexibility is sacrificed, which can often mean sacrificing maintainability in terms of how much extra work is required to accomplish the same thing in a slower language. You can statically declare everything in your language as much as you want, but if all you can do is integer math and array usage (e.g. a Turing machine), you are not exactly going to be productive.</p><p>How you balance maintenance against performance decides what shape your programming language takes. And since you can never make the perfect programming language it will influence what programmers are attracted to your programing language.</p><p>Take C as an example. Tight, well-written C code is hard to beat short of writing assembly when it comes to performance. But I think any programmer would find it hard to argue that C code is easy to maintain compared to other options. Getting your memory management right can be tricky. Segfaults are not the easiest things to debug. The list goes on. C exemplifies maintainability sacrificed for speed.</p><p>Towards the other end of the spectrum is Python. Writing code in Python is fast and highly productive. The language is a good balance of high-level constructs along with a small core language so that an experienced programmer can keep the whole language (sans black magic) in their head. And the black magic is there to help when needed (e.g. frame access for debugging). You shouldn’t be able to segfault your program from straight Python code that doesn’t do anything segfault-y (e.g. ctypes). And the debug cycle is extremely fast thanks to the lack of a expensive compilation step. But Python’s performance is not that of C. While you can get great performance with <a href="http://pypy.org/">PyPy</a>, Python’s “good enough” speed is not always good enough for everyone in all situations.</p><p>Then you start looking at languages that try to strike a balance between high maintainability and great speed; do you pay for your program with man-hours or CPU cycles? Go seems to be the newest favourite amongst people trying to find more of a middle ground. While typed, it heavily relies on type inference and uses interface typing over inheritance typing. It has fast compilation time to have a faster iteration cycle. Memory management is handled for you. And it’s fast. But it does have the issue (for <a href="http://alexgaynor.net/">some</a>) of very explicit error handling (which if you remember Go’s systems programming pedigree makes it not surprising). Lack of generics and access to the low-level guts of the language can lead to more work to accomplish something than it would in Python.</p><p>So what’s the point of this rant? Mainly to point out that programming languages are hard and thus there is no perfect language, now or ever. When you choose a language for a project you need to weigh performance against maintainability of what you will be producing; I would say you should figure what kind of performance needs you have and what computing resources are at your disposal and then find the most productive language that meets those performance needs (e.g. why the Python community has always said to prototype in Python before worrying if you actually need to go faster than what Python + C or PyPy can provide). Also be willing to learn a new language to see if it fits your style of coding better than your current favourite language since people will never stop trying to strive for that impossible goal of the perfect programming language.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af0a418b6302" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coder-who-says-py/the-key-challenges-in-designing-a-programming-language-af0a418b6302">The key challenges in designing a programming language</a> was originally published in <a href="https://medium.com/coder-who-says-py">Coder Who Says Py</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[My Thoughts on Go (again)]]></title>
            <link>https://medium.com/coder-who-says-py/my-thoughts-on-go-again-3228b624c946?source=rss----c65447f85ebf---4</link>
            <guid isPermaLink="false">https://medium.com/p/3228b624c946</guid>
            <dc:creator><![CDATA[Brett Cannon]]></dc:creator>
            <pubDate>Sat, 02 Nov 2013 21:02:32 GMT</pubDate>
            <atom:updated>2013-11-02T21:02:32.349Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*7kbd-tVk3co-9RiilFN1TA.png" /></figure><p>A little over three years ago I blogged <a href="http://sayspy.blogspot.ca/2010/10/my-thoughts-on-go.html">my thoughts on the Go programming language</a>. At the time I said <a href="http://www.python.org/">Python</a> &gt; <a href="http://www.scala-lang.org/">Scala</a> &gt; <a href="http://golang.org/">Go</a>. I came to that conclusion by reading all of the <a href="http://golang.org/doc/">various docs available on the Go website</a> and then doing <a href="https://github.com/brettcannon/find-duplicate-files">a couple of toy examples</a>. But since I did that evaluation I have shifted from a breadth-based evaluation of various programming languages to a more depth-based one (mainly due to a lack of time and thus having to be picky about what programming languages I learn). I’m also evaluating languages for a website that I have been planning to build for years which will have both a front-end and back-end component. That means choosing two languages for the project. For the moment I’m focusing on the back-end language.</p><p>Since I’m going to be doing this project with <a href="http://www.karlschmidt.net/">a good friend of mine</a> I’m not instantly choosing Python as the back-end language. While Python would let me be the most productive, a personal programming project should be fun, useful in a utilitarian sense, and a learning experience. While doing Python web back-end stuff would of course lead to some learning, it wouldn’t be nearly as much of a learning experience if I did the project in a different language. Plus using Python might not be as useful professionally for my friend as other languages.</p><p>But what language we choose can’t suck. =) While being an admitted programming language polyglot/whore, I am also willing to admit I&#39;m an opinionated/biased polyglot/whore. That means I&#39;m not going to use just any language since I would like to think I know good language design is and thus can’t stand poorly designed languages (just ask my co-workers).</p><p>And so I am evaluating Go and Scala as potential back-end languages for this project. Both are somewhat niche compared to e.g. Java, but they are gaining users consistently. Both are being used at real world companies which scale to large user bases. Both languages are high level and so I don&#39;t have to care about things like memory management or pointer fiddling. Both languages are stable and passed their 1.0 releases. But most importantly, both languages don’t drive me bat-shit crazy.</p><p>For Scala I have been taking the “<a href="https://class.coursera.org/progfun-003/class/index">Functional Programming Principles in Scala</a>” course at Coursera to refresh my functional programming chops as well as Scala knowledge (plus another friend of mine said she would learn Python if I took the course, so there’s that =). But for Go I didn’t have an equivalent (I took <a href="http://tour.golang.org/">A Tour of Go</a> previously, but it doesn’t teach me a programming paradigm I rarely get to use).</p><p>Luckily a little coding project popped up for me that Go seemed suited for: I needed to find out what files were duplicates of each other. While not complicated or requiring any novel programming solution, that’s actually not a bad thing when evaluating a language. It means that for the mundane stuff you can see if the language functions in a way that makes sense to you and allows you to be productive. And productivity is important. For one this is a personal project so I don’t want or need it to scale to a Google service so I don’t need to consider sacrificing my time in the name of efficiency (e.g. I ain’t going to use C++ to try and eek out every cycle of performance). I also have a limited amount of free time so I don’t want to sacrifice more time than I have to (within reason, obviously, else I would just use Python).</p><p>Anyway, I ended up <a href="https://github.com/brettcannon/find-duplicate-files">coding a solution</a> over a couple of nights when I had a few spare minutes that totals a little over 200 lines. That includes both a synchronous and partially concurrent solution. There is slightly less than that number of lines of testing code as well. So nothing <em>that</em> complicated, but it did involve all the basics and allowed me to get more of a feel of the language and a refresh of what I already knew from three years ago.</p><p>What conclusions about Go did I draw? You can tell the language was designed by systems programmers who wanted to be more productive (which makes sense if you know Go’s history). From the support of passing objects by pointer to the lack of exceptions, it’s obvious Go team members come from a history steeped in C++ coding (at Google especially) where they grew tired of manually managing memory and such but didn&#39;t want to entirely want to toss out all of the practices found in C/C++. And that’s totally understandable since you don’t necessarily want to fully diverge from common practices of your target audience, else you risk not gaining users.</p><p>And yet, somewhat ironically, it isn’t the C/C++ programmers that have taken to Go but the Python/Ruby programmers. Why is that? Well, when I have watched presentations on Go and there is a die-hard C++ programmer in the audience who asks a question, it typically revolves around how to gain back some control that Go has taken away, e.g. turning off garbage collection or more pointer control. In other words the typical programmer who loves C++ doesn&#39;t want to give up any control, while Go’s entire point is to take some of that control away to try and make you more productive.</p><p>Dynamic programming language lovers, though, are used to giving up plenty of low-level control for productivity gains. And so when a language like Go comes along that is small enough to fit in your head and gives you great performance characteristics you don&#39;t mind that you are giving up some high-level constructs for low-level speed boosts. That’s why some Python/Ruby programmers are switching. It does paint Go in a different light if you take it as Python but with typing and certain performance-hurting aspects removed compared to C++ but with abilities removed to make your life simpler.</p><p>Personally, I don&#39;t buy into the trade-off wholesale. I would have to measure performance against using <a href="http://www.pypy.org/">PyPy</a> in a project before I started tossing Python aside entirely and losing it’s flexibility in the name of speed. I do see the appeal though. Nothing really jumped out at me as horribly designed or a something that would totally drive me bonkers. Probably the biggest stumbling block was coming up with some idiomatic way to have a pool of goroutines to handle the hashing of files. After that it was realizing `defer` calls are executing at function exit and not code block exit (RAII in C++ and context managers in Python spoil you). Otherwise nothing leapt out at me to cause me to want to drop the project.</p><p>In the end, Go is still a viable language to use. Yes, it requires a bit more work than Python to reach the same semantic conclusion, but it isn&#39;t enough to drive me up the wall. And the trade-offs it asks of me to write maintainable, fast code do not feel systematically unreasonable. If someone wouldn&#39;t let me use Python for a project I would present Go as an alternative.</p><p>Now I just need to see how more Scala stacks up with some other test project to see which language I prefer behind Python.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3228b624c946" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coder-who-says-py/my-thoughts-on-go-again-3228b624c946">My Thoughts on Go (again)</a> was originally published in <a href="https://medium.com/coder-who-says-py">Coder Who Says Py</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Taking the Time to Re-evaluate Assumptions]]></title>
            <link>https://medium.com/@brettcannon/taking-the-time-to-re-evaluate-assumptions-46f13da6a4ec?source=rss----c65447f85ebf---4</link>
            <guid isPermaLink="false">https://medium.com/p/46f13da6a4ec</guid>
            <dc:creator><![CDATA[Brett Cannon]]></dc:creator>
            <pubDate>Mon, 09 Sep 2013 22:42:54 GMT</pubDate>
            <atom:updated>2013-09-09T22:42:54.185Z</atom:updated>
            <content:encoded><![CDATA[<h4>Or “Hey, I have enough storage to use FLAC files!”</h4><p>Up until today I never really thought about using <a href="http://en.wikipedia.org/wiki/FLAC">FLAC</a> for my personal audio files.</p><p>Yes, I still own and buy digital music (Andrea is the one with the <a href="http://www.rdio.com/">Rdio</a> account). I like supporting artists I like. Plus I’m one of those people who will create a playlist and loop on it all day, every day, for a week so I don’t need a music subscription to mix things up (plus Andrea lets me know about new stuff). So for me, I listen to music through <a href="https://play.google.com/music/listen">Google Play Music</a> or my <a href="http://www.sonos.com/">Sonos</a> through local files.</p><p>Don’t ask me why, but I realized today that my storage situation for my music files had reached a point to make it feasible to use FLAC/<a href="http://en.wikipedia.org/wiki/Apple_Lossless">ALAC</a> versions of music. Maybe it was reading an article about <a href="http://www.theverge.com/2013/8/28/4667966/nin-audiophile-mastered-version-hesitation-marks">NIN’s “audiophile master” version of <em>Hesitation Marks</em></a> on <a href="http://www.theverge.com/">The Verge</a> and the mention of Neil Young’s forthcoming music service and player which focus on uncompressed music on that made me think about it. But I realized that my old habit of always using high bitrate mp3s wasn’t really necessary and I might as well just use the lossless file formats. It’s not like storage space is an issue anymore (I use a 64 GB USB 3 flash drive to serve my music from my router), so why bother losing any fidelity? Yes, I realize that my Sonos player might not really take advantage, but what’s the point of risking fidelity loss when space is no longer an issue?</p><p>While I wait to hear back from <a href="http://store.nin.com/">store.nin.com</a> tech support on my account so I can buy the latest album, I realized that if anyone was going to have given me lossless copies of files it would be Trent Reznor . And lo and behold, I had FLAC copies of <a href="http://ghosts.nin.com/">Ghosts I-IV</a> sitting amongst my music files. I checked that <a href="https://sonos.custhelp.com/app/answers/detail/a_id/80/~/supported-audio-formats">Sonos</a> and <a href="https://support.google.com/googleplay/answer/1100462?hl=en">Play Music</a> supported FLAC (they do), and so I went ahead and started using the FLAC versions and tar.xz’ed the mp3s for cold storage.</p><p>Obviously switching to FLAC over mps3 isn’t a revelation that is going to take the internet by storm. But it does make a good story that it does pay to stop on occasion and challenge one’s assumptions about what makes sense, especially in technology. I grew up during a time when encoding an mp3 took <strong>hours</strong>. Now it encodes faster than listening to the music. I remember my <a href="http://en.wikipedia.org/wiki/Ipod#Models_and_features">second-generation iPod which was 10 GB</a> was considered big (1/6 the size of the little flash drive protruding out of the back of my router with my entire music collection). Over these years my assumptions have just continued such that I never stopped to think that my aversion to using FLAC because of its file size compared to high bitrate mp3s and the lack of high-end music components negated the need to use FLAC. But now, thinking about it, why not use FLAC? Why risk not being able to gain even a sliver of clarity at the cost of some storage space?</p><p>So stop and think about what drives a decision on occasion? Especially with technology it quite possibly has been altered or entirely nullified such that you need to re-evaluate your decision.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=46f13da6a4ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Offline JavaScript Testing]]></title>
            <link>https://medium.com/coder-who-says-py/offline-javascript-testing-4a535ed1e5df?source=rss----c65447f85ebf---4</link>
            <guid isPermaLink="false">https://medium.com/p/4a535ed1e5df</guid>
            <dc:creator><![CDATA[Brett Cannon]]></dc:creator>
            <pubDate>Mon, 02 Sep 2013 18:35:22 GMT</pubDate>
            <atom:updated>2013-09-02T18:41:10.437Z</atom:updated>
            <content:encoded><![CDATA[<h4>Or “How I tested my file://localhost web app”</h4><p>As the creator of <a href="https://oplop.appspot.com/">Oplop</a> and maintainer of its <a href="https://github.com/brettcannon/oplop/tree/master/HTML">primary implementation in HTML + JavaScript</a>, I feel an obligation to not break anything. If I screw up then someone ends up with an incorrect account password or can’t them when they need to. Me screwing up is a bad thing.</p><p>And so to help prevent that I figured I should actually test Oplop’s web app code. Now when it comes to testing web apps there are roughly two parts to a complete testing plan: unit tests and UX/end-to-end/integration/call-it-whatever-you-want-when-you-click-buttons tests. This post is purely about the former as I am lucky enough to have a web app whose UX is simple and small enough for me to test manually (although I hope to automate even those tests some day as well).</p><p>So for these previously mythical unit tests I needed a solution which would work offline (i.e. file://localhost is where all files served from) as Oplop uses the <a href="https://developer.mozilla.org/en/docs/HTML/Using_the_application_cache">HTML application cache</a> and so fancy XHR loaders and such are not an option. I also wanted a solution that could be used in some constant integration (aka CI) solution so that I would never accidentally break anything. This led to some digging online to figure how to hell to make this happen. It essentially broke down into three parts: the unit testing library, a way to drive those tests, and a CI solution to make sure whenever a commit occurred the tests still passed.</p><p>There are a lot of JavaScript unit testing frameworks out there. You could use <a href="http://qunitjs.com/">QUnit</a> from the <a href="https://jquery.org/">jQuery Foundation</a> or go more fancy and use a project like <a href="http://visionmedia.github.io/mocha/">Mocha</a> which lets you choose your own assertion library and then provides the extra stuff to write the unit tests. In the end I went with <a href="http://pivotal.github.io/jasmine/">Jasmine</a> as it just made the most sense to my brain at first reading and didn’t try to go overboard with features.</p><p>I also tossed in <a href="https://github.com/velesin/jasmine-jquery">jasmine-jquery</a> for good measure. That gave me some assertion functions that help with jQuery code. It also gave support for fixtures. Now this was the one place where working from file://localhost to guarantee compatibility with the application cache was annoying. Normally jasmine-jquery uses XHR to load fixtures so that you can keep them in separate files. Unfortunately XHR doesn’t like file://localhost as it views <strong>every</strong> file as its own domain from XHR’s perspective. So I had to inline string constants to get the fixtures I wanted. Not horrible, but a little annoying.</p><p>With Jasmine + jasmine-jquery I was able to write my tests and use Jasmine’s test runner to make sure my tests all executed properly. But that only gets me an HTML page in a single browser to verify the tests worked. What about running in multiple browsers at once? Or how about from the command-line for CI usage?</p><p>This is where a full-feature test runner comes into play. <a href="https://code.google.com/p/js-test-driver/">JsTestDriver</a> is the old-school test driver/runner which would execute your tests on multiple browsers simultaneously and then report back the results in the terminal. I actually once used JsTestDriver with QUnit on another project so I knew I wanted something like it, but I decided to look for something newer.</p><p>I ended up finding <a href="http://karma-runner.github.io/">Karma</a> from the <a href="http://www.angularjs.org/">AngularJS</a> team. It’s the spiritual successor to JsTestDriver and works well. It uses <a href="http://nodejs.org/">node.js</a> for installation and supports all the major browsers along with file monitoring so it automatically re-runs itself upon file change. It even launches the browsers for you and shuts them down when you quit. It also includes a Jasmine interface so that in my case setup didn’t require some other package to work with the unit test framework.</p><p>With Karma I’m able to test with <a href="http://www.chromium.org/blink">Blink</a>, <a href="https://developer.mozilla.org/en-US/docs/Mozilla/Gecko">Gecko</a>, and <a href="http://www.webkit.org/">WebKit</a> thanks to <a href="https://www.google.com/intl/en/chrome/browser/">Chrome</a>, <a href="http://www.mozilla.org/en-US/firefox/new/">Firefox</a>, and <a href="http://phantomjs.org/">PhantomJS</a> (+ <a href="http://www.apple.com/ca/safari/">Safari</a> on my laptop). And all of this can be done from the command-line so that it can be used from a CI solution.</p><p>I should also mention <a href="http://casperjs.org/">CasperJS</a> which is a PhantomJS/SlimerJS-specific test runner that I considered. Unfortunately that leaves out Blink and so coverage isn’t quite as wide with what’s possible with Karma.</p><p>With a unit testing framework to write tests in and a test runner to run the tests in as many browsers as possible, that just left a CI solution. Lucikily I had already discovered <a href="https://drone.io/">drone.io</a> for Oplop previously for use with its Python implementation. Some people really like <a href="https://travis-ci.org/">Travis</a> for CI, but it’s restricted to GitHub while drone.io works with GitHub, Bitbucket, and Google Code which I appreciate since I still have projects on Google Code.</p><p>The setup was extremely simple. You can see the setup at <a href="https://drone.io/github.com/brettcannon/oplop">Oplop’s drone.io page</a>, but it’s so short I can just paste the relevant bit here:</p><pre>npm install karma<br>sudo start xvfb<br>./node_modules/.bin/karma start karma.js --single-run</pre><p>Basically you just install Karma, activate the X11 screen buffer implementation, and then run your tests once. That will run in Chrome, Firefox, and PhantomJS (including automatically downloading and using the newest version of PhantomJS!) and make sure the tests pass in all browsers.</p><p>Overall I’m happy with the outcome. I’ve already used the tests to make sure I didn’t botch anything when I moved a namespace-based solution for modularizing the JavaScript code. And by refactoring my JavaScript to minimize the UI assumptions it made I can test most of the UX without having to drive the browser itself, leaving me with minimal manual testing (still plan to look into getting Selenium running so I don’t even need to do that).</p><p>The biggest pain was simply figuring all of this out. Seems everyone has their own opinion and way to do this sort of thing so there’s no authoritative solution to go off of. Plus you have to realize that a unit test framework that includes an HTML page for a test runner won’t get you simple multi-browser testing nor a way to integrate into a CI solution. I’m glad I put the time into researching all of this, but geez the world doesn’t make it straight-forward if you don’t know what you are doing.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4a535ed1e5df" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coder-who-says-py/offline-javascript-testing-4a535ed1e5df">Offline JavaScript Testing</a> was originally published in <a href="https://medium.com/coder-who-says-py">Coder Who Says Py</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why I will never fit in amongst C++ programmers]]></title>
            <link>https://medium.com/coder-who-says-py/why-i-will-never-fit-in-amongst-c-programmers-3616b9e389c9?source=rss----c65447f85ebf---4</link>
            <guid isPermaLink="false">https://medium.com/p/3616b9e389c9</guid>
            <dc:creator><![CDATA[Brett Cannon]]></dc:creator>
            <pubDate>Sun, 21 Jul 2013 17:59:19 GMT</pubDate>
            <atom:updated>2013-07-21T17:59:11.816Z</atom:updated>
            <content:encoded><![CDATA[<h4>Or “Why I don’t give a crap about your types and neither should you”</h4><p>I have been actively programming in <a href="http://www.python.org/">Python</a> for nearly 13 years, ten of which have been spent as a core developer of the language itself. You could say I have an opinion when it comes to programming languages. That I have taken sides. That I am a programming language bigot.</p><p>And you would kind of be right. Obviously I have chosen as my programming language of choice when I can use it. But I’m also not going to sit here and claim that Python is the best solution to every problem, nor that people have to like Python. All I ever ask of people who prefer to use other programming languages is to have a justification as to why and it be rational.</p><p>But when I run into people who can’t justify why they prefer to do something in a programming language a certain way without being able to give me what I view as a rational explanation, it drives me up the absolute wall. It’s baseless ignorance that has typically led to people bashing Python or dynamically typed programming languages back when Python wasn’t known by at least name to every programmer out there. I have a bachelors in philosophy and a Ph.D. in computer science: do not give me crap logic in general, and especially not when it comes to programming.</p><p>And yet this happens regularly. For some ironic reason,programmers are curmudgeons like everyone else when something new is presented to them. Programmers work on a daily basis with technology that has revolutionized the world, with tools that they can update on a whim, and yet when provided an opportunity to try something new which could improve their programming lives or just simply be different, they push back. “I like things just as they are.” Yes, my non-programming friends, programmers can often fear change (or at best be indifferent to the point of inaction).</p><p>The latest example I came across involves <a href="https://en.wikipedia.org/wiki/C%2B%2B11">C++11</a> and its new <a href="https://en.wikipedia.org/wiki/C%2B%2B11#Type_inference">‘auto’ keyword</a> after a conversation I had with some long-term C++ programmers (I have only been using the language part-time for a year so I typically defer to others for coding best practices). For those of who you don’t know, the ‘auto’ keyword does basic type inference for local variables where the type can be inferred by the return type of some expression (e.g. a function call’s return value). For those used to languages that provide type inference it brings C++ in line with languages where you only have to care at API boundaries where types act as documentation/contract (which I’m totally fine with, as are the language designers of <a href="http://www.scala-lang.org/">Scala</a>, <a href="http://golang.org/">Go</a>, <a href="http://www.dartlang.org/">Dart</a>, etc. just to name some newer examples). And for people used to dynamically typed languages this is just one less place to care about types which you have already learned how to live without (hopefully happily).</p><p>But when I suggested using ‘auto’ in a situation like this:</p><pre>void UppercaseSomething(SomeStruct&amp; thingy) {<br>  // …<br>  auto uppercase_something = thingy.get_something();<br>  UppercaseString(&amp;uppercase_something);<br>  thingy.set_something(uppercase_something);<br>  // …<br>}</pre><p>I had a bunch of C++ programmers tell me they disagreed with my usage of ‘auto’ for the ‘string’ type. I was given two reasons why this disagreement existed:</p><ol><li>The ‘string’ type is too “simple” to bother inferring</li><li>The ‘string’ type is short enough to not need the use of ‘auto’</li></ol><p>Up to this point using ‘auto’ seemed reasonable to everyone for complicated types and types whose names were a pain to type out. But to someone like me who prefers to live in a world where the only types I ever care about are at the API boundary, being told ‘string’ is too ‘simple’ and/or short for ‘auto’ just flabbergasted me.</p><p>When I was told the type was too “simple”, in actuality it was meant to mean that it’s so fundamental to the language that everyone knows it and so knowing it’s a string means everyone knows exactly what methods are available, what it’s memory model and semantics are, etc. By forcing people to write ‘string’ it lets people know they are working with something for which they can <em>probably</em> skip reading the docs for.</p><p>To this I said “seriously?!?” and then tried to properly articulate myself. To start, if you think ‘string’ is so fundamental, what about ‘iterator’? Everyone seemed fine with using ‘auto’ for the ‘iterator’ type from the STL, and yet that API is rather fundamental in C++. Same goes for ‘vector’. This judgment call over what is or is not fundamental to C++ is just that: a judgment call. Every other language that I know which provides type inference in no way encourages people to bother specifying the type for “simple” things like strings. In other words this classification of ‘string’ being so fundamental seemed borderline arbitrary and not to have proven itself as critical in any other programming language out there that provided type inference.</p><p>But this also assumes you can’t infer the ‘string’ type from either the expression setting the variable or its use around it. In the example above I think ‘UppercaseString()’ gives the type away. But if not knowing what type a variable is by strictly reading it truly bothered you then you probably shouldn’t be using ‘auto’ anywhere to begin with (this is where Java programmers might be chuckling to themselves).</p><p>I would also argue that using ‘auto’ makes the key points about variable access of the type more clear. If you had to read ‘const string&amp;’ and ‘const auto&amp;’ I would argue they are at worst equally readable to deduce that the variable holds a const reference, and at best the auto usage is easier as you don’t have to mentally ignore ‘string’ to understand that it doesn’t use pointer access.</p><p>And addressing the length argument for typing also seems arbitrary. If I already typed out ‘auto’ then who cares about the length? It’s still shorter anyway. But this argument was more for something like ‘int’. Okay, save a character, but is that really all that important? What about when that ‘int’ turns into a ‘long’, or a ‘long long’? Now you have no savings or actually typed more.</p><p>And even more important, what about refactoring? At least using ‘auto’ you have a chance of cutting back on refactoring errors as you don’t have to care about local variables, only non-compatible API boundary types no longer working.</p><p>In other words, it felt like I was back in a world where people made claims about a change they were introduced to without having evidence to back up those claims. The only argument I could possibly buy into is the whole “everyone knows what a string is, so when you know that’s the type you should just go ahead and type it”, but even then, switching back and forth between ‘auto’ and types deemed too simple seems like it would be mentally annoying at best and taxing at worst as a judgment call I would have to think about every time I touched/wrote a variable declaration. In other words not worth it in my book to make the distinction, especially as no other language with type inference that I have come across has suggested that kind of selective type declarations for local variables.</p><p>If people have suggestions or alternative explanation as to why everyone else was right I’m quite ready to listen and try to understand because I would like to understand in order to be a better C++ programmer and to understand the mindset.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3616b9e389c9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coder-who-says-py/why-i-will-never-fit-in-amongst-c-programmers-3616b9e389c9">Why I will never fit in amongst C++ programmers</a> was originally published in <a href="https://medium.com/coder-who-says-py">Coder Who Says Py</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing a Zip File Importer: The Path Hook]]></title>
            <link>https://medium.com/@brettcannon/writing-a-zip-file-importer-the-path-hook-2dbd375e00b9?source=rss----c65447f85ebf---4</link>
            <guid isPermaLink="false">https://medium.com/p/2dbd375e00b9</guid>
            <dc:creator><![CDATA[Brett Cannon]]></dc:creator>
            <pubDate>Sat, 22 Jun 2013 00:12:14 GMT</pubDate>
            <atom:updated>2013-06-22T00:07:28.108Z</atom:updated>
            <content:encoded><![CDATA[<h4>Part 1</h4><p>There are essentially three parts to any importer in Python 3:</p><ol><li>The <a href="http://docs.python.org/3/glossary.html#term-path-entry-hook">path hook</a></li><li>The <a href="http://docs.python.org/3/glossary.html#term-finder">finder</a> (<a href="http://docs.python.org/3/glossary.html#term-meta-path-finder">meta</a> or <a href="http://docs.python.org/3/glossary.html#term-path-entry-finder">path entry</a>)</li><li>The <a href="http://docs.python.org/3/glossary.html#term-loader">loader</a></li></ol><p>In three posts I hope to write about the creation of each needed class in order to come up with a new zip-based importer to replace <a href="http://docs.python.org/3/library/zipimport.html#module-zipimport">zipimport</a> (<a href="http://bugs.python.org/issue17630">issue #17630</a>).</p><h3>Why Replace zipimport?</h3><p>You might be wondering why would I go to all of this trouble to replace a module that seems to basically work? In essence: <em>maintaining C code sucks compared to Python code</em>. You see, zipimport is written entirely in C for bootstrapping reasons; you can’t load stdlib modules from a zip file if you can’t load the zip file importer which depends on those stdlib modules itself.</p><p>But this has the problem of maintaining C code. Did you know zipimport has its own implementation of a zip file reader? Yeah, which means any fixes or improvements which may go into the <a href="http://docs.python.org/3/library/zipfile.html#module-zipfile">zipfile</a> module do not make there way into zipimport. E.g. you ain’t about to get ZIP64 support in zipimport unless you want to write a patch to support it in the C code. It also means that improving the importer to be more flexible is simply not supported as no one wants to put the effort in. But if a zip file importer was written in Python, then supporting things that zipfile supports, allowing for alternative loaders, etc. don’t end up seeing like such a stretch.</p><h3>The Path Hook</h3><p>Creating a path hook involves writing an object which, when called, can make a decision as to whether it can provide a finder for a specific path. In the case of <a href="http://docs.python.org/3/library/importlib.html#importlib.machinery.FileFinder">importlib.machinery.FileFinder</a>, its path hook returns a finder whenever it is presented with a directory. For a zip file path hook, it needs to detect a zip file. Sounds easy, right?</p><p>The trick with path hooks is handling the case where the “directory” for a package has been appended to the original thing typically found in sys.path. In the case of zip files, it’s when you have a path to something like /path/to/code.zip but are in some package named pkg: /path/to/code.zip/pkg/ (notice how the two paths are mushed together). Because people tweak package locations in __path__ on occasion you typically want to try and handle this case instead of just throwing your hands up and saying “screw __path__ manipulations, I will figure out where a module is from the top of a zip file and not care about funky choices made by users”. While it’s tempting to do this (especially for things like database importers where the concept of a file path is in no way inherent in the storage mechanism), in the case of zip files it makes sense to try and support this (unlike zipimport).</p><p>How do you do this? Well, you start with the path you were given, and check if it’s a file. If it is, is it a zip file? If so then great! If not then you can’t handle this path. Otherwise you slice off the end of the path, walking up until you hit a file or run out of path parts to slice off. It’s a bit clunky, but the only other alternative is to match based on some substring like “.zip” which is rather brittle.</p><p>But that’s actually it, by definition, for supporting a path hook: is this path something I can provide a finder for? Sometimes, though, you have one other question to worry about: is there something I should be caching in my path hook?</p><p>See, a single zip file might end up with a bunch of different paths pointing into it thanks to various packages it contains. Now each of those paths will have a finder stored in sys.path_importer_cache. Each of those finders will have to check if a path exists in the zip file, and then eventually pass on the zip file object to the loader so it can actually read the data it needs to load a module. What all of this means is that multiple finders can point to a single zip file (e.g. /path/to/code.zip/pkg/, /path/to/code.zip/pkg/subpkg/, etc.). You really don’t want to have to be constantly opening and closing zip file for every import. So in this instance it’s best to cache zip files in the path hook. That way finders can just ask the path hook for the zip file when it needs it and thus only have a single zipfile.ZipFile object open per zip file. Saves on stat calls, overhead, etc.</p><p>And that’s it! The code for a zip file path hook is all about finding out if a path has a zip file in it and managing the cache of zip files.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2dbd375e00b9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing a Zip File Importer: The Finder]]></title>
            <link>https://medium.com/@brettcannon/writing-a-zip-file-importer-the-finder-183a8410bd27?source=rss----c65447f85ebf---4</link>
            <guid isPermaLink="false">https://medium.com/p/183a8410bd27</guid>
            <dc:creator><![CDATA[Brett Cannon]]></dc:creator>
            <pubDate>Sat, 22 Jun 2013 00:14:50 GMT</pubDate>
            <atom:updated>2013-06-22T00:14:50.704Z</atom:updated>
            <content:encoded><![CDATA[<h4>Part 2</h4><p>When writing a <a href="http://docs.python.org/3/glossary.html#term-finder">finder</a> like <a href="http://docs.python.org/3/library/importlib.html#importlib.machinery.FileFinder">importlib.machinery.FileFinder</a> it’s all about working with the storage mechanism you are trying to support. In this instance it’s making sure that the paths the finder is meant to work with are serviceable and being able to check if certain files exist.</p><p>In the case of zip files, it’s tricky because the paths use <strong>/</strong> as a path separator. If someone on Windows adds a path on sys.path using <strong>\</strong> as the path separator, you need to support it. That means constantly translating between OS-specific paths and zip file paths.</p><p>Once you have that down, then you need to implement your finder so that when you are asked if it can provide a loader for a module, it can search within the zip file in the directory the finder is meant to be in charge of to see if a source file can be found. This is the core of a finder and it takes a few steps:</p><ol><li>See if a directory matches the tail end of the module being search for, and if it does record that fact</li><li>If a directory exists then check for an __init__.py file for it to be a real package</li><li>If no real package is found, check for a .py file</li><li>If none of that works but a directory was found then return it as a portion of a possible future namespace package</li></ol><p>All of this roughly translates into the following code:</p><pre>def find_module(self, fullname):<br>  portions = []<br>  module_name = fullname.rpartition(‘.’)[-1]<br>  directory = self.make_directory_path(module_name)<br>  if self.has_directory(directory):<br>    portions.append(directory)<br>    package_base = self.join(directory, ‘__init__’)<br>    for suffix in SOURCE_SUFFIXES:<br>      package = package_base + suffix<br>      if self.has_file(package):<br>        return self.loader(fullname, package), portions<br>  module_base = self.make_file_path(module_name)<br>  for suffix in SOURCE_SUFFIXES:<br>    module = module_base + suffix<br>    if self.has_file(module):<br>      return self.loader(fullname, module), portions<br>  return None, portions</pre><p>The tricky bit that came out of implementing this general algorithm for zip files is that there are the paths you are using to look in the zip file (i.e. paths with only <strong>/</strong> as the path separator) and then the paths that you are shuttling around to support people passing in OS-specific paths. In other words the paths coming in and going out of that method need to use the path separators that the user chose to work with while everything internal needs to work with <strong>/</strong> only (and on top of that have directories all end in <strong>/</strong>). And it’s that weird dichotomy of directories that prevents importlib from having just a generic algorithm with some abstract API for which finder subclasses can simply override.</p><p>Luckily people don’t typically muck with the storage mechanisms of module code. I’m willing to bet something like 95% of all code is backed by files on a filesystem, 4% are zip files, and the rest is some exotic, custom finder (e.g. SQL database). So for most people writing custom importers it’s going to come down to the loader and not the finder.</p><p>Anyway, that’s the key points for handling a zip file finder: managing paths and re-implementing the basic algorithm for determining if a package, module, or namespace exists for the requested module.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=183a8410bd27" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing a Zip File Importer: The Loader]]></title>
            <link>https://medium.com/@brettcannon/writing-a-zip-file-importer-the-loader-c84fc90f7f5d?source=rss----c65447f85ebf---4</link>
            <guid isPermaLink="false">https://medium.com/p/c84fc90f7f5d</guid>
            <dc:creator><![CDATA[Brett Cannon]]></dc:creator>
            <pubDate>Sat, 22 Jun 2013 00:16:52 GMT</pubDate>
            <atom:updated>2013-06-22T00:16:52.498Z</atom:updated>
            <content:encoded><![CDATA[<h4>Part 3</h4><p>For a zip file-based <a href="http://docs.python.org/3/glossary.html#term-loader">loader</a>, luckily there is not much heavy lifting. Working with the finder led to the realization that zip files use <strong>/</strong> while users use whatever the hell they want for a path separator. With that path-managing code already dealt with, that leaves just implementing the <a href="http://docs.python.org/3/library/importlib.html#importlib.abc.SourceLoader">importlib.abc.SourceLoader</a> ABC. That requires implementing the following methods:</p><ul><li>get_filename(fullname)</li><li>get_data(path)</li><li>(optionally) path_stats(path)</li></ul><p>Not much, eh? This is the kind of thing that I view as the crowning achievement of importlib (that and making import maintainable in pure Python code). By staring at the interfaces originally defined in <a href="http://python.org/dev/peps/pep-0302/">PEP 302</a> for literally years, I have slowly been able to refine the algorithms involved with import (and especially loading) to the bare essentials that are absolutely required at any one point based on what may vary between uses (e.g. storage mechanism, variance of the language, etc.).</p><p>In the case of zip files, the three methods to be implemented are rather easy. The get_filename() method just returns what was given to the constructor. The get_data() method needs to translate paths which use OS-specific path separators and possibly starting with the zip file path as a prefix, but otherwise it’s just reading from a zip file and returning the bytes. The path_stats() method is actually quite easy thanks to <a href="http://docs.python.org/3/library/zipfile.html#zipinfo-objects">zipfile.ZipInfo</a> objects. Implement those three methods and you have source loading along with optional bytecode loading (toss in set_data() and you have bytecode writing as well, but for overhead reasons I didn’t implement it).</p><p>And that’s it! If you look back through the collection of posts on this topic you will find posts on the path hook, finder, and this loader post which cover (roughly) everything needed to make a zip file importer in pure Python come about. Issue <a href="http://bugs.python.org/issue17630">#17630</a> is tracking the issue of whether this code should end up in the stdlib (it’s a little sticky with zipimport there for backwards-compatibility and bootstrapping reasons, so a pure Python importer is duplicate functionality, albeit with easier maintainability and some niceties).</p><p>One thing that I should mention which is not critical to understanding how I went about writing a loader for zip files is a quirk of trying to work with bytecode files in zip files. It turns out the zip files only keep a modification time at a <strong>two</strong> second granularity. Yes, you read that right: two seconds. That means only even numbered seconds are possible. Unfortunately that’s troublesome as importlib as of this writing does just a straight numeric comparison of the timestamp found in the bytecode file and what path_stats() returns for the source file. In order to properly support bytecode files in order to minimize ignoring them I will probably have to extend the API for loaders to add a compare_mtime(source, bytecode) in order to support a one second fuzziness factor which can be overridden for this special case.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c84fc90f7f5d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>