mixins.py

Go to the documentation of this file.
00001 __copyright__ = """\
00002 (c). Copyright 1990-2008, Vyper Logix Corp., 
00003 
00004                    All Rights Reserved.
00005 
00006 Published under Creative Commons License 
00007 (http://creativecommons.org/licenses/by-nc/3.0/) 
00008 restricted to non-commercial educational use only., 
00009 
00010 See also: http://www.VyperLogix.com and http://www.pypi.info for details.
00011 
00012 THE AUTHOR VYPER LOGIX CORP DISCLAIMS ALL WARRANTIES WITH REGARD TO
00013 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
00014 FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
00015 INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
00016 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
00017 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
00018 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE !
00019 
00020 USE AT YOUR OWN RISK.
00021 """
00022 
00023 from vyperlogix.classes.CooperativeClass import Cooperative
00024 
00025 ##
00026 # 
00027 #     This MixIn provides the tinytree.tree with the ability to render itself as a nested Python dict {} to allow for pickling.
00028 #     
00029 #     The assumption is that all Node classes that use this MixIn will either use the properties_idiom to define properties or
00030 #     implement instance vars to define properties.
00031 #     
00032 #     Also the __init__() method must specify optional params for all of the properties that init the Node which means
00033 #     the Node instance must be capable of being initialized via the individual params rather than only via the __init__()
00034 #     method.
00035 #     
00036 class TinyTreeMixIn(Cooperative):
00037     __avoid__ = ['parent','children']
00038     ##
00039     # 
00040     #         Renders the node as a Python dict {} suitable for pickling.
00041     #         DO NOT USE tail-recursion optimization on this function or bad evil things will happen to the children of each node.
00042     #         
00043     def asPythonDict(self):
00044         node = {}
00045         for key in self.__dict__.keys():
00046             if (key not in self.__avoid__):
00047                 property_name = key.strip('__')
00048                 node[property_name] = self.__dict__[key]
00049         children = []
00050         for aNode in self.children:
00051             children.append(aNode.asPythonDict())
00052         node['children'] = children
00053         node['__module__'] = self.__module__
00054         return node
00055 
00056     ##
00057     # 
00058     #         Renders the tree node from a Python dict {} taken from a pickle.
00059     #         DO NOT USE tail-recursion optimization on this function or bad evil things will happen to the children of each node.
00060     #         
00061     def fromPythonDict(self,aDict):
00062         node = self.__class__()
00063         for key in self.__dict__.keys():
00064             if (key not in self.__avoid__):
00065                 property_name = key.strip('__')
00066                 self.__dict__[key] = aDict[property_name]
00067         if (aDict.has_key('children')):
00068             for aChild in aDict['children']:
00069                 node.addChild(self.fromPythonDict(aChild))
00070         return node
00071     
00072 if (__name__ == '__main__'):
00073     import sys
00074     print >>sys.stdout, __copyright__
00075     print >>sys.stderr, __copyright__
00076 
00077 

© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved; If you reference this document or any part of this document you must use the citation verbatim (including the link) "© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved."

Notice: This source code contained in this document is NOT open source and is NOT being distributed as open source.

122,241 lines of code and growing...