[Python-ideas] @classproperty, @abc.abstractclasspropery, etc.

K. Richard Pixley rich at noir.com
Mon Jan 3 23:06:35 CET 2011


On 20110103 13:22, Dirkjan Ochtman wrote:
> On Mon, Jan 3, 2011 at 22:09, K. Richard Pixley<rich at noir.com>  wrote:
>> I think the meanings of the new ones are pretty straightforward, but in case they are not...
>>
>> @staticproperty - like @property only without an implicit first argument.  Allows the property to be called directly from the class without requiring a throw-away instance.
>>
>> @classproperty - like @property, only the implicit first argument to the method is the class.  Allows the property to be called directly from the class without requiring a throw-away instance.
>>
>> @abc.abstractattribute - a simple, non-callable variable that must be overridden in subclasses
>>
>> @abc.abstractstaticproperty - like @abc.abstractproperty only for @staticproperty
>>
>> @abc.abstractclassproperty - like @abc.abstractproperty only for @classproperty
> Do you have actual use cases for these?
Yes.  Here's a toy example for abstractclassproperty:

class InstanceKeeper(object):
     __metaclass__ = abc.ABCMeta

     @abc.abstractclassproperty
     def all_instances(cls):
         raise NotImplementedError

class InstancesByList(InstanceKeeper):
     instances = []

     @classproperty
     def all_instances(cls):
         return cls.instances

class InstancesByDict(InstanceKeeper):
     instances = {}

     @classproperty
     def all_instances(cls):
         return list(cls.instances)

class WhateversByList(InstancesByList):
     instances = []

     ...

class OthersByList(InstancesByList):
     instances = []

     ...

class StillMoreByDict(InstancesByDict):
     instances = {}

     ...

class MoreAgainByDict(InstancesByDict):
     instances = {}

     ...

I'm working on a library for reading and writing ELF format object 
files.  I have a bunch of classes representing various structs.  And the 
structs have, or point to, other structs.  I'm using different 
subclasses to describe different byte ordering, (endianness), and word 
size, (32 vs 64 bit).  Here are examples for the others.

class Codable(object):
     __metaclass__ = abc.ABCMeta
     @abc.abstractattribute
     coder = None

     @classproperty
     def size(cls):
         return cls.coder.size

class FileHeader(Codable):
     __metaclass__ = abc.ABCMeta

     @abc.abstractattribute
     sectionHeaderClass = None
     """
     Used to create new instances.
     """"

     sectionHeader = None

     @abc.abstractstaticproperty
     def word_size():
         raise NotImplementedError

     def __new__(...):
         """
         factory function reading the first few bytes
         and returning an instance of one of the subclasses
         """
         ...

     def __init__(self, ...):
         ...
         self.sectionHeader = self.sectionHeaderClass(...)

class Bit64(object):
     @staticproperty
     def word_size():
         return 64

class Bit32(object):
     @staticproperty
     def word_size():
         return 32

class FileHeader64l(FileHeader, Bit64):
     coder = struct.Struct(...)
     sectionHeaderClass = SectionHeader64l

class FileHeader64b(FileHeader, Bit64):
     coder = struct.Struct(...)
     sectionHeaderClass = SectionHeader64b

class FileHeader32l(FileHeader, Bit32):
     coder = struct.Struct(...)
     sectionHeaderClass = SectionHeader32l

class FileHeader32b(FileHeader, Bit32):
     coder = struct.Struct(...)
     sectionHeaderClass = SectionHeader32b

class SectionHeader(Codable):
     __metaclass__ = ABCMeta

     @abc.abstractattribute
     subsectionHeaderClass = None

    ...

class SectionHeader64l(SectionHeader, Bit64):
     coder = struct.Struct(...)

     ....


--rich



More information about the Python-ideas mailing list