Re: Dabo in 30 seconds?

2005-08-01 Thread xtian
I think this is the kind of thing that Phillip J Eby's
PythonEggs/setuptools project is supposed to manage - you can declare
your dependencies, and it can manage (to some extent) download and
installation of the correct versions of dependencies, without
clobbering existing package versions.

It's at http://peak.telecommunity.com/DevCenter/PythonEggs

I haven't heard much about it recently, but it looks brilliant.

xtian

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: recommended way of generating HTML from Python

2005-02-20 Thread xtian
Stan (part of nevow, which is part of twisted) is a nice python syntax
for building HTML - I like the use of () and [] to separate attributes
from sub-elements.

For example:

class Greeter(rend.Page):
def greet(self, context, data):
return random.choice([Hello, Greetings, Hi]),  , data

docFactory = loaders.stan(
tags.html[
tags.head[ tags.title[ Greetings! ]],
tags.body[
tags.h1(style=font-size: large)[ Now I will greet you: ],
greet
]
])

(From http://nevow.com/releases/0.3.0/nevow-intro.html)

I don't know how detachable it is from the rest of nevow. I'd assume it
wouldn't be too difficult to implement in a standalone fashion.

xtian

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Multiple initialization methods?

2005-02-16 Thread xtian
Several people have told you about dispatching to the different methods
based on the different parameters. Another technique is to have the
different initialisation methods be static or class methods. For
example, the python dictionary type can be instantiated in a variety of
ways:

dict(a=1, b=2, c=3) - {'a': 1, 'c': 3, 'b': 2}

dict([('a',1), ('b',2), ('c',3)])  - {'a': 1, 'c': 3, 'b': 2}

These are examples of dispatching to different initialisers based on
the passed parameters, but there's also the fromkeys() class method,
which accepts a list of keys and will return a new dictionary with the
elements of the list as keys:

dict.fromkeys(['a','b','c']) - {'a': None, 'c': None, 'b': None}

This could be implemented as follows:

class dict(object):
.   def __init__(self, *args, **kws):
.   # standard initialisation...
.
.   def fromkeys(cls, keys):
.   # transform the list of keys into a list of (key, value) pairs,
.   # then delegate to the standard initialiser
.   return cls([(k, None) for k in keys])
.   # or:
.   result = cls()
.   for k in keys:
.   result[k] = None
.   return result
.   fromkeys = classmethod(fromkeys)

(In 2.4, this could use decorators and generator comprehensions
instead.)

I often use this (even in languages like Java which support
overloading) when instantiating domain objects that might be retrieved
in many different ways, for example getting an instance of a user by
database id or by login details:

u = User.getById(1234)
u = User.getByLogin(username, password)

Cheers,
xtian

-- 
http://mail.python.org/mailman/listinfo/python-list