Back to index

salome-kernel  6.5.0
__init__.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # This library is free software; you can redistribute it and/or
00005 # modify it under the terms of the GNU Lesser General Public
00006 # License as published by the Free Software Foundation; either
00007 # version 2.1 of the License.
00008 #
00009 # This library is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 # Lesser General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU Lesser General Public
00015 # License along with this library; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00017 #
00018 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00019 #
00020 
00021 __all__ = [ "deprecation", "logger", "termcolor", "logconfig" ]
00022 
00023 # WARN: This file SHOULD NOT import salome stuff so that modules of the
00024 # package could be used outside of a SALOME session context when
00025 # possible. For example logger.py, datamodeler.py, enumerate.py,
00026 # diclookup.py, ... does not need a SALOME running application.
00027 
00028 #
00029 # ==============================================================================
00030 # Generic serialization functions for the module datamodeler
00031 # ==============================================================================
00032 #
00033 import pickle
00034 
00035 def serialize(dataToSerialize):
00036     """
00037     Creates a string description of the specified data.
00038     .. attribute:: dataToSerialize
00039     a data object to serialize
00040     """
00041     serialstring = pickle.dumps(dataToSerialize)
00042     return serialstring
00043 
00044 def unserialize(stringToUnserialize):
00045     """
00046     Creates a data object from its string description. The string description
00047     is supposed to be obtained by a call to serialize.
00048     .. attribute:: stringToUnserialize
00049     a string description of the object
00050     """
00051     data = pickle.loads(stringToUnserialize)
00052     return data
00053 
00054 class Callable:
00055     """
00056     This class is used to create class-method (see MetaData)
00057     """
00058     def __init__(self, anycallable):
00059         self.__call__ = anycallable
00060 #
00061 # ==============================================================================
00062 # Basic use cases and unit tests
00063 # ==============================================================================
00064 #
00065 
00066 def TEST_serialization():
00067     from testdata import TestData
00068     ref_name = "my study case"
00069     studyData = TestData()
00070     studyData.setName(ref_name)
00071 
00072     print "serialize data ..."
00073     serialString = serialize(studyData)
00074     print "unserialize data ..."
00075     unserialData = unserialize(serialString)
00076 
00077     res_name = unserialData.getName()
00078     print res_name
00079     if ( ref_name != res_name ):
00080         return False
00081 
00082     unserialData.log()
00083     unserialData.setName("an other name")
00084 
00085     return True
00086 
00087 if __name__ == "__main__":
00088     import unittester
00089     unittester.run("salome/kernel/__init__","TEST_serialization")