Python Barcode Generator I was asked to provide some example code for a job interview and didn't have anything to offer, so wrote the following function.
A barcode generator may seem a bit basic, but there's some logic behind the choice:

It's something I recently needed, so not entirely contrived.
It's simple enough to do in one function, so it can be reviewed easily.
It's complex enough that a new programmer would make a mess of it.
It's visual. You can render the result in a browser.
It's useful, so I could make a little gist out of it.

The barcode is in the Interleaved 2 of 5 Format. It's a simple format, and there's a Wikipedia article that covers it well.
Update: A new version of this code has been posted here. The new code includes many of the suggestions made below.
def render(digits):
    
    '''This function converts its input, a string of decimal digits, into a
    barcode, using the interleaved 2 of 5 format. The input string must not
    contain an odd number of digits. The output is an SVG string.

        import barcode
        svg = barcode.render('0123456789')

    Strings are used to avoid problems with insignificant zeros, and allow
    for iterating over the bits in a byte [for zipping].
    
    # Wikipedia, ITF Format: http://en.wikipedia.org/wiki/Interleaved_2_of_5
    '''
    
    # Encode: Convert the digits into an interleaved string of bits, with a
    # start code of four narrow bars [four zeros].

    bits = '0000'
    bytes = {
        '0': '00110', '5': '10100',
        '1': '10001', '6': '01100',
        '2': '01001', '7': '00011',
        '3': '11000', '8': '10010',
        '4': '00101', '9': '01010'
        }

    for i in range(0, len(digits), 2):

        # get the next two digits and convert them to bytes
        black, white = bytes[digits[i]], bytes[digits[i+1]]
        
        # zip and concat to `bits` 5 pairs of bits from the 2 bytes
        for black, white in zip(black, white): bits += black + white

    # Render: Convert the string of bits into an SVG string and return it.

    svg = '&lt;svg height=&quot;50&quot;&gt;'
    bar = '&lt;rect x=&quot;{0}&quot; y=&quot;0&quot; width=&quot;{1}&quot; height=&quot;50&quot; {2}&gt;&lt;/rect&gt;'
    pos = 5 # the `x` attribute of the next bar [position from the left]

    for i, bit in enumerate(bits):

        width = int(bit) * 2 + 2
        color = '0,0,0' if (i % 2 == 0) else '255,255,255'
        style = 'style=&quot;fill:rgb({0})&quot;'.format(color)
        
        svg += bar.format(pos, width, style)
        pos += width

    return svg + '&lt;/svg&gt;'

Note: This works in both Python 2 and Python 3
The code generally looks pretty good, but I think there are some things you could fix up here.

Don't forget the stop code

You have the start code of 0000 but you've forgotten the stop code which is 100.  When you calculate, you could just tack it onto the end with bits += '100' 

Use SVG &lt;def&gt;s

Since the purpose is to create an SVG barcode, it make sense to tighten the resulting SVG.  You may already know how to use a &lt;def&gt; in SVG and it definitely makes things a little easier to understand here.  The way your code is currently structures, it creates four different styles of &lt;rect&gt; which are all combinations of narrow/wide and black/white.  You could predefine each of those shapes and then simply intantiate them within the body of the svg code.  That would make the definition for svg and bar like this:

svg = '''&lt;svg height="50"&gt;&lt;defs&gt;
&lt;g id="b0"&gt;&lt;rect x="0" y="0" width="2" height="50"/&gt;&lt;/g&gt;
&lt;g id="b1"&gt;&lt;rect x="0" y="0" width="4" height="50"/&gt;&lt;/g&gt;
&lt;/defs&gt;'''
bar = '&lt;use xlink:href="#b{0}" x="{1}" y="0" {2}/&gt;'


Using it would then be 

svg += bar.format(bit, pos, style)


An improvement would be to create both bars and spaces like this:

&lt;g id="b0"&gt;&lt;rect x="0" y="0" width="2" height="50" style="fill:rgb(0,0,0)"/&gt;&lt;/g&gt;
&lt;g id="b1"&gt;&lt;rect x="0" y="0" width="4" height="50" style="fill:rgb(0,0,0)"/&gt;&lt;/g&gt;
&lt;g id="s0"&gt;&lt;rect x="0" y="0" width="2" height="50" style="fill:rgb(255,255,255)"/&gt;&lt;/g&gt;
&lt;g id="s1"&gt;&lt;rect x="0" y="0" width="4" height="50" style="fill:rgb(255,255,255)"/&gt;&lt;/g&gt;


Then your loop could look then like this:

for i, bit in enumerate(bits):
    width = int(bit) * 2 + 2
    svg += bar.format('bs'[i%2], bit, pos)
    pos += width


Although that may look verbose, it actually is over 2k shorter for a 12 digit barcode.

Think carefully about data representation

Your code currently translates digits into a series of '1' and '0' characters and then translates again into SVG rectangles.  Why not eliminate a step? Your code could just as easily translate them in a single operation.

Use list comprehensions instead of for loops

The use of list comprehensions is almost always faster than a for loop in Python, so we use them when we can.  It also tends to make the code shorter.  So for example, we could change your loop to calculate the string of bar code bits to calculate all the black bits and then all the white bits like this:

black = "".join([bytes[i] for i in digits[0::2]])
white = "".join([bytes[i] for i in digits[1::2]])
# shuffle them together
databits = "".join("".join(i) for i in zip(black,white))
# create the full bar code string with start and stop
bits = "".join(['0000',databits,'100'])


Doing this with join also saves time.  Appending strings with += is very slow in Python.

Prefer xrange to range

When you use range, a whole list object is created, using memory.  With xrange, a generator is created instead populated which can save memory.  For any reasonably sized bar code this won't make much difference here, but it's good practice for Python 2.7.  In Python 3, xrange doesn't exist and range creates a generator, so keep that in mind if you change versions.

Don't draw more than you have to

You're drawing one &lt;rect&gt; for every bar or space, but it's really not necessary.  Instead, you could create one larger rectangle that's the "background" space color and then only draw the black bars.