The Shoes of the Fisherman's Wife Are Some Jive-Ass Slippers

tpot (at) frungy . org

rss

2003
Months
Dec

Thu, 18 Dec 2003

Internal Company Survey

Please select your job function:

  1. Sales
  2. People Manager
  3. Other
Hmm.

posted at: 09:40 | path: | permanent link to this entry

Wed, 17 Dec 2003

Emulating Container Types

Python comes with a nice mixin class, UserDict.DictMixin for emulating container (dictionary) types. All you have to do is provide __getitem__(), __setitem__(), __delitem__(), and keys() and the mixin class does the rest.

Here's my implementation of a tdbdict from the SWIG bindings for Samba's Trivial Database:

import tdb, os
from UserDict import DictMixin

class tdbdict(DictMixin):
    def __init__(self, name, hash_size = 0, tdb_flags = 0,
                 open_flags = os.O_RDWR | os.O_CREAT, mode = 0600):
        self.tdb = tdb.tdb_open(name, hash_size, tdb_flags, open_flags, mode)

    def __getitem__(self, key):
        result = tdb.tdb_fetch(self.tdb, key)
        if result is None:
            raise KeyError(key)
        return result

    def __setitem__(self, key, value):
        tdb.tdb_store(self.tdb, key, value, 1)

    def __delitem__(self, key):
        tdb.tdb_delete(self.tdb, key)

    def keys(self):
        result = []
        while 1:
            if len(result) == 0:
                k = tdb.tdb_firstkey(self.tdb)
            else:
                k = tdb.tdb_nextkey(self.tdb, k)
            if k == None:
                break
            result.append(k)
        return result
The DictMixin class implements all the other dictionary methods (values(), items(), has_key(), get(), clear(), setdefault(), iterkeys(), itervalues(), iteritems(), pop(), popitem(), copy(), and update()) without any additional effort. posted at: 17:24 | path: /computers/programming | permanent link to this entry

Tue, 16 Dec 2003

Puerile Australianisms

From the Washington Post (warning: irritating registration required):

Patenting Air or Protecting Property?
Information Age Invents a New Problem

By Jonathan Krim
Washington Post Staff Writer
Thursday, December 11, 2003

Universities, corporations and tens of thousands of Web site providers
across the country probably never imagined they would be rooting for
the pornography industry.

...
Ha ha. Almost as funny as "router". posted at: 15:15 | path: | permanent link to this entry

Biculturalism

I enjoy reading Joel Spolsky's Joel on Software column. The latest edition is a review of esr's The Art of UNIX Programming from the point of view of a Windows programmer.

His main point is that it is only cultural differences (i.e GUI vs command line) that separate us as programmers. However he nicely provides a counter example to his other point, that "Raymond all too frequently falls into the trap of disparaging the values of other cultures without considering where they came from". He then exclaims that that "the Unix world is so full of self-righteous cultural superiority, 'advocacy,' and slashdot-karma-whoring sectarianism". Wow.

It's a shame that people emphasise the adversarial nature of Windows and UNIX as the two systems can co-exist quite nicely serving complementary roles within an organisation. Most of this is probably due to Microsoft's continuous output of FUD on the topic which tends to spill over into the mindset of developers on both sides.

Of course it's easy to see that there is bias in both camps, and esr is perhaps not the most restrained of commentators when it comes to pointing out the good and bad points of the two cultures. It's kind of sad to see Joel take esr's UNIX superiority complex at face value though.

posted at: 12:08 | path: /software | permanent link to this entry

Mon, 08 Dec 2003

Some neat Python patterns

The Python Cookbook in the ActiveState Programmer Network has a bunch of neat Python patterns. I've just discovered a whole bunch of them by one guy, Alex Martelli. They have a simple elegance to them that is very characteristic of Python.

Here are my favourites:

Assign and Test

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66061
class DataHolder:
    def __init__(self, value=None): self.value = value
    def set(self, value): self.value = value; return value
    def get(self): return self.value

while data.set(file.readline()):
    process(data.get())

Determining Current Function Name

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66062
import sys
this_function_name = sys._getframe().f_code.co_name
this_line_number = sys._getframe().f_lineno
this_filename = sys._getframe().f_code.co_filename

Multiple Constructors

Finally, here's a a nice pattern from Skip Montanaro for having multiple constructors for a class a la C++:
class foo:
    def __init__(self, **kw):
        if kw.has_key("foo"):
            self.foo_init(kw["foo"])
        if kw.has_key("bar"):
            self.bar_init(kw["foo"])
    def foo_init(foo):
        pass
    def bar_init(bar):
        pass
Go Python! posted at: 13:59 | path: /computers/programming | permanent link to this entry