Properties and Objects...

George V. Neville-Neil gnn at neville-neil.com
Sat Sep 22 14:06:38 EDT 2007


I have been reading the mailing list and I am unfortunately going to
open up discussion I've seen twice in the last two years but which
still bugs me, so please accept my apologies in advance.

I have been working on a set of modules that make objects which look
like network packets (http://pcs.sf.net) in that you can set up a
class, say ipv4, which when instantiated has the following properties:

>>> from pcs.packets.ipv4 import *
>>> ip = ipv4()
>>> ip.bytes
'@\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>> ip.ttl
64
>>> ip.ttl=32
>>> ip.bytes
'@\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>> 

Note that setting the ttl in the packet changes the underlying bytes.
Doing this makes writing network tests, and other networking code,
extremely easy.

This is done by having Field classes which are collected into a list
in a Packet class (the layout) and by doing special setattr/getattr
calls.

I have been trying to switch this over to using properties, which seem
at first glance to be cleaner, but which I am having a lot of problems
with.  In particular I want to add a property to an object, not a
class.  The field list in a class is actually relatively static but I
wind up with ugly code like:

class ethernet(pcs.Packet):
    """Ethernet"""
    __layout__ = pcs.Layout()
    _map = ethernet_map.map
    
    src = pcs.StringField("src", 48)
    dst = pcs.StringField("dst", 48)
    type = pcs.Field("type", 16)

    def __init__(self, bytes = None, timestamp = None):
        """initialize an ethernet packet"""

        src = pcs.StringField("src", 48)
        dst = pcs.StringField("dst", 48)
        type = pcs.Field("type", 16)

        pcs.Packet.__init__(self, [dst, src, type], bytes = bytes)
        self.description = inspect.getdoc(self)

and assigning the properties at class time means that it's hard to
have variations, packets with the same name but with slightly varying
field lists.

So, is there a way to assign a property to an object, like this:

    def __init__(....
         self.src = property()

or something like that?

And, failing that, is there a clean way to modify the class in it's
__init__() method so I don't have to call the Field objects twice,
once for the property effect and once to put into the list in the base
Packet class?

With my somewhat convoluted setattr/getattr implementation the
Ethernet class looked like this:

class ethernet(pcs.Packet):
    """Ethernet"""
    __layout__ = pcs.Layout()
    _map = ethernet_map.map
    
    def __init__(self, bytes = None, timestamp = None):
        """initialize an ethernet packet"""

        src = pcs.StringField("src", 48)
        dst = pcs.StringField("dst", 48)
        type = pcs.Field("type", 16)

        pcs.Packet.__init__(self, [dst, src, type], bytes = bytes)
        self.description = inspect.getdoc(self)

which was much more straightforward for someone to understand and to
code.  Since one of the project goals is to have the creation of new
packets classes be as easy as possible I would like to be able to do
something closer to the above.

Best,
George



More information about the Python-list mailing list