Posts Tagged ‘python’

summing datetime.timedelta objects in Python

Posted in General on February 9th, 2010 by slacy – Be the first to comment

Argh! The following snippet is totally broken:

import datetime 

times = [datetime.timedelta(hours=1), datetime.timedelta(minutes=30)]
print sum(times)
print min(times)
print max(times)

It raises “TypeError: unsupported operand type(s) for +: ‘int’ and ‘datetime.timedelta’”

The thing is, I want this function to be able to sum both a list of floats, integers, or timedelta objects.  Other than doing a bunch of type checking, or writing my own sum method, is there some easy way to do this that I’m missing? I guess I could write:

print datetime.timedelta(milliseconds=sum([d.milliseconds for d in times]))

Yuk.  That’s not really type safe either, since I’d have to check the type of times[0] before I started.

If you program, you should watch this.

Posted in General on January 7th, 2010 by slacy – Be the first to comment

The video linked below sort of blew my mind.

The integration and instant feedback between test & code

The notion of “spec” (not “unittest”)

The spliraling nature of the way he writes the code.

Here’s the blog post with the embedded video, but the embedding size is too small to see what’s going on.

Here’s the direct link to the video on Vimeo, which won’t make too much sense unless you read the blog post first.

Announcing Parents Guild

Posted in General on December 4th, 2009 by slacy – Be the first to comment

Just a quick note to my readers and searchers that we’ve launched our new parenting website, Parents Guild.  We’ve got question & answering, e-mail notifications, tagging, voting, and a bunch of other parenting-specific features in the works.  We’ve got about 50 users so far, which is pretty great considering we haven’t done much marketing. :)   We’re welcoming feedback (use the link on the left hand side of the site, or comment here, or comment on the blog or facebook).

Our goal is to make the site as welcoming and informative to as many people as possible.  That includes Dads, Grandparents, parents of older children, people without children, as well as expecting couples.  Come on by and share ideas & advice!

It’s easy to sign up and ask or answer a question!

We also have a blog and a facebook page, so you can follow what we’re doing on the site.

Resolving circular model imports caused by Haystack & Django

Posted in General on November 19th, 2009 by slacy – Be the first to comment

So, in converting our search to Haystack, I’ve found that some of my standalone maintainence scripts no longer work.  They had an import error trying to import some of the models.

The issue is that I’ve updated my models.py with some Haystack code.  The code at hand is in the save() method of the model object, and basically looks like this:

[... other misc django imports above ... ]
from haystack import site

class MyModel(models.Model):
    [ ... fields of model, etc. ... ]
    def save(self, force_insert=False, force_update=False):
        super(MyModel, self).save(force_insert, force_update)
        if self.is_child_node():
            site.get_index(MyModel).update_object(self.parent)

The problem was that if I had a standalone script that said:

from mysite.models import MyModel

Then I would get a circular import, because the “from haystack import site” line ends up working it’s way over to search_sites.py that says “haystack.autodiscover()” which ends up looking for all search_indexes.py, and one of these says “from myapp.models import MyModel”.  So, there’s your circular reference.

The solution to this problem is to put a haystack import at the top of my standalone script, before I import my model.  Adding “from haystack import site” works, even though the code therein doesn’t actually use haystack.

I think the haystack code here:

/usr/lib/python2.5/site-packages/haystack/__init__.py in autodiscover()
 88         # Step 3: import the app's search_index file. If this has errors we want them
 89         # to bubble up.
---> 90         __import__("%s.search_indexes" % app)
 91
 92 # Make sure the site gets loaded.

Should have a try/except block around it, and then this extra import wouldn’t be necessary. The funny thing is that looking at the source, all the other imports there catch ImportError and don’t pass it back up, but this case does.

I was able to simply debug this in ipython my running “from myapp.models import MyModel” and debugging, per above.  It was fairly clear that it’s a circular import.  I’m surprised (in general) that Python doesn’t handle circular imports any more gracefully than saying “<type ‘exceptions.ImportError’>: cannot import name MyModel”

flymake for emacs + Python

Posted in General on November 6th, 2009 by slacy – Be the first to comment

flymake is an emacs mode that lets you “compile” (or syntax check) your code on the fly. For Python, this means that you can run several syntax checkers, like pep8 or pylint, or pyflakes (or all of the above) while you’re editing your code in realtime. To get this set up in emacs, do the following:

Create ~/bin/pychecker, which will be a simple script calling all your checkers.

#!/bin/bash
pylint --output-format=parseable "$1"
pyflakes "$1"
pep8 --repeat "$1"
true

Second, edit your .emacs and set up flymake to call this script for python files, and add some useful keyboard shortcuts for jumping between errors & warnings:

(require 'flymake)
(defun flymake-pylint-init ()
 (let* ((temp-file (flymake-init-create-temp-buffer-copy
                    'flymake-create-temp-inplace))
        (local-file (file-relative-name
                     temp-file
                     (file-name-directory buffer-file-name))))
   (list "~/bin/pychecker" (list local-file))))

(add-to-list 'flymake-allowed-file-name-masks
             '("\\.py\\'" flymake-pylint-init))

(defun my-python-mode-hook ()
 (interactive)
 (flymake-mode)
 (local-set-key [S-up]
                (lambda ()
                  (interactive)
                  (flymake-goto-prev-error)
                  (message "%s"
                    (flymake-ler-text (caar (flymake-find-err-info
                    flymake-err-info
                    (flymake-current-line-no)))))))
 (local-set-key [S-down]
                (lambda ()
                  (interactive)
                  (flymake-goto-next-error)
                  (message "%s"
                    (flymake-ler-text (caar (flymake-find-err-info
                    flymake-err-info
                    (flymake-current-line-no)))))))
)
(add-hook 'python-mode-hook 'my-python-mode-hook)

Useful keybindings for emacs python-mode

Posted in General on November 4th, 2009 by slacy – Be the first to comment

I wrote up this keybinding the other day at work, and found that I missed it when I went home, so I wrote it again from scratch.

It allows you easily indent or undent the current region using M-left and M-right:

(global-set-key [M-left] '(lambda () (interactive)
  (save-excursion
    (py-shift-region-left (region-beginning) (region-end)))))

(global-set-key [M-right] '(lambda () (interactive)
  (save-excursion
     (py-shift-region-right (region-beginning) (region-end)))))

pprinting the Django context object

Posted in General on October 22nd, 2009 by slacy – Be the first to comment

This was non-obvious to me for quite some time, and I tried several different approaches before settling on this one:

If you would like to pprint your Django Context (or RequestContext) object from within a template itself, there are several approaches you can take.  The easiest way I found is to say:

context['context'] = context

in your view(s) that you want to display, and then in the template, you can say:

{% for c in context %}
<p>{{ c|pprint }}</p>
{% endfor %}

Or something along those lines.

The reason that the simple {{ context|pprint }} doesn’t output what you expect is because of the __repr__() method on the Context() object in Django.  It constructs a big huge string for you, when what you want is to let pprint do all the heavy lifting and indentation.

How to split views.py into a directory (Django, Python)

Posted in General on July 24th, 2009 by slacy – 2 Comments

So, I’ve been doing some Django programming on the side, and the thing that annoys me is that Django forces you to put nearly all your source code into 2 files:  models.py and views.py

Being a reasonable person, I wanted to have a separate file for each view in my system.  In other words, I want to create a views directory, with __init__.py inside, and my view files in there.  Not being a Python expert, I found this harder than expected.  Here’s what you need to do:

  1. Create a views directory.
  2. Split views.py into one file per method in the new views directory.
  3. Edit views/__init__.py and for each view, say “from myview import *”
  4. Use your views as you previously did in urls.py

I’ve heard some rumblings that you could create an __init__.py that did something like go through every file in the current directory and import everything there.  That seems a bit over the top, and I’m happy to manage the imports in __init__.py for now.

I believe a very similar technique should work for Models, but I haven’t tested it.

Perl in place replacement syntax

Posted in General on May 13th, 2009 by slacy – 1 Comment

I always forget how to do this, so I’m writing it down:

perl -pi -e “s/foo/bar/g”

Is there a way to do this in Python and be as concise? That would be really nice, and would eliminate one more place where I end up using Perl and tearing my hair out every time.