Sujet : Re: Python (was Re: I did not inhale)
De : ldo (at) *nospam* nz.invalid (Lawrence D'Oliveiro)
Groupes : comp.unix.shell comp.unix.programmer comp.lang.miscDate : 26. Aug 2024, 22:35:25
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vaises$2k7o6$2@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
User-Agent : Pan/0.160 (Toresk; )
On Mon, 26 Aug 2024 08:33:30 -0700, John Ames wrote:
... the simple fact that Guido & co. made a boneheaded choice like that
is the reason I'll never be able to *respect* it, even when I do find
myself using it.
I restore the redundancy by using “#end” comments. E.g. a seriously
nontrivial case:
def def_struct_class(name, ctname, extra = None) :
# defines a class with attributes that are a straightforward mapping
# of a ctypes struct. Optionally includes extra members from extra
# if specified.
ctstruct = getattr(CAIRO, ctname)
class result_class :
__slots__ = tuple(field[0] for field in ctstruct._fields_) # to forestall typos
def to_cairo(self) :
"returns a Cairo representation of the structure."
result = ctstruct()
for name, cttype in ctstruct._fields_ :
setattr(result, name, getattr(self, name))
#end for
return \
result
#end to_cairo
@classmethod
def from_cairo(celf, r) :
"decodes the Cairo representation of the structure."
result = celf()
for name, cttype in ctstruct._fields_ :
setattr(result, name, getattr(r, name))
#end for
return \
result
#end from_cairo
def __getitem__(self, i) :
"allows the object to be coerced to a tuple."
return \
getattr(self, ctstruct._fields_[i][0])
#end __getitem__
def __repr__(self) :
return \
(
"%s(%s)"
%
(
name,
", ".join
(
"%s = %s" % (field[0], getattr(self, field[0]))
for field in ctstruct._fields_
),
)
)
#end __repr__
#end result_class
#begin def_struct_class
result_class.__name__ = name
result_class.__doc__ = \
(
"representation of a Cairo %s structure. Fields are %s."
"\nCreate by decoding the Cairo form with the from_cairo method;"
" convert an instance to Cairo form with the to_cairo method."
%
(
ctname,
", ".join(f[0] for f in ctstruct._fields_),
)
)
if extra != None :
for attr in dir(extra) :
if not attr.startswith("__") :
setattr(result_class, attr, getattr(extra, attr))
#end if
#end for
#end if
return \
result_class
#end def_struct_class