serialize.py :  » Database » Gadfly » gadflyZip » gadfly » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » Gadfly 
Gadfly » gadflyZip » gadfly » serialize.py
''' essentially, specialized pickle for this app:

:Author: Aaron Watters
:Maintainers: http://gadfly.sf.net/
:Copyright: Aaron Robert Watters, 1994
:Id: $Id: serialize.py,v 1.4 2002/05/11 02:59:05 richard Exp $:
'''

# TODO need to fix serialization/deserialization of btand and btor
import types

def serialize(ob):
    """ Simple protocol for generating a marshallable ob

        TODO: I'm worried that tuples are special cases here...
    """
    if not isinstance(ob, types.InstanceType):
        # base type
        return ob

    args1 = ob.initargs()
    args1 = tuple(map(serialize, args1))
    args2 = ob.marshaldata()
    return (ob.__class__.__name__, (args1, args2))

def deserialize(description):
    """ Dual of serialize
    """
    # base type
    if not isinstance(description, types.TupleType) or len(description) != 2:
        return description

    # pull out the class name and marshal data
    (name, desc) = description

    # TODO: these doesn't actually appear to be possible
    if name == "tuple":
        # tuple case
        return desc
    if name == "list":
        # list case: map deserialize across desc
        return map(deserialize, desc)

    # all other cases are classes of semantics
    import semantics
    klass = getattr(semantics, name)
    (args1, args2) = desc
    args1 = tuple(map(deserialize, args1))
    ob = apply(klass, args1)
    ob.demarshal(args2)
    return ob

# invariant:
#   deserialize(serialize(ob)) returns semantic copy of ob
#   serialize(ob) is marshallable
# ie,
#   args1 = ob.initargs() # init args
#   args1d = map(serialize, args1) # serialized
#   args2 = ob.marshaldata() # marshalable addl info
#   # assert args1d, args2 are marshallable
#   args1copy = map(deserialize, args1)
#   ob2 = ob.__class__(args1copy)
#   ob2 = ob2.demarshal(args2)
#   # assert ob2 is semantic copy of ob


#
# $Log: serialize.py,v $
# Revision 1.4  2002/05/11 02:59:05  richard
# Added info into module docstrings.
# Fixed docco of kwParsing to reflect new grammar "marshalling".
# Fixed bug in gadfly.open - most likely introduced during sql loading
# re-work (though looking back at the diff from back then, I can't see how it
# wasn't different before, but it musta been ;)
# A buncha new unit test stuff.
#
#
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.