How to use itertools.cycle to set even and odd rows

I find code like this in a lot of web applications:

list_of_x_objects = ['a', 'b', 'c', 'd', 'e']
for i, x in enumerate(list_of_x_objects):
if i % 2: htmlclass = "odd"
else: htmlclass = "even"
print """

  • %s
  • """ % (htmlclass, x)

    Never mind the print statement. That’s just to illustrate the point without having to explain some template syntax.

    The same thing can be expressed with itertools.cycle:

    list_of_x_objects = ['a', 'b', 'c', 'd', 'e']
    for htmlclass, x in zip(itertools.cycle(['odd', 'even']), list_of_x_objects):
    print """

  • %s
  • """ % (htmlclass, x)

    I see several advantages of the second approach:

    • It’s way more flexible. I can easily switch to a style that repeats every three lines (or four, or five…).
    • I don’t create the variable i when all I really want is a class variable that toggles between values.
    • The second approach avoids the modulus operator. Since I hardly ever use the modulus operator, when I do come across it, I always have to take a second and puzzle out what’s happening.

    Notes from Cleveland Ruby meeting on Thursday, Jan 25th

    This post contains some python-related information, I promise.

    Fun time. Corey Haines explained behavior-driven development and showed some examples using RSpec at last night’s Cleveland Ruby meetup.

    As an aside, Corey said “powershell is what the unix command line will be when it grows up” and a thousand angels fell over dead when they heard this blasphemy.

    The story-based tests in RSpec seem downright magic. You can write in an english-y syntax:

    Given a = 1,
    When
    b.foo(a)
    Then
    b should return "Hurray"

    Or something like that.

    I like that RSpec supports a result called “Pending”. This guy writes a good explanation of how it works, and I agree with this remark:

    It’s easy enough to rename a test method so it doesn’t execute, but before RSpec I’ve never worked with one where you can mark it as pending and it then reminds you that you still have work to come back too.

    I figure that it would be straightforward to add this into nose. Maybe raise a special exception called PendingTest that gets caught differently.

    I learned a neat way of using a mock object without having to pass it in as a parameter based on some code I saw last night.

    Corey had a couponcontroller that operated on coupon objects. He made a mock coupon object to use with his tests for his couponcontroller. Then, in his test code, he monkeypatched the coupon module so that when somebody said “give me a coupon” he got a mock coupon instead.

    I spent a few minutes trying something vaguely like that in python. I’m not sure I like it, but it gets the point across.

    I have a file coupon.py:

    # This is coupon.py.

    class Coupon(object):
    "I'm the real coupon"

    def foo(self):
    print "This is the real coupon"
    return "foo"

    And I have a file couponcontroller.py:

    # This is couponcontroller.py.

    from coupon import Coupon

    def couponcontroller():
    c = Coupon()
    return c.foo()

    In my test_couponcontroller.py, I want the couponcontroller to use my mock coupon, not the real one.

    # This is test_couponcontroller.py.

    import couponcontroller

    class mockCoupon(object):
    "I'm not the real coupon."
    def foo(self):
    print "Congratulations. You're using a mock."
    return "foo"

    def setup():
    # Mess with the module.
    couponcontroller.Coupon = mockCoupon

    def test_couponcontroller():
    "couponcontroller should return a string 'foo'"
    assert couponcontroller.couponcontroller() == "foo"

    It seems to work:

    $ nosetests -s test_couponcontroller.py
    couponcontroller should return a string 'foo' ... Congratulations. You're using a mock.
    ok

    ----------------------------------------------------------------------
    Ran 1 test in 0.003s

    OK

    In summary, there’s clearly a lot of smart people in the ruby community, even if they insist on using syntax like

    @adder ||= Adder.new

    How to make sure you write more tests

    I’ve been writing way more tests for my code lately, and they’ve become the backbone of my development style.

    I used to write some web code, then play around with it in the browser and look for something to blow up. Automated testing was usually an afterthought or used for confirmation. Or maybe in some cases I would write tests dilligently at the beginning of a project, but start skipping them as deadlines approached. Following TDD has always felt like sticking to high-school abstinence pledges. I wrote tests because I thought I should do it, not because I wanted to do it.

    But now I’ve found a way to make writing tests central to getting my work done. It’s because of the computer I’m using now. It is such a crappy box that it takes about 5 minutes for firefox to start up. Then each page load takes about another 30 seconds at least. In the time it takes to click through three or four pages using firefox or epiphany, my twill scripts can run through about a hundred pages.

    There’s a scene in Star Wars: a New Hope where Obiwan trains Luke to use the light saber while blindfolded. Well, he’s not blindfolded, really, he’s wearing a helmet with the blast shield down, but the idea is the same. Luke has to use “the force” to feel out where the floating droid is, rather than relying on his vision.

    Anyway, writing web pages with this Compaq Presario 1200 feels kind of like that. It’s too frustrating to check my pages with Firefox. The only way I can make sure that anything really works is to write a test for it.

    PS: I wrote this posting with lynx.

    Possible bug in 1.0.4b3 tag of turbogears

    The /visit/api.py file in the 1.0.4b3 tag of turbogears has this function, starting on line 177:

    def encode_utf8(params):
    '''
    will recursively encode to utf-8 all values in a dictionnary
    '''
    res = dict()
    for k, v in params.items():
    if type(v) is dict:
    res[k] = encode_utf8(v)

    else:
    res[k] = v.encode('utf-8')

    return res

    If you have a query string like ?a=1&a=2, then params has a key u’a’ that points to a list that contains u’1′ and u’2′. And encode isn’t defined for lists, so . . .

    Fortunately, the /visit/api.py file in the branches/1.0 branch already has a fix for this problem, so I ran setup.py develop in my checkout directory and was back in business.

    I lost so much time today figuring this out because I kept looking for the bug in my code, rather than in the framework itself. Also, the code works fine as long as the query string doesn’t have more than one value for the same key.

    While I’m on the soapbox, I really wish that testutil.py would change this function:

    def tearDown(self):
    database.rollback_all()
    for item in self._get_soClasses():
    if isinstance(item, types.TypeType) and issubclass(item,
    sqlobject.SQLObject) and item != sqlobject.SQLObject \
    and item != InheritableSQLObject:
    item.dropTable(ifExists=True)

    to something sort of like this instead:

    def tearDown(self):
    database.rollback_all()
    import copy # Probably don't actually import here, but this is just for illustration.
    x = copy.copy(self.__get_soClasses()) # store a copy of the list.
    x.reverse() # Now reverse it.
    for item in x: # Iterate the reversed copy.
    if isinstance(item, types.TypeType) and issubclass(item,
    sqlobject.SQLObject) and item != sqlobject.SQLObject \
    and item != InheritableSQLObject:
    item.dropTable(ifExists=True)

    The whole point of using self.__get_soClasses is that it looks for a list that defines the order to follow when creating tables. You can define soClasses in your model to make sure that your independent tables are created before your dependent tables.

    Well, when it comes time to destroy all your tables, you should destroy the dependent tables first.

    I posted this about a month ago to the turbogears trunk mailing list already.

    Sidenote — if you’re one of the people that are selflessly donating your time to working on turbogears, please don’t take my rants here personally. I’m really grateful that other people are building tools and giving them away, so that I can make a living.

    Test-driven-development can be labor intensive

    I wanted to add a list of checkboxes to the “create employee” and “edit employee” pages. Each checkbox adds that employee to a group, and each group has certain privileges. Really standard stuff. I wrote the code in my controller and template in about 20 minutes

    Then I played around with the new pages in my browser to check for obvious errors. I created some employees, monkeyed with their groups, then saved them, then opened up the edit screen, verified everything worked right, then monkeyed with them some more, and so forth. That probably took 5 minutes of clicking around lazilly.

    In the past, that’s when I would have committed the code to the repository and moved on to something else. I played with it, it didn’t break, so we’re done. Total dev time: 30 minutes.

    This time, I wrote a series of twill scripts to go through the different combinatorials of users and groups.

    For example, if I have groups A and B, I would really test creating 4 different employees:

    • new user with no group membership
    • new user in group A
    • new user in group B
    • new user in groups A and B

    After each creation, I verify that the page displays the data correctly and I then hit the database and make sure that everything is set correctly.

    For the screen that allows editing employees, the most thorough possible test would take those four new employees and loop until each has been changed to every other possible configuration.

    This took about another two hours by the time it was done. The next time I have to write code like this, it will be much faster because I figured out how to write code that yields tests iteratively. Using TDD, total dev time hit about 2.5 hours.

    So, in this particular case, is it worth it?

    Here’s the reasons why I would say that it was worth it:

    I’m still learning how to write good tests. Writing thorough tests requires a different mindset, for me anyway. If I wait until I face some really gnarly complex code to write tests, I’m likely to write some crappy incomplete tests. Each test I write makes me faster at writing tests. Also, when I’m regularly writing tests, I write application code with testing in mind. I think more about design and protecting against what could go wrong, rather than just reaching the finish line any way I can.

    And here’s the contrarian view:

    Time is scarce and writing tests takes time. In a time-constrained environment, writing needless tests is as silly as blowing off real work to write blogs.

    I’m not sure which voice in my head I will listen to on this one.

    MVC Blasphemy

    I just put HTML code into my data model. I have a list-of-objects page. Each object is an instance of an object defined in my data model, derived from a row in a database. Each object needs a pretty link drawn that object’s detailed-view page. So I added a property on my object:
    class Message(SQLObject):
    def _get_view(self):
    "Draw a link to the view page for this message."
    return cElementTree.XML("""VIEW""" % self.id)
    # Lots of other stuff snipped out.

    This is now what my kid template looks like:

    MESSAGE STUFF

    I pass in messages and columns; messages is a list of objects and columns is a tuple of strings that map to attributes or properties, like “view”.

    I’m happy with this decision. I know I could have manipulated the messages or created some new classes in my controller, but I couldn’t really see any advantage. This way works.

    I just don’t want anyone else doing this 🙂

    Don’t put parentheses around your assert expressions and the error string!

    I had a bunch of unit tests that were passing even though I knew they should be failing. I traced it down to the fact that I put parentheses around my assert statements because the tests were really really long and I wanted to put the error string on a separate line.

    This is what I spent the last 45 minutes trying to figure out:

    >>> assert 1 == 0, "OH NOES"
    ------------------------------------------------------------
    Traceback (most recent call last):
    File "", line 1, in
    AssertionError: OH NOES

    >>> assert (1 == 0,
    ... "OH NOES")

    >>> assert (1 == 0, "OH NOES")

    >>>

    The assertion doesn’t raise because I suspect that the assert evaluates each element in the tuple separately, and the string returns True.

    And these don’t work, but for different reasons:

    >>> (assert 1 == 0, "OH NOES")
    ------------------------------------------------------------
    File "", line 1
    (assert 1 == 0, "OH NOES")
    ^
    SyntaxError: invalid syntax

    >>> assert 1 == 0,
    ------------------------------------------------------------
    File "", line 1
    assert 1 == 0,
    ^
    SyntaxError: invalid syntax

    Dangit.

    Ubuntu Gutsy twill package breaks the showforms command

    On one computer named snowball, I installed the ubuntu twill package, then tried to test my webapp:

    $ twill-sh -u http://coleridge:8181/login2
    ==> at http://coleridge:8181/login2

    -= Welcome to twill! =-

    current page: http://coleridge:8181/login2
    >> showforms
    current page: http://coleridge:8181/login2
    >> show




    Login 2

    Login 2



    current page: http://coleridge:8181/login2
    >>

    The page clearly contains a form, but twill’s showforms command doesn’t display it. Fortunately, I found this post on the twill mailing list and so I didn’t waste too much time scratching my head. Then on this other box, coleridge, I installed twill using easy_install, and all was well:

    $ twill-sh -u http://localhost:8181/login2
    ==> at http://localhost:8181/login2

    -= Welcome to twill! =-

    current page: http://localhost:8181/login2
    >> showforms

    Form name=login2 (#1)
    ## ## __Name__________________ __Type___ __ID________ __Value__________________
    1 user_name text (None)
    2 passwd password (None)
    3 1 None submit (None)

    current page: http://localhost:8181/login2

    Sidenote: snowball is my new computer. She is a beaut; a 600mhz Compaq Presario with 60mb of ram, and a hard drive that makes clicking sounds when anything happens. I think Clinton was still in office when she was manufactured. I’d like to thank my son for helping decorate her with some very cool Godzilla stickers.

    Photo0250.jpg

    Installing on such an old machine meant I had to monkey a fair amount with some kernel modules, which is always a blast, and I am now much more aware of how much every program really costs in terms of CPU utilization and memory footprint.

    My .screenrc is a testament to obsession over meaningless details

    Most every time I start a new screen session, I set up four windows using particular window names. After several hours of reading the screen man page and trying stuff out, I discovered how to automate the process. I added this section to my .screenrc:

    screen -t ipython 0 /usr/bin/ipython -p matt
    screen -t code 1 # This is where I use vi.
    screen -t psql 2 /usr/bin/psql -U mydbusername mydb
    screen -t irssi 3 /usr/bin/irssi

    You can use the -c option when you start screen to specify a file other than $HOME/.screenrc. So I might write a bunch of different .screenrc config files and then I can choose what ever one I want. I can already imagine a .screenrc customized for doing python work, and another one customized for writing rants, and another one for doing sysadmin work.

    A few other useful screen options:

    • screen -S sessionname will start a screen session and name it sessionname.
    • screen -ls will list all the screen sessions already running. Those session names are helpful in this case.
    • screen -r sessionname will attach a screen session that you specify.

    A few examples of how to use :g in vim

    This vim tip of the day is really good. I use the :g/pattern/d command occasionally, but there is a lot more possible. Here’s the tip:


    VimTip 227: Power of :g
    http://vim.sourceforge.net/tip_view.php?tip_id=

    :g is something very old and which is very powerful. I just wanted to illustrate the use of it
    with some examples. Hope, it will be useful for someone.

    Brief explanation for ":g"
    -------------------------
    Syntax is:
    :[range]:g//[cmd]
    You can think the working as, for the range (default whole file), execute
    the colon command(ex) "cmd" for the lines matching . Also, for all
    lines that matched the pattern, "." is set to that particular line (for
    certain commands if line is not specified "." (current line) is assumed).

    Some examples
    -------------
    Display context (5 lines) for all occurences of a pattern
    :g//z#.5
    :g//z#.5|echo "=========="
    << same as first, but with some beautification >>
    Delete all lines matching a pattern
    :g//d
    Delete all blank lines (just an example for above)
    :g/^\s*$/d
    Double space the file
    :g/^/pu =\"\n\"
    :g/^/pu _
    << the above one also works >>
    Copy all lines matching a pattern to end of file
    :g//t$
    Yank all lines matching a pattern to register 'a'
    0"ay0:g//y A
    Increment the number items from current line to end-of-document by one
    :.,$g/^\d/exe "normal! \"
    Comment (C) lines containing "DEBUG" statements
    g/^\s*DEBUG/exe "norm! I/* \A */\"
    A Reverse lookup for records
    (eg: An address book, with Name on start-of-line and fields after a space)
    :g/?^\w?p "if only name is interested
    :g//ka|?^\w?p|'ap "if name and the lookup-line is interested
    :g//?^\w?|+,/^[^ ]/-1p "if entire record is interested
    Reverse a file (just to show the power of 'g')
    :g/^/m0

    Foot note 1: use :v to negate the search pattern
    Foot note 2: Some explanation of commonly used commands with :g
    :2,8co15 => Copy lines 2 through 8 after line 15
    :4,15t$ => Copy linesa 4 through 15 towards end of document (t == co)
    :-t$ => Copy previous line to end of document
    :m0 => Move current line to the top of the document
    :.,+3m$-1 => Move current line through cur-line+3 to the last but one line
    of the document
    Foot note 3: Commands used with :g are ex commands, so a help search should
    be,
    :help :
    eg. :help :k