meta-class review
Jorgen Grahn
grahn+nntp at snipabacken.se
Wed Oct 6 07:27:06 EDT 2010
On Wed, 2010-10-06, Ethan Furman wrote:
> MRAB wrote:
>> On 06/10/2010 00:17, Ethan Furman wrote:
>> > [snip]
>> > Any comments appreciated, especially ideas on how to better handle
>> > class- and staticmethods
>> >
>> I think that's a bit of overkill. The problem lies in the printing
>> part, but you're spreading the solution into the rest of the
>> application! (A case of the tail wagging the dog, perhaps? :-))
>>
>> IMHO you should just use a simple function when printing:
>>
>> def dash_zero(x):
>> return str(x) if x else '-'
>>
>> '%-25s: %7s' % ('DPV Failure', dash_zero(counts['D']))
>
> Yes, simple is better than complex, isn't it? :) And certainly a *lot*
> less code!
>
> Thank you for pointing that out -- hopefully my blush of embarassment
> will fade by morning.
IMHO wrapping it in a class made much sense -- I just didn't see why
it exploded with more and more. There are a few classes like that which
I frequently use:
a. statistics counters which are like ints, but can only be incremented
and printed (or placed into SNMP messages, or whatever the system
uses)
b. integers to be printed right-aligned in tables of a certain width,
and as '-' or 'n/a' or '' when they are zero. If they are so
int-like that you can't do (a), then just build them on-the-fly
when you're printing:
f.write('%s: %s\n' % (name, MyFormatted(value)))
Class MyFormatted here is very much like dash_zero above; it
has no methods except __init__ and __str__.
I mostly do this in C++; perhaps it makes more sense in a language with
static typing, overloading and templates.
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
More information about the Python-list
mailing list