473,240 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,240 software developers and data experts.

Properties and Objects...

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\x0 0\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
Sep 22 '07 #1
1 1206
George V. Neville-Neil wrote:
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.
You can't. The underlying mechanism (descriptors) works on the class level.
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)
From the piece of code you present I would guess that the above class
attributes are superfluous. Or doesn't pcs.Packet.__init__() do the

self.src = ...

thingy?
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.
What's so hard about subclassing?
So, is there a way to assign a property to an object, like this:

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

or something like that?
You can make a property that delegates its behaviour, e. g:

import new

def get_src_one(self):
return 42

class A(object):
src = property(lambda self: self.get_src())
def __init__(self, get):
self.get_src = new.instancemethod(get, self)

a = A(get_src_one)
print a.src

But -- ceterum censeo -- subclassing is probably better, cleaner and
faster. Then instead of calling the class directly make a factory function
that picks the appropriate subclass.

Peter
Sep 24 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ytrewq | last post by:
Should dynamic ("expando") properties be restricted to native and user-defined objects? Or should host objects - such as references to the browser or a plug-in or to the document and its elements -...
4
by: orekinbck | last post by:
Hi There I am learning about reflection so apologies if this post is vague ... I am doing some unit testing and have written a function that accepts two objects and tests to see if their...
1
by: Ben | last post by:
Hi There I am learning about reflection so apologies if this post is vague ... I am doing some unit testing and have written a function that accepts two objects and tests to see if their...
8
by: Joel Reinford | last post by:
I would like to build a class that has properties which can be accessed by string names or index numbers in the form of MyClass.Item("LastName"). The string names or item index values would be...
12
by: Perre Van Wilrijk | last post by:
Hi there, When I started using VB6, I used to write classes with properties and functions as following ... Private lngf1 As Long Private strf2 As String Public Property Get f1() As Long...
0
by: george_Martinho | last post by:
It seems that the ASP.NET Microsoft team didn't think about this!! The profilemanager class has the following methods: - DeleteInactiveProfiles. Enables you to delete all profiles older than a...
0
by: Giorgio | last post by:
It seems that the ASP.NET Microsoft team didn't think about this!! The profilemanager class has the following methods: - DeleteInactiveProfiles. Enables you to delete all profiles older...
5
by: Simon | last post by:
Hi all, We have an ASP.NET 1.1 application running on IIS6 on Server 2003. Most of the base objects we are using in this application are taken from a windows application also written by us. We...
3
by: cwertman | last post by:
I have a question regarding dynamic properties. I have an Object say Account --Id --Prefix --Fname --Lname --Suffix
17
by: David C. Ullrich | last post by:
Having a hard time phrasing this in the form of a question... The other day I saw a thread where someone asked about overrideable properties and nobody offered the advice that properties are...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.