Posts about programming (old posts, page 31)

2010-03-24 04:53

rst2pdf 0.14 released!

It's my pleasure to announce that I just uploaded rst2pdf 0.14 to the site at

Rst2pdf is a program and a library to convert restructured text directly into PDF using Reportlab.

It supports True Type and Type 1 font embedding, most raster and vector image formats, source code highlighting, arbitrary text frames in a page, cascading stylesheets, the full restructured text syntax and much, much more.

It also includes a sphinx extension so you can use it to generate PDFs from documents built with Sphinx.

In case of problems, please report them in the Issue tracker ( or the mailing list (

This release fixes several bugs and adds some minor features compared to 0.13.2. Here are some of the changes:

  • Fixed Issue 197: Table borders were confusing.
  • Fixed Issue 297: styles from default.json leaked onto other syntax highlighting stylesheets.
  • Fixed Issue 295: keyword replacement in headers/footers didn't work if ###Page### and others was inside a table.
  • New feature: oddeven directive to display alternative content on odd/even pages (good for headers/footers!)
  • Switched all stylesheets to more readable RSON format.
  • Fixed Issue 294: Images were deformed when only height was specified.
  • Fixed Issue 293: Accept left/center/right as alignments in stylesheets.
  • Fixed Issue 292: separate style for line numbers in codeblocks
  • Fixed Issue 291: support class directive for codeblocks
  • Fixed Issue 104: total number of pages in header/footer works in all cases now.
  • Fixed Issue 168: linenos and linenothreshold options in Sphinx now work correctly.
  • Fixed regression in 0.12 (interaction between rst2pdf and sphinx math)
  • Documented extensions in the manual
  • Better styling of bullets/items (Issue 289)
  • Fixed Issue 290: don't fail on broken images
  • Better font finding in windows (patch by techtonik, Issue 282).
  • Fixed Issue 166: Implemented Sphinx's hlist (horizontal lists)
  • Fixed Issue 284: Implemented production lists for sphinx
  • Fixed Issue 165: Definition lists not properly indented inside admonitions or tables.
  • SVG Images work inline when using the inkscape extension.
  • Fixed Issue 268: TOCs shifted to the left on RL 2.4
  • Fixed Issue 281: sphinx test automation was broken
  • Fixed Issue 280: wrong page templates used in sphinx


2010-03-19 14:34

If it's worth doing, it's worth doing right.

Yesterday in the PyAr mailing list a "silly" subject appeared: how would you translate spanish to rosarino?

For those reading in english: think of rosarino as a sort of pig latin, where the tonic vowel X is replaced with XgasX, thus "rosario" -> "rosagasario".

In english this would be impossible, but spanish is a pretty regular language, and a written word has enough information to know how to pronounce it, including the location of the tonic vowel, so this is possible to do.

Here is the thread.

It's looong but, final outcome, since I am a nerd, and a programmerm and programmers program, I wrote it.

What surprised me is that as soon as I started doing it, this throwaway program, completely useless...I did it cleanly.

  • I used doctrings.
  • I used doctests.
  • I was careful with unicode.
  • Comments are adequate
  • Factoring into functions is correct

A year ago I wouldn't have done that. I think I am finishing a stage in my (slow, stumbling) evolution as a programmer, and am coding better than before.

I had a tendency to, since python lets you write fast, write fast and dirty. Or slow and clean. Now I can code fast and clean, or at least cleaner.

BTW: this would be an excellent exercise for "junior" programmers!

  • It involves string manipulation which may (or may not) be handled with regexps.
  • Using tests is very quickly rewarding
  • Makes you "think unicode"
  • The algorithm itself is not complicated, but tricky.

BTW: here is the (maybe stupidly overthought) program,

# -*- coding: utf-8 -*-

Éste es el módulo gasó.

Éste módulo provee la función gasear. Por ejemplo:

>>> gasear(u'rosarino')

import unicodedata
import re

def gas(letra):
    '''dada una letra X devuelve XgasX
    excepto si X es una vocal acentuada, en cuyo caso devuelve
    la primera X sin acento

    >>> gas(u'a')

    >>> gas (u'\xf3')

    return u'%sgas%s'%(unicodedata.normalize('NFKD', letra).encode('ASCII', 'ignore'), letra)

def umuda(palabra):
    Si una palabra no tiene "!":
        Reemplaza las u mudas de la palabra por !

    Si la palabra tiene "!":
        Reemplaza las "!" por u

    >>> umuda (u'queso')

    >>> umuda (u'q!eso')

    >>> umuda (u'cuis')


    if '!' in palabra:
        return palabra.replace('!', 'u')
    if'([qg])u([ei])', palabra):
        return re.sub('([qg])u([ei])', u'\\1!\\2', palabra)
    return palabra

def es_diptongo(par):
    '''Dado un par de letras te dice si es un diptongo o no

    >>> es_diptongo(u'ui')

    >>> es_diptongo(u'pa')

    >>> es_diptongo(u'ae')

    >>> es_diptongo(u'ai')

    >>> es_diptongo(u'a')

    >>> es_diptongo(u'cuis')


    if len(par) != 2:
        return False

    if (par[0] in 'aeiou' and par[1] in 'iu') or \
    (par[1] in 'aeiou' and par[0] in 'iu'):
        return True
    return False

def elegir_tonica(par):
    '''Dado un par de vocales que forman diptongo, decidir cual de las
    dos es la tónica.

    >>> elegir_tonica(u'ai')

    >>> elegir_tonica(u'ui')
    if par[0] in 'aeo':
        return 0
    return 1

def gasear(palabra):
    Convierte una palabra de castellano a rosarigasino.

    >>> gasear(u'rosarino')

    >>> gasear(u'pas\xe1')

    Los diptongos son un problema a veces:

    >>> gasear(u'cuis')

    >>> gasear(u'caigo')

    Los adverbios son especiales para el castellano pero no
    para el rosarino!

    >>> gasear(u'especialmente')

    #from pudb import set_trace; set_trace()

    # Primero el caso obvio: acentos.
    # Lo resolvemos con una regexp

        return re.sub(u'([\xe1\xe9\xed\xf3\xfa])',lambda x: gas(,palabra,1)

    # Siguiente problema: u muda
    # Reemplazamos gui gue qui que por g!i g!e q!i q!e
    # y lo deshacemos antes de salir

    # Que hacemos? Vemos en qué termina

    if palabra[-1] in 'nsaeiou':
        # Palabra grave, acento en la penúltima vocal
        # Posición de la penúltima vocal:
        # Palabra aguda, acento en la última vocal
        # Posición de la última vocal:

    # Pero que pasa si esa vocal es parte de un diptongo?

    if es_diptongo(palabra[pos-1:pos+1]):
        pos += elegir_tonica(palabra[pos-1:pos+1])-1
    elif es_diptongo(palabra[pos:pos+2]):
        pos += elegir_tonica(palabra[pos:pos+2])

    return umuda(palabra[:pos]+gas(palabra[pos])+palabra[pos+1:])

if __name__ == "__main__":
    import doctest

2010-03-15 11:14

rst2pdf 0.13 released!

I've just uploaded the 0.13 version of rst2pdf, a tool to convert reStructured text to PDF using Reportlab to

rst2pdf supports the full reSt syntax, works as a sphinx extension, and has many extras like limited support for TeX-less math, SVG images, embedding fragments from PDF documents, True Type font embedding, and much more.

This is a major version, and has lots of improvements over 0.12.3, including but not limited to:

  • New TOC code (supports dots between title and page number)
  • New extension framework
  • New preprocessor extension
  • New vectorpdf extension
  • Support for nested stylesheets
  • New headerSeparator/footerSeparator stylesheet options
  • Foreground image support (useful for watermarks)
  • Support transparency (alpha channel) when specifying colors
  • Inkscape extension for much better SVG support
  • Ability to show total page count in header/footer
  • New RSON format for stylesheets (JSON superset)
  • Fixed Issue 267: Support :align: in figures
  • Fixed Issue 174 regression (Indented lines in line blocks)
  • Fixed Issue 276: Load stylesheets from strings
  • Fixed Issue 275: Extra space before lineblocks
  • Fixed Issue 262: Full support for Reportlab 2.4
  • Fixed Issue 264: Splitting error in some documents
  • Fixed Issue 261: Assert error with wordaxe
  • Fixed Issue 251: added support for rst2pdf extensions when using sphinx
  • Fixed Issue 256: ugly crash when using SVG images without SVG support
  • Fixed Issue 257: support aafigure when using sphinx/pdfbuilder
  • Initial support for graphviz extension in pdfbuilder
  • Fixed Issue 249: Images distorted when specifiying width and height
  • Fixed Issue 252: math directive conflicted with sphinx
  • Fixed Issue 224: Tables can be left/center/right aligned in the page.
  • Fixed Issue 243: Wrong spacing for second paragraphs in bullet lists.
  • Big refactoring of the code.
  • Support for Python 2.4
  • Fully reworked test suite, continuous integration site.
  • Optionally use SWFtools for PDF images
  • Fixed Issue 231 (Smarter TTF autoembed)
  • Fixed Issue 232 (HTML tags in title metadata)
  • Fixed Issue 247 (printing stylesheet)

2010-03-11 15:09

Finding a programmer that can program.

If you haven't read Jeff Atwood's Why Can't Programmers.. Program? go ahead, then come back.

Now, are you scared enough? Don't be, the problem there is with the hiring process.

Yes, there are lots of people who show up for programming positions and can't program. That's not unusual!

It's related to something I read by Joel Spolsky (amazingly, Jeff Atwood's partner in

Suppose you are a company that tries to hire in the top 1% of programmers, and have an open position.

You get 100 applicants. Of those, 99 can't program. 1 can. You hire him.

Then the company next door needs to do the same thing. They may get 100 applicant. 99 can't program ... and probably 80 of them are the same the previous company rejected before!

So no, hiring the best 1 out of 100 is not a way to get a programmer in the top 1% at all, that's just statistics intuition getting the worse of you.

You don't want to hire in the top 1% of applicants, you want to hire in the top 1% of programmers. Different universes.

These two things are the two sides of the same coin. 99% of applicants are useless, that's why they are applicants, because they can't get a job and they can't get a job because they are useless as programmers.

So, judging programmers by the standard of the applicants you get is like judging quality of a restaurant by licking its dumpster.

But now, having taken care of this, how do you find a programmer that can actually program?

Easy! Find one that has programs he can show you!

I would never hire a programmer that can't show me code. There must be something wrong with him, because programmers write programs.

That's just what we do. If we didn't what kind of programmers would we be?

Let's see some obvious objections to my argument:

  1. He wrote code for his previous employer and can't show it.

    So, he did. What else has he written? Some open source code? Maybe snippets in a blog? Answers in stackoverflow?

    Nothing? He has written nothing he was not paid to write? He is not who I want. He only programs for money, he lacks passion for programming, he doesn't enjoy it. He is probably not very good at it.

  2. He is just finishing college, he has not written much code yet!

    Why? What stopped him? He has been learning to program for years, what has he done with the knowledge he has been receiving? Saving it for his 25th brthday party? He has not practiced his craft? Not the programmer I need.

But having him show you code is not enough, of course. It also has to be good code, if you are serious about hiring excellent programmers.

So here's some bonus criteria:

  1. Check the languages he uses. If he codes COBOL for pleasure, he may or may not be what you want.
  2. Open source == bonus points: it means he is not ashamed of his code, plus it makes his credentials trivial to verify.
  3. If he leads a project with multiple contributors and does a good job he is half way to becoming a programmer/manager, so huge bonus points.
  4. Projects with long commit histories show responsability and a level head.
  5. Development mailing lists let you gauge his personality. Is he abrasive? Is he thin-skinned? Is he annoying?

Then there's the obvious stuff, references from previous employers, interviews, exercises, an such. But those are the least important filters, the most important thing is that he must be able to code. And showing you his code is the way to do it.

2010-03-03 19:28

Hacked on kuatia for a couple of hours...

As mentioned previously, I am hacking a bit on a proof-of-concept word processor. Right now, it's hosted on googlecode and called kuatia.

Now, it is far from being useful for anything, but... it can do nested itemized and bulleted lists.

Here's a screenie of the editor and the PDF output it produces via reStructured Text:


Personally I think that's not too bad.

2010-02-25 11:16

Marave 0.7 released

I just uploaded version 0.7 of Marave, my fullscreen text editor to

Marave is a "relaxing" text editor inspired by ommwriter, DarkRoom and many others. It combines a spartan fullscreen UI with a vanishing UI, which gets out of the way of your text.

It supports syntax highlighting, inine spellchecking, background music, audible keyboard feedback, themes, is extensible via plugins, and much more.

Here's a screenshot:


There are no major new features in 0.7, but there are important internal changes and some major bugs fixed:

  • Fixed bug that broke opening files if you had no spellchecker
  • Implemented basic RTL language support
  • Several other minor fixes
  • Refactored the editor component so it can be reused

2010-02-24 14:13

A teaser for an idea

I have been thinking on what I really really want in a word processor. And then what would it take to create such a thing.

A few minutes of playing have led me the way of this teaser (video here if you can't see it):

Could something come out of it? Who knows.

2010-02-23 14:23

Editor: a better QTextEdit

Writing an editor is reinventing the wheel. I know that. I tell myself Marave is a fine wheel, with distinct features, and I think that is true, but, if you are reinventing the wheel, there's no need to reinvent the axle and the spoke, too.

So, I refactored the stuff that I think a text editor must provide into a nice library, so the next time someone must invent a wheel, he can use Marave's neat spokes and axles:

So, introducing Editor, the most-obviously named class ever! It's a text editing widget for PyQt with extra features, which you can use as a drop-in replacement for a QTextEdit or QPlainTextEdit.

Right now, it lives inside Marave's SVN but it may even move out someday.

Here are its features:

  • Syntax highlighting

    And I don't mean "in theory", like QTextEdit and company do! Editor can highlight a bunch of languages, because it uses GNU source highlight via Lorenzo Bettini's Source Highlight Qt.

  • Spell checking

    If you have PyEnchant installed and the right dictionaries, it will do online spellchecking.

  • Search and Search+Replace widgets

    The Editor class can give you nice widgets for search or search and replace already hooked with the editor widget, so you can add them to your app's UI easily.

  • new/open/save/saveas methods:

    Don't implement opening/saving, etc yourself! That's always the same code!

Hopefully this will be helpful for someone else :-)

2010-02-23 12:03

Preview chapter of "Grok 1.0 Web Development"

The nice fellows at Packt Publishing have sent me a copy of Grok 1.0 Web Development by Carlos de la Guardia to review.

I am reading it and expect to write about it in a few days (BTW: nice so far! My proposed slogan: it's like zope, without the Java [1]), and here is a sample chapter so you can see what it's about. It's easy to read standalone, too:

Chapter 5: Forms

Having had to deal this week with the brokenness that's Django oldforms (legacy site, don't even ask), the Grok way of dealing with forms is refreshing ;-)

[1] Yes, of course I know Zope had no Java in it, it just felt like Java ;-)

2010-02-22 00:54

Are we really this clueless about software costs?

Here's what Ohloh has to say about the cost of developing Marave

Really, Marave is maybe a month of part-time programming. How could that possible be U$S71355, or "1 Person Years"?

Is this garbage the best we have to estimate costs? If that's the case, then whenever you see something about "Open source program X would take Y years and cost Z dollars to write", cut it down by a factor of 10 or more.

Here's what Marave really costed to develop:

  • Nothing.

Ok, here's what it would have costed if I had charged for it:

I am guessing about 100 hours of my time. At my "I hope they pay me this much" rate of U$S 40/hour , that's U$S 4000, which means Ohloh is off by 1600%.

OTOH, for that much freelance work I would not charge you the full rate, I would probably end charging you more like U$S20/hour which would make Ohloh's guess over 3000% too high.

In conclusion: if you like my code (and hey, you can see it for yourself), hire me, I am incredibly cheap, or amazingly fast!

Contents © 2000-2019 Roberto Alsina