[omniORB] Support for __getitem__ on structs

Diez B. Roggisch deets at web.de
Sat Feb 19 14:04:22 GMT 2005


Hi,

one of the powers of python stems from the ability to create ad-hoc data 
structures by returning tuples. Now when exposing a python-driven service via 
corba, one has to resort to structs, as no such ad-hoc types are available 
(and tinkering with sequences of anys gets tedious). For example, my server 
contains this definition:

 struct Pair {
        string key;
        float value;
    };

Of course in python, this would simply be a tuple:

v = ("foo", 1.34)

Now when I have to create a pair from the above, I simply do this:

pair = Pair(*v)

This is pretty concise. However, to convert from a Pair to my beloved tuple, I 
have to jump through some hoops:

v = pair.key, pair.value

Of course this is a very short example - I have structs with 10 or more 
members. One example is a struct modeled after a time.struct_time.

Now what I did yesterday was to use python's introspection capabilities to 
create a __getitem__-method on the generated Pair-class which looks like 
this:

def __getitem__(self, i, j=None):
      if j is not None:
            return [self[k] for k in xrange(i,j)]
      return getattr(self, ['key', 'value'][i])

The trick is to get the list ['key', 'value'] - I extract it from the 
__init__'s function code. 


With this __getitem__, this line

v = pair.key, pair.value

simply becomes

v = tuple(pair)

As the code generation for python bindings has all necessary informations to 
create such a method, I'd love to see it beeing added. I already tried to 
dive into the source, but failed to get a grasp between the C/C++ part of 
omniORB and python. Any chance this addition could get into ominORB? I don't 
expect any running code being broken - who defines a python magic method name 
in corba asks for trouble anyway....

Rgeards,

Diez B. Roggisch





More information about the omniORB-list mailing list