Eventual Consistency

  • Converting an existing Backbone.js project to Require.js

    published Sun 25 August 2013

    Recently, I've undertaken the task of converting a rather large Backbone.js app to using Require.js. Since the process can get tricky at times, here's a quick recap of what I had to do to get it done.

    0. Why use Require.js in the first place?

    Most projects start small. A couple of script tags that include jQuery (or Zepto, or whatever else you are using), a couple of plug-ins, and your own code. As the project matures, more code is added, and to keep things sane, we divide them into gradually more modular components. We usually end up with a lot of files, each one containing a specific portion of functionality. While this seems easy enough at first, it makes reusing them really hard since they usually depend of each other in all sorts of weird ways.

    Require.js helps us define exactly what our component depends on, almost like "regular" imports in other development environments. It means we can lazily load dependencies as needed, as well as reuse components easily, as dependencies are pulled automatically. In addition, with r.js we can easily combine and minify our app to reduce the amount of script ...

    read more →
  • Automatic Host Discovery with Fabric and Boto

    published Mon 01 July 2013

    Over at Swayy our server infrastructure is hosted on Amazon's AWS.

    We use EC2 instances that can roughly be divided into 3 roles - DB, Web and Worker servers. We generally use Fabric to manage and deploy to these servers, which means we must maintain a roledef for each of our environments. A roledef, in Fabric, is a simple mapping between a role (say "db") and an array of hostnames.

    It's a clever mechanism for deployment, but since it is essentially a directory - it also means that we need to update it when we add or tear down nodes. Even though this is not necessarily a bad idea, and it does follow Python's "Explicit is better than implicit" approach, dealing with a large number of nodes gets tedious.

    While looking for more of a "convention over configuration" approach we came up with a little helper class of our own. It uses the Boto library (which is a Python wrapper around the AWS API) to dynamically fetch the list of EC2 servers and generate a roledef using a set of regular expressions. This is perhaps better explained with an example.

    Here is what our roledefs look like now:

    production_roledef ...
    read more →
  • Getting E-Mail right with Django and SES

    published Sun 05 May 2013

    Sending E-Mail is an important part of almost every web or mobile product. And while it may seem like a trivial thing to do at first, getting it right and finding the optimal setup is often not that easy. Spam filters, bounce rates and throttling are all things we must take into account, and sooner or later they WILL fail us.

    Here's a quick run-down on our current setup at Swayy. While it definitely isn't perfect, it held up pretty well so far, sending tens of thousands of emails, with a relatively high acceptance (and conversion) rate. I'll go over setting up Django with Amazon's Simple Email Service, while covering some best practices and things to avoid.

    Step #1 - setting up the Domain

    Before we dive into code, we first need to prepare our DNS records. There are basically two things that we need to do.

    The first step is to add a DNS SPF record - a TXT record that lets other mail servers know we allow email originating from SES to use our domain name in outgoing mail.

    $ dig swayy.co TXT
    ; <<>> DiG 9.8.3-P1 <<>> swayy.co TXT
    swayy.co.       3600    IN  TXT "v ...
    read more →
  • Avoiding Common Backbone.js Pitfalls

    published Tue 26 March 2013

    With the rise of HTML5 and the huge advancement in Javascript performance and APIs in modern browsers, single page applications are now more popular than ever. With that, several open source frameworks and libraries were born to help us develop these somewhat complex applications.

    Backbone.js, while being one of the more popular choices, is also one of the smallest ones (in both terms of scope and actual byte count). So while Backbone is tremendously helpful, it does leave a lot of grunt work for us, the developers, to do ourselves.

    This sometimes creates a bit of friction when getting started, as we face common problems, expecting Backbone to take care of them for us, when in reality we may leave "holes" or hidden bugs in our app. Here are some common ones, that are easily avoided, once we realize they exist.

    1. Creating memory leaks by not unbinding events

    A common pattern in Backbone.js is creating views that listen on changes in models or collections. This technique is usually intended to allow the view to automatically re-render itself when the underlying data changes. It also means that for large collections we may end up with many views (at ...

    read more →
  • Backbone.js with Django 1.5

    published Mon 28 January 2013

    Backbone.js and other client-side MV* frameworks are a relatively new paradigm in designing and building rich web applications. Since this transition is still ongoing, most serious server-side frameworks are just now catching up to this trend. Django included.

    This is not a critique on Django - most sites and apps are still built in the "traditional" way. Backbone and friends are not a one-size-fits-all solution. They have their respective use cases, but they do introduce a new level of complication that is simply not needed in many real world project.

    That being said, while there are lots of tools around to make the transition to single page apps smoother, a big part of Django is still about rendering templates, and the prevalent serialization format is still HTML.

    These are some lessons I've learned by experience, trying to reduce the friction between the two frameworks.

    Models != Resources

    Backbone.js has two classes for working with data: Models and Collections. Intuitively, this is reminiscent of Django's own Models and QuerySets, but this is generally not an accurate comparison. Backbone relies on RESTful APIs, in which a model is actually a RESTful resource.

    In real life, such resources are rarely easily ...

    read more →
  • Better Python APIs

    published Sun 09 December 2012

    Python comes with a large number of built-in functions, operators and keywords. They make working with data structures and built-in types very easy, but usually when we define our own data types (classes) we also tend to come up with our own ways to manipulate and consume our data.

    One of the nice things in python, is that we don't have to. We can use "underscore methods" to make our classes compatible with the built-in functions and operators.

    This makes our code easier to use and does a better job of hiding our nasty, complex implementation from the user. More importantly, it makes our code more intuitive. This means that in many cases our API will just do what the user expected it to do. I call this Code UX, and we should be working on constantly improving it.

    Here are a few practical examples for doing that:

    Make your code REPL friendly

    Python has an awesome REPL shell, which I use all the time to introspect code and to play around with implementations before finally adding them to my code base. I use BPython or IPython instead of the regular Python shell since they offer many niceties such ...

    read more →
  • Improving Your Python Productivity

    published Sun 02 December 2012

    I've been programming in python for a few years now, and I'm still often amazed by how clear and DRY well written Python code can be. There are many small tips and tricks I've learned over time, mostly by reading the code of popular open source projects, such as Django, Flask, Requests and others.

    Here are a few things I've picked up that are sometimes overlooked, but can really help with everyday work.

    1. Dictionary and Set comprehensions

    Most Python developers know and use list comprehensions. For those of you unfamiliar with the concept, a list comprehension is a shorter, more concise way of creating lists.

    >>> some_list = [1, 2, 3, 4, 5]
    >>> another_list = [ x + 1 for x in some_list ]
    >>> another_list
    [2, 3, 4, 5, 6]

    Since python 3.1 (and also back-ported to Python 2.7), we can now use the same idiom to create sets and dictionaries:

    >>> # Set Comprehensions
    >>> some_list = [1, 2, 3, 4, 5, 2, 5, 1, 4, 8]
    >>> even_set = { x for x in some_list if x % 2 == 0 }
    >>> even_set
    set([8, 2, 4])
    >>> # Dict Comprehensions
    >>> d = { x: x % 2 == 0 for x in range(1, 11) }
    >>> d
    {1: False, 2: True, 3: False ...
    read more →
  • Google Chrome Tips for Web Developers

    published Fri 09 November 2012

    Google Chrome comes packed with an awesome suite of tools to aid in development. They contain countless features and are an awesome asset when used properly. Here's a collection of a few tips I have gathered.

    Break on DOM changes

    Chrome lets you drop into the debugger when a DOM element is changed or removed. To do this, right click the element on the page and select Inspect Element to bring up the Elements panel.

    Highlight the DOM element you want to break on in the panel, right click it and choose Break On... and select the cases in which you would like to set a break point. It's really easy and helps a lot - Great for those "Who is moving my #!@#$@% div?!" moments.

    Chrome DOM Break

    The magic of $0 (Dollar-Zero)

    Sometimes, when fiddling in the console, we want to reference a certain DOM element on the page. Instead of querying for it manually with document.querySelector or similar methods, we can simply use the Elements tab to select it.

    From the Elements tab, highlight the desired element, then open the console tab, and type in $0. It is now referencing the highlighted element and we can operate on it ...

    read more →
  • Structuring Client-side JavaScript code

    published Fri 19 October 2012

    JavaScript is a powerful language, but like any other tool it has its ugly parts and its good parts.

    One of its "features", is that it doesn't really enforce any particular packaging practices. Modules or namespaces are not first class citizens. This means that as developers we are pretty much free to do whatever we want, and with this great power, comes great responsibility.

    This is my set of best practices, detailing how to better organize projects, create structure and enforce design paradigms in a way that makes it easy to read and maintain. It's definitely not the only way to go, and might not fit all use cases, but I found it to be useful in real world scenarios for most of the projects I've done. That being said, I'm sure there is room for improvement, so I'd be more than happy to hear how your way is better and why I'm wrong and stupid.

    Breaking things down into files

    One of the first things I've learned, is that JavaScript makes it really easy to start off with a single file, listening to when the DOM is ready, and create a whole ...

    read more →
  • 5 jQuery tips for intermediates

    published Fri 05 October 2012

    jQuery is a much talked about library. The web is full of tips, guides and tutorials. Being a web developer, I've probably read dozens of them over the years, and it seems that they usually target only the far edges of the jQuery learning curve. It's either a tutorial for those using jQuery for the first time, or a deep dive into jQuery's structure for those competent (and willing) enough to really understand its internals.

    Being somewhere around the middle of that curve, I'm always looking for good resources, containing practical advise I could utilize in my day to day work with the library. This post is here to cater others that find themselves in that same boat.

    It's a collection of tips, tricks, and lessons I've picked up while working on Summer and prior projects. Some are slightly advanced, but don't go as far as discuss the implementation of the library. Just solid tips that helped me be more productive and happy when using the library.

    I have also included a link to a jsFiddle page for each tip, showing a real world example that you can mess around with, and get ...

    read more →

Page 1 / 2 »

Hi, I'm Oz Katz

I am a co-founder and CTO over at Swayy.

I usually write about software development using Python, JavaScript and other awesome, open source tools.

Feel free to reach out on Twitter, or contact me using the links at the bottom of the page.