A couple of OOPython questions

A

Alexy Khrabrov

Reading python books feels a bit like reading Perl books -- although
OO is there, some fundamental questions are not illuminated, and then
OO comes after scripting or text, as in the Cookbook! E.g., a = b is
a deep copy. How do you do shallow copy? What are the equivalent of
copy constructors?

Here's another OO question for some real-life application, not that
puny regex fun (or was that perl? :)... I've developed a CORBA server
for structural protein data, and made a simple python client for it
with omniORBpy. My server wraps a huge DB2 database into a series of
accessors, each returning rows from the corresponding table. Let's
say I just want to view the rows. As I came to expect, the thing
(perl or python) will unravel and pretty-print any hierarchy of
dictionaries or lists. [I can't help it, but perl books are shorter
since hash is a better term than a dictionary! Just like with graphs,
where node saves you space over vertex.]

OK, now I go to my python prompt and I want to inspect my objects as
returned by the CORBA server. One of them represents an atom site. I
merrily type just its name to see the great unknown unwrap before my
eyes. Here:
<MMS.StructConf instance at 0x8cb1b44>

Hmm. That's not what I hoped for. Some fragments of code I saw run
through my mind and I type in,
['_NP_RepositoryId', '__doc__', '__init__', '__module__', 'beg_auth',
'beg_label', 'conf_type', 'details', 'end_auth', 'end_label', 'id']

Good, we're getting somewhere. I also googled out vars(a), which
seems to be the same as a.__dict__ (are they?... why is __dict__ not
shown above then? 2.2...)

Printing out gazillion fields by hand for each class is silly, they
only wrap strings and numbers and other objects of that nature. I am
ready to dive in a recursive descent. No such luck -- whoever created
CORBA bindings for python didn't make those objects dictionary-like!
I can't get members by name, although they _are_ in some __dict__,
useless apparently, until I supply __getitem__!

Is there an easy way to write something _once_, general enough, to
print out a hierarchy of nested objects which just don't have
__getitem__, although every field is either a string or a number or
another object?

Cheers,
Alexy Khrabrov
 
H

Hans Nowak

Alexy said:
Reading python books feels a bit like reading Perl books -- although
OO is there, some fundamental questions are not illuminated, and then
OO comes after scripting or text, as in the Cookbook! E.g., a = b is
a deep copy. How do you do shallow copy? What are the equivalent of
copy constructors?

"a = b" is not a copy at all. Well, I suppose you could say it copies a
reference, but that's about it. Assignment never copies objects, whether deep
or shallow.

For "real" copying, use the copy module, or the .copy() method if available.
 
D

David C. Fox

Alexy said:
OK, now I go to my python prompt and I want to inspect my objects as
returned by the CORBA server. One of them represents an atom site. I
merrily type just its name to see the great unknown unwrap before my
eyes. Here:


<MMS.StructConf instance at 0x8cb1b44>

Hmm. That's not what I hoped for. Some fragments of code I saw run
through my mind and I type in,


['_NP_RepositoryId', '__doc__', '__init__', '__module__', 'beg_auth',
'beg_label', 'conf_type', 'details', 'end_auth', 'end_label', 'id']

Good, we're getting somewhere. I also googled out vars(a), which
seems to be the same as a.__dict__ (are they?... why is __dict__ not
shown above then? 2.2...)

Printing out gazillion fields by hand for each class is silly, they
only wrap strings and numbers and other objects of that nature. I am
ready to dive in a recursive descent. No such luck -- whoever created
CORBA bindings for python didn't make those objects dictionary-like!
I can't get members by name, although they _are_ in some __dict__,
useless apparently, until I supply __getitem__!

Is there an easy way to write something _once_, general enough, to
print out a hierarchy of nested objects which just don't have
__getitem__, although every field is either a string or a number or
another object?

I'm not sure exactly what you are looking for, but here's a first
attempt. Save the following to a Python file called recursive_repr,
import recursive_repr, and try print recursive_repr.pr(x).

import types

def type_or_class(x):
"""returns the type of a variable x, or its class when the latter is
more specific, so that the types of two variables can be compared
"""
t = type(x)
if t is types.InstanceType:
# for old-style classes, __class__ is more specific
return x.__class__
# for built-in types and new-style classes
return t

def name_of_class(x):
t = type_or_class(x)
return "%s.%s" % (t.__module__, t.__name__)

def pr(x):
if hasattr(x, '__dict__'):
# return "class %s: %s" % (name_of_class(x), pr(x.__dict__))
return {'class %s' % name_of_class(x) : pr(x.__dict__)}
elif type(x) is type({}):
d = {}
for key, value in x.items():
d[key] = pr(value)
# return repr(d)
return d
return x
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top