Object "dumping"
Derek Thomson
dthomson at users.sf.net
Sun Feb 2 20:47:38 EST 2003
Hi all,
I'm debugging a particularly nasty problem in a moderately complex bit
of Python, and I really need something that I can use to just print out
the state of an object.
I also do a lot of Perl, and that's available in Perl with the
Data::Dumper module.
I know Python has pprint, but that stops when it encounters an object
ie. it only dumps sequences and hashes, which is less than what I want.
As I like to illustrate my examples, here's a simple class in Python:
#!/usr/bin/env python
class RequestHeader:
def __init__(self,
_service_context, _request_id, _response_expected,
_object_key, _operation, _requesting_principal):
self.service_context = _service_context
self.request_id = _request_id
self.response_expected = _response_expected
self.object_key = _object_key
self.operation = _operation
self.requesting_principal = _requesting_principal
return
def get_request_id(self):
return self.request_id
# ... other methods ...
Now, if I attempt to use pprint to "dump" the object to a stream as
follows ...
#!/usr/bin/env python
from requestheader import RequestHeader
import pprint
rh1 = RequestHeader('some context', 13, 1, 'an object key',
'op1name', 'principal1')
rh2 = RequestHeader('some other context', 14, 2, 'different object key',
'opname2', 'principal2')
rh_list = [ rh1, rh2 ]
pprint.pprint(rh_list)
... I get:
[<requestheader.RequestHeader instance at 0x81150d4>,
<requestheader.RequestHeader instance at 0x8127b8c>]
Now, to show you what I actually want, here it is in Perl. First the
class definition:
#!/usr/bin/env perl
use warnings;
use strict;
package RequestHeader;
use English;
# (Perl uses packages for class namespaces)
sub new
{
# Get the constructor arguments
my ($class,
$service_context, $request_id, $response_expected,
$object_key, $operation, $requesting_principal) = @ARG;
# We will represent instances of this class with a hash table
my $self = {
service_context => $service_context,
request_id => $request_id,
response_expected => $response_expected,
object_key => $object_key,
operation => $operation,
requesting_principal => $requesting_principal };
# Bless the reference to the hash so that method calls know which
# class (or package) this object belongs to
bless $self, $class;
# Return the new object reference
return $self;
}
sub get_request_id
{
my ($self) = @ARG;
return $self->{request_id};
}
# ... other methods ...
return 1; # Return module load success
Now, here's the code to "dump" some of those objects ...
#!/usr/bin/env perl
use warnings;
use strict;
use RequestHeader;
use Data::Dumper;
my $rh1 = RequestHeader->new('some context', 13, 1,
'an object key', 'op1name',
'principal2');
my $rh2 = RequestHeader->new('some other context', 14, 2,
'different object key', 'op2name',
'principal2');
my $rh_list = [ $rh1, $rh2 ];
print Dumper($rh_list);
When I run this, I get:
$VAR1 = [
bless( {
'requesting_principal' => 'principal2',
'object_key' => 'an object key',
'service_context' => 'some context',
'response_expected' => 1,
'operation' => 'op1name',
'request_id' => 13
}, 'RequestHeader' ),
bless( {
'requesting_principal' => 'principal2',
'object_key' => 'different object key',
'service_context' => 'some other context',
'response_expected' => 2,
'operation' => 'op2name',
'request_id' => 14
}, 'RequestHeader' )
];
... which is what I need for debugging purposes. Now, I know I can add
methods to the RequestHeader class for printing, but I don't want to do
that for every single class I want to dump. Now, as I'm constantly
harping on, the Python and Perl object models are quite similar, so a
generic "object dumper" should be possible. Does one exist?
Thanks,
Derek.
More information about the Python-list
mailing list