trying to wrap xmlrpclib into classes

Skip Montanaro skip at pobox.com
Wed Jan 22 15:24:11 EST 2003


    >> As does this.  Do you wrap twodaysago in a DateTime object locally or
    >> just pass it along as a tuple?

    Steven> tuple - is that OK?

Yes, that's fine.  It will emerge as a list on the other end, because
XML-RPC doesn't have the concept of an immutable array.

    Steven> an XMLRPC array is translated in a list of dictionary objects,
    Steven> IIUC, which is a bit awkward to navigate

    Steven> for my application, I prefered to wrap that into a dictionary of
    Steven> tuples, providing easier access to the items in the array.
    Steven> Maybe, after having learned more about the different data types,
    Steven> I might come up with another structure...

I think maybe you're misinterpreting something.  Can you post an example
with output which shows the problem?  When you say "dictionary of tuples" do
you mean a dictionary with tuples as the keys or with tuples as the values?
If you want to use a dictionary with tuples as keys, then you'll have to
stringify those tuples somehow before passing the dict across the wire.
You'd then need to unserialize it at the other end.  In my own application I
do something similar to allow Unicode strings to pass back and forth.
Untested code cobbled from my own app:

    xmlrpc_dispatch = {}
    def xmlrpc_marshal_dict(d):
        newd = {}
        for k in d.keys():
            newk = k
            if not isinstance(k, str):
                # this will only work if you're talking to Python at
                # the other end and repr(k) can be reparsed to
                # reconstitute k!
                newk = repr(k)
            newd[newk] = xmlrpc_marshal(d[k])
        return newd
    xmlrpc_dispatch[dict] = xmlrpc_marshal_dict

    def xmlrpc_marshal(item):
        if type(item) in (float, int, bool, str):
            return item
        try:
            return xmlrpc_dispatch[type(item)](item)
        except KeyError:
            # stringify anything else we don't understand
            return repr(type(item))

Skip





More information about the Python-list mailing list