KBD

Keith Devens .com

Friday, September 3, 2010 Flag waving
Although at the beginning the paradigm was worth consideration, now the entire effort in the primeval soup paradigm is self-... – Hubert P. Yockey (Information Theory and Molecular Biology, p336)

Tag: Programming

Children:

Page 1 →

Daily link icon Wednesday, August 25, 2010

  1. blog | Perlgeek.de :: Pascal's Triangle in Perl 6. Funky.

       (0) Tags: [Perl]

Daily link icon Tuesday, July 27, 2010

  1. Daring Fireball: An Improved Liberal, Accurate Regex Pattern for Matching URLs.

       (0) Tags: [Regular expressions]

Daily link icon Wednesday, July 21, 2010

  1. Python SimpleNote api class by Scott Jackson.

       (0) Tags: [Python, SimpleNote]

Clever way to parse an ISO date in Python

This post on StackOverflow has a clever way to parse ISO8601 formatted dates in Python using its datetime module. There are third-party modules for this, but I don't know why parsing at least ISO8601 standard dates isn't built into the datetime module.

Anyway, here's the code:

import re,datetime
s="2008-09-03T20:56:35.450686Z"
d=datetime.datetime(*map(int, re.split('[^\d]', s)))

It'll also work if the timestamp uses a space instead of the 'T'.

Daily link icon Sunday, July 18, 2010

  1. Python metaclasses in depth - Jason in a Nutshell (via).

       (0) Tags: [Python]

Daily link icon Friday, July 16, 2010

Thinking of using Pylons over Django

Thinking of using Pylons over Django.

Fact: My site is fucking simple. I have a little bit of a complicated database schema in the way I do tags, but still... I kept running into things in Django that I either had to patch, work around, or that Django's ORM just wasn't capable of doing. Worse, I ran something that it's capable of doing and it executed unnecessary queries. At every step I kept feeling like I might have to abandon my project because I'd run into some limitation in Django that it was too annoying to work around. I looked into modifying the source but it's doing a lot of metaclass mojo that I really don't want to figure out.

I've heard good things about SQLAlchemy so I'd like to give it a shot instead. I don't need Django's authentication modules, and while its autogeneration of an admin interface is really really nice, I wouldn't really mind writing my own if it gives me more flexibility elsewhere. Admin interfaces are easier than people-facing code anyway.

Lastly, I came across this Django/Pylons comparison that has this to say about Pylons' debug interface:

What i can not leave out of this blog post is the pylons debug screen. Just like django it gives you the ability to see the context and variables of every line in the traceback. It also allows you to type the python code from web browser at ANY point from your traceback.

That's fucking sexy.

Mako is the template engine that's normally used along with Pylons. I'm interested to know how it compares to Django's templates, which are really nice.

Edit: re: admin interfaces, in the comments on this Django vs Pylons comparison some people recommend FormAlchemy for HTML forms generation. If I try Pylons maybe I'll give it a shot.

Daily link icon Wednesday, July 7, 2010

  1. Pylons, Python web framework. Used by reddit!

       (0) Tags: [Pylons]

Daily link icon Tuesday, July 6, 2010

Two problems with Django

Two problems with Django I've run into (there's a third but I can't remember it atm).

1. ManyToManyFields don't work correctly with legacy databases. The correct code for this Djangosnippet, btw, is:

class ManyToManyField(models.ManyToManyField):
    def __init__(self, *args, **kwargs):
        source = kwargs.pop('source_db_column', None)
        reverse = kwargs.pop('reverse_db_column', None)
        if source is not None:
            self._m2m_column_cache = source
        if reverse is not None:
            self._m2m_reverse_column_cache = reverse
        super(ManyToManyField, self).__init__(*args, **kwargs)

This works, but something equivalent should be in core Django. I feel like db_column (I think it's on Field) should be used instead of source_db_column above. As far as I can tell the normal db_column parameter isn't even used on a ManyToManyField but I can't be sure yet because there's so much magic going on. For instance, the fields _m2m_column_cache and _m2m_reverse_column_cache don't even appear anywhere in the Django source because of all the metaclass mojo they're doing.

2. select_related() is ridiculously inefficient. Here's some example code:

entries = WeblogEntry.objects. \
  filter(creation_dt__range=(start,end)). \
  annotate(comment_count=Count('comments')). \
  select_related("tags"). \
  exclude(tags__name='hidden'). \
  order_by('-creation_dt')

for e in entries:
    for t in e.tags.all():
        print t.name,
    print ""
    
for q in connection.queries:
    print q, "\n\n"

A database query is executed for each of those e.tags.all()s, when ideally Django should be smart enough to know the keys that WeblogEntry and 'tags' join on, collect all the keys from the results of the WeblogEntry query, do one query for all the related tags, and then match up a list of matching tags with every WeblogEntry. Maybe I'm missing something.

Edit: ridiculously inefficient = doing nothing at all:

You can refer to any ForeignKey or OneToOneField relation in the list of fields passed to select_related.

So it doesn't work with ManyToManyFields at all Smiley frowning Django should at least throw an exception if you try to use select_related on anything other than a ForeignKey or OneToOneField.

Maybe this is something I can fix one way or the other.

Edit: here's a ticket about this issue

Daily link icon Saturday, July 3, 2010

Understanding imports and PYTHONPATH

Understanding imports and PYTHONPATH. This is helpful since it recommends virtualenv, which I hadn't heard of. I've been wanting to hack on Django a bit to see if maybe there's a straightforward way to fix Django's problem with ManyToManyFields and legacy databases, or at least see how it generates its field names in generated SQL, without having to modify my main django install. I guess virtualenv is one way to do it.

Oh look, Django's docs give advice on how to do this Smiley

Daily link icon Wednesday, June 30, 2010

Django ManyToManyField with a legacy database

Django currently has a limitation when trying to use a ManyToManyField with a legacy database.

class WeblogEntry(models.Model):
    tags = models.ManyToManyField(Tag,db_table="Weblog_Entry_Tags",blank=True,db_column="Entry_Id")
    ...

The db_column field is ignored even though ManyToManyField inherits from Field, which supports db_column. You should be able to specify the names of related fields in addition to the name of the table.

Here are two solutions I've found:

I've tried both bits of code and neither seems to work: Django still looks for a field named Weblog_Entry_Tags.weblogentry_id. Maybe they're not updated for Django 1.2.

This should really be in base Django. There's not much point in letting you specify the name of the table if you can't also specify the names of your join fields.

Redoing my website in Django, part 1

As I'm going back and looking at the PHP code for this site that I wrote years ago, I'm impressed at how much "domain knowledge" is embodied in the code. For instance, the main weblog template on this site doesn't simply print out the last n entries, nor entries for the last n days, but all the entries for the last n days that have entries. So, there will always be, say, 5 days' worth of entries on the main weblog view, but the days don't have to be consecutive, get it?

How do you even express that with a Django model object? >.<

Edit: Hey look at that, there's a dates query method that does what I want. Only downside is that the ending date is set to 00:00:00 time (i.e. the beginning of the day), so if you do a range on it it'll exclude everything for that day (unless it's timestamped at midnight of course). The solution is a little clunky (code below), taken from here. I'm surprised there's nothing built-into Django's query machinery to make this easier.

recent = 5
dates_with_entries = WeblogEntry.objects.dates('creation_dt','day',order='DESC')[:recent]
start = dates_with_entries[len(dates_with_entries)-1]
end = datetime.datetime.combine(dates_with_entries[0], datetime.time.max)
entries = WeblogEntry.objects.filter(creation_dt__range=(start,end)).order_by('-creation_dt')

Edit: Uh oh... Django is unhappy:

Many-to-many fields with intermediate tables cannot be symmetrical.

Tags on my site can have parent and child tags. I wonder if this is a many-to-many relationship that Django can't model Smiley frowning

Daily link icon Monday, June 28, 2010

Confusing Django error

I'm getting a confusing Django error in the admin interface.

Here's some code. (Edit: yes I know my names are bad, still working with what I got from inspectdb atm.) In models.py:

class WeblogEntries(models.Model):
    entry_id = models.IntegerField(primary_key=True, db_column='Entry_Id')
    ...

class WeblogComments(models.Model):
    entry = models.ForeignKey(WeblogEntries)
    entry_id = models.IntegerField(db_column='Entry_Id')
    comment_id = models.IntegerField(primary_key=True, db_column='Comment_Id')
    ...

In my admin.py:

from keithdevensdotcom.kbdcms.models import WeblogEntries, WeblogComments
from django.contrib import admin

admin.site.register(WeblogEntries)
admin.site.register(WeblogComments)

If I comment out entry = models.ForeignKey(WeblogEntries) and go to /admin/kbdcms/weblogcomments/11627/ the admin interface comes up fine. If I leave in the foreign key I get the following error message when going to the admin:

TemplateSyntaxError at /admin/kbdcms/weblogcomments/11627/
Caught TypeError while rendering: coercing to Unicode: need string or buffer, NoneType found
...
Template error

In template /Library/Python/2.6/site-packages/django/contrib/admin/templates/admin/includes/fieldset.html, error at line 18

The thing is, as defined, the model works. I can do a python manage.py shell and play around with it and everything seems correct:

>>> c = WeblogComments.objects.get(comment_id=11627)
>>> c.entry
<WeblogEntries: Nerdblog.com: Client Fragmentation: Apps making software more expensive>
>>> c.entry.weblogcomments_set.all()
[<WeblogComments: Andy Baker|http://www.ixxy.co.uk/|You can write C for Android: http://developer.andr>, <WeblogComments: Keith|http://keithdevens.com/|Neat, I didn't know you could write C on Android, >, <WeblogComments: Andy Baker|http://www.ixxy.co.uk/|Yeah. I'd love to see Python too but even with the>, <WeblogComments: Andy Baker|http://www.ixxy.co.uk/|Actually there's an official Mono port as well: ht>]

This post is the data I'm looking at.

Edit: figured it out. When the admin interface displays for a one-to-many relationship like that, it gives you a dropdown of ALL possible parent entries. Some of my early weblog posts had no title, so my __unicode__ method:

   def __unicode__(self):
        return self.title

threw an exception on those entries. Changed to:

    def __unicode__(self):
        return self.title or "Entry #%d" % self.entry_id

and it worked.

Daily link icon Friday, June 25, 2010

  1. Why Tabs are on Top in Firefox 4 « Alex Faaborg (via).

       (0) Tags: [Firefox]

Daily link icon Tuesday, June 22, 2010

  1. Nerdblog.com: Client Fragmentation: Apps making software more expensive (via):

    So to me, the absolutely brilliant thing Google has done with Android is to fragment client development. (They might not see it this way.)

    With Android, I have to write Java code. And honestly, it was hard enough to make my C++ stuff work in Objective C. If I really want to be portable from desktop to mobile, I need at least three code bases now, and that's ignoring RIM and separate resources for iPad, etc. When you cross language boundaries, there is no #ifdef shared code, there is no "fix this on platform X", there's just hard work.

    Google's approach looks smart for the Web, because it makes browser compatibility look like a cakewalk. In short, client fragmentation makes writing code that runs in the browser a lot more attractive than it was 5 years ago.

    Yet another reason I dislike that Android requires Java. Everything else plays nice with C, Android requires a completely different codebase.

       (4) Tags: [Android, Java]

Daily link icon Friday, June 18, 2010

  1. ADC—Introducing Blocks and Grand Central Dispatch ~

       (0) Tags: [Apple, Programming]

Daily link icon Sunday, June 6, 2010

  1. Rob Pike and Russ Cox on 'Go' at Google I/O ~. I wish Google had made Go the primary language for Android instead of Java. I hate that while client side Java has all but disappeared (particularly including browser applets), and it seems to me that server-side Java has never been particularly attractive but is used less today than in the past (I never hear anything about Struts et al. anymore), Android is the one thing somewhat revitalizing Java.

       (0) Tags: [Google, Java]

Daily link icon Friday, May 28, 2010

  1. Daring Fireball: Duncan Davidson on Android's Virtual Machine Performance as a Competitive Factor Against the iPhone. A few links to read.

       (0) Tags: [Android, iPhone, Java, Objective C]

Daily link icon Monday, May 24, 2010

  1. methinks, My Android Toolbox ~. Jedi in particular seems like a good tool. It's inexcusable for modern programming languages to not have these constructs built-in.

       (0) Tags: [Android, Java]

Daily link icon Friday, May 21, 2010

Android + Java

It pains my soul that Java is the primary language of the Android platform. That is all.

Daily link icon Monday, May 17, 2010

  1. Sequel Pro — MySQL database management app for Mac OS X. I went to use MySQL Workbench and it took me a few minutes before I realized that the release version is 5.1 and the version that lets you actually query a database is 5.2, which is in alpha(?) atm.

       (0) Tags: [MySQL]
  2. Django | Weblog | Django 1.2 released (via). Doh, and I just installed the release candidate yesterday.

       (0) Tags: [Django]

Daily link icon Tuesday, May 11, 2010

  1. Mozilla firms up Firefox 4 plans | News | PC Pro ~. Firefox 4 plans = "make it look and feel like Chrome". Edit: on the other hand, look, HTML5 parser! (via Simon)

       (0) Tags: [Chrome, Firefox]

Daily link icon Wednesday, May 5, 2010

  1. iPad Programming Guide: Introduction

       (0) Tags: [Apple, Programming]

Daily link icon Monday, May 3, 2010

  1. zen-coding ~

    Zen Coding is an editor plugin for high-speed HTML, XML, XSL (or any other structured code format) coding and editing. The core of this plugin is a powerful abbreviation engine which allows you to expand expressions—similar to CSS selectors—into HTML code.

       (2) Tags: [Programming]
  2. antirez: fsync() on a different thread: apparently a useless trick. Some stuff in the comments I'd like to look through.

       (0) Tags: [Programming]

Daily link icon Thursday, April 8, 2010

  1. Django 1.2 gets a 'smart if'. About time.

       (0) Tags: [Django]

Daily link icon Tuesday, March 23, 2010

  1. Datejs - An open-source JavaScript Date Library ~.

       (0) Tags: [Javascript]

Daily link icon Wednesday, July 29, 2009

  1. Django 1.1 release notes. Django 1.1 is out, yay. ~

       (0) Tags: [Django]

Daily link icon Sunday, July 12, 2009

  1. John Resig - HTML 5 Parsing ~:

    What's interesting about this particular implementation is that it's actually an automated conversion of Henri's Java HTML 5 parser to C++. This conversion happens automatically and changes will be pushed upstream to the Mozilla codebase.

    Normally I would balk at the mention of a wholesale, programmatic, conversion of a Java codebase over to C++ but the results have been very surprising: A 3% boost in pageload performance.

    When I finally get my new Django-based site running I'll switch my markup over to HTML5 Smiley

       (0) Tags: [Django, HTML, Mozilla, This website]

Daily link icon Wednesday, July 8, 2009

  1. up and running with cassandra :: snax ~:

    Cassandra is a hybrid non-relational database in the same class as Google's BigTable. It is more featureful than a key/value store like Dynomite, but supports fewer query types than a document store like MongoDB.

    Cassandra was started by Facebook and later transferred to the open-source community. It is an ideal runtime database for web-scale domains like social networks.

    This post is both a tutorial and a "getting started" overview. You will learn about Cassandra's features, data model, API, and operational requirements—everything you need to know to deploy a Cassandra-backed service.

       (0) Tags: [Databases, Twitter]
Page 1 →
September 2010
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
2627282930 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 2 posts

Recent comments XML

new⇒Call a function from a string in Python

or​use:
?!code:language=python
def​fce(arg1=None,arg2=None):
#some​usefu...

Richard: Sep 2, 1:08pm

new⇒Spider solitaire

Been playing 4S Spider for a couple​of years. Only recently did I start​to ...

jimibd: Sep 2, 3:16am

Generated in about 0.192s.

(Used 13 db queries)