當前位置: 首頁>>代碼示例>>Python>>正文


Python serializer.Serializer類代碼示例

本文整理匯總了Python中serializer.Serializer的典型用法代碼示例。如果您正苦於以下問題:Python Serializer類的具體用法?Python Serializer怎麽用?Python Serializer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Serializer類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: RPCProxy

class RPCProxy(object):
    
    def __init__(self, uri, method=None, namespaces=None, sid=''):
        self.__serviceURL = uri
        self.__serviceName = method
        self.namespaces = isinstance(namespaces, ClassLoader) and namespaces or \
                              ClassLoader(namespaces or [])
        self._seril = Serializer(self.namespaces)
        self.sid = sid
        self.logger = None
        self.start_call_listener = []
        self.end_call_listener = []
    
    def __call__(self, *args, **kw):
        args = self._seril.serialize((args, kw))
        
        post_data = {"method": self.__serviceName, 
                     'params': args, 
                     'id':'httprpc',
                     'sid':self.sid}
        
        #@todo xx
        for l in self.start_call_listener:
            l(name=self.__serviceName, args=args, kw_args=kw)
        
        rpc_response = self.post_request(self.__serviceURL, post_data)
        try:
            respdata = rpc_response.read()
            ret, e = self._seril.deserialize(respdata)
        except Exception, e:
            raise RPCException("Failed to deserialize response data:%s, exception:%s" % 
                               (respdata, e))
        finally:
開發者ID:dalinhuang,項目名稱:demodemo,代碼行數:33,代碼來源:rpc_over_http.py

示例2: testSimpleSerialization

 def testSimpleSerialization(self):
     m = self.createTestMessage()
     serialized = StringIO(Serializer.serialize(m))
     
     deserialized = Serializer.deserialize(serialized, TestMessage1)
     
     self.assertDictEqual(m.__dict__, deserialized.__dict__)
開發者ID:ptsurko,項目名稱:coursera_cloud,代碼行數:7,代碼來源:serializer_test.py

示例3: RPCStub

class RPCStub(object):
    def __init__(self, uri, stub, namespace=None):
        self.uri = uri
        self.stub = stub
        self.namespace = namespace
        self._seril = Serializer(namespace)
        self.logger = None
        
    def process(self, method, data):
        ret = exception = None
        try:
            args, kw = self._seril.deserialize(data)
            try:
                self.logger and self.logger.info(u"method:%s, args:%s, kw:%s" % (method,
                                                args, kw))
            except Exception, e:
                #@todo:  fix the decode problem.
                self.logger and self.logger.info(str(e))
                
            
            h = self._local_service(self.stub, method)
            if h is None: raise RPCException(u"Not found interface '%s'" % method)
            
            ret = h(*args, **kw)
            
            self.logger and self.logger.info("return:%s" % (ret, ))
        except BaseException, e:
            exception = e
            self.logger and self.logger.exception(e)
開發者ID:dalinhuang,項目名稱:demodemo,代碼行數:29,代碼來源:rpc_over_http.py

示例4: __init__

class NodeMaker:
    def __init__(self):
        self.ser = Serializer()
        self.words = defaultdict(int)
        self.graph = Graph()
    def PosNo(self, morph):
        for i, p in enumerate([u'形容詞', u'名詞']):
            if p in morph.pos():
                return i+2
        return 0
    def regist(self, text):
        lines = text.split('\n')
        lst = []
        for lnum, line in enumerate(lines):
            morphs = wakachi.parse(text)
            for morph in morphs:
                if self.PosNo(morph):
                    lst.append(morph)
                    self.words[(morph.posid, morph.original)] += 1
                else:
                    lst.append(None)
            lst += [None]*5
            if line == '':
                self.consume(lst)
                lst = []
        self.consume(lst)
    def consume(self, lst, back=3, fore=10): #0:N, 1:V, 2:Y
        size = len(lst)
        for i in xrange(size):
            if lst[i] is None: continue
            posno = self.PosNo(lst[i])
            node = []
            for x in xrange(posno):
                node.append(self.ser.encode((lst[i].posid, lst[i].original(), x)))
                self.graph.registerNode(node[x])
            #for node = V
            for j in xrange(max(0,i-fore), min(size,i+back)):
                if lst[j] is None or self.PosNo(lst[j]) == 2: continue
                ny = self.ser.encode((lst[j].posid, lst[j].original(), 2))
                self.graph.addEdge(node[1], ny)
            #for node = Y
            if posno == 3:
                for j in xrange(max(0,i-back), min(size,i+fore)):
                    if lst[j] is None: continue
                    nv = self.ser.encode((lst[j].posid, lst[j].original(), 1))
                    self.graph.addEdge(node[2],nv)
開發者ID:awakia,項目名稱:experiment,代碼行數:46,代碼來源:labelrank.py

示例5: __init__

 def __init__(self, problem, config):
     self.abs_path = os.path.dirname(os.path.abspath(__file__))
     self.serializer = Serializer()
     self.config_file = config
     self.config = self.serializer.read_config(self.abs_path + "/" + config)        
     self.ik_solution_generator = IKSolutionGenerator()        
     self.clear_stats(problem)
     self.run(problem)
開發者ID:hoergems,項目名稱:abt_newt,代碼行數:8,代碼來源:run.py

示例6: _fetch_metadata_by_json

    def _fetch_metadata_by_json(self, json_obj):
        """ parses incoming json object representation and fetches related 
        object metadata from the server. Returns None or the Metadata object.

        This method is called only when core object is not found in cache, and 
        if metadata should be requested anyway, it could be done faster with 
        this method that uses GET /neo/<obj_type>/198272/metadata/

        Currently not used, because inside the pull function (in cascade mode) 
        it may be faster to collect all metadata after the whole object tree is 
        fetched. For situations, when, say, dozens of of objects tagged with the
        same value are requested, it's faster to fetch this value once at the 
        end rather than requesting related metadata for every object.
        """
        if not json_obj['fields'].has_key('metadata') or \
            not json_obj['fields']['metadata']:
            return None # no metadata field or empty metadata

        url = json_obj['permalink']

        # TODO move requests to the remote class
        resp = requests.get( url + 'metadata' , cookies=self._meta.cookie_jar )
        raw_json = get_json_from_response( resp )

        if not resp.status_code == 200:
            message = '%s (%s)' % (raw_json['message'], raw_json['details'])
            raise errors.error_codes[resp.status_code]( message )

        if not raw_json['metadata']: # if no objects exist return empty result
            return None

        mobj = Metadata()
        for p, v in raw_json['metadata']:
            prp = Serializer.deserialize(p, self)
            val = Serializer.deserialize(v, self)
            prp.append( val )

            # save both objects to cache
            self._cache.add_object( prp )
            self._cache.add_object( val )

            setattr( mobj, prp.name, prp )

        return mobj # Metadata object with list of properties (tags)
開發者ID:samarkanov,項目名稱:python-gnode-client,代碼行數:44,代碼來源:session.py

示例7: __init__

 def __init__(self, uri, method=None, namespaces=None, sid=''):
     self.__serviceURL = uri
     self.__serviceName = method
     self.namespaces = isinstance(namespaces, ClassLoader) and namespaces or \
                           ClassLoader(namespaces or [])
     self._seril = Serializer(self.namespaces)
     self.sid = sid
     self.logger = None
     self.start_call_listener = []
     self.end_call_listener = []
開發者ID:dalinhuang,項目名稱:demodemo,代碼行數:10,代碼來源:rpc_over_http.py

示例8: add

 def add(self, product_id, unit_cost, quantity=1, **extra_data_dict):
     """
         Returns True if the addition of the product of the given product_id and unit_cost with given quantity 
         is succesful else False.
         Can also add extra details in the form of dictionary.
     """
     product_dict = self.__product_dict(
         unit_cost, quantity, extra_data_dict)
     self.redis_connection.hset(
         self.user_redis_key, product_id, Serializer.dumps(product_dict))
     self.user_cart_exists = self.cart_exists(self.user_id)
     self.set_ttl()
開發者ID:nimeshkverma,項目名稱:E-Cart,代碼行數:12,代碼來源:ecart.py

示例9: get_product

 def get_product(self, product_id):
     """
         Returns the cart details as a Dictionary for the given product_id
     """
     if self.user_cart_exists:
         product_string = self.redis_connection.hget(
             self.user_redis_key, product_id)
         if product_string:
             return Serializer.loads(product_string)
         else:
             return {}
     else:
         raise ErrorMessage("The user cart is Empty")
開發者ID:nimeshkverma,項目名稱:E-Cart,代碼行數:13,代碼來源:ecart.py

示例10: write

    def write(self, buf, offset=0):
        """
        Updates object information 

        :param buf:     a YAML representation of an object.
        :type buf:      str

        :return:        0 on success or and negative error code.
        """
        try:
            new = Serializer.deserialize(self.model_instance.__class__, buf)
            new.id = self.model_instance.id

        except Exception, e:
            return -1 # TODO find a way to handle expceptions better..
開發者ID:G-Node,項目名稱:MorphDepot,代碼行數:15,代碼來源:fsmapping.py

示例11: __init__

 def __init__(self, model, **kwargs):
     self.model = model
     self.fields = kwargs.get("fields") or self.model._meta.get_all_field_names()
     self.name = kwargs.get("name")
     self.parent_name = kwargs.get("parent_name", False)
     if self.parent_name:
         self.parent_id = self.parent_name + "_id"
     else:
         self.parent_id = ""
     self.id = self.name + "_id"
     self.set_name = self.name + "_set"
     self.relations = kwargs.get("relations") or [
         rel.get_accessor_name() for rel in model._meta.get_all_related_objects()
     ]
     self.parent_model = kwargs.get("parent_model")
     self.serialize = Serializer(self.fields)
開發者ID:ColDog,項目名稱:django-api,代碼行數:16,代碼來源:views.py

示例12: __push_data_from_obj

    def __push_data_from_obj(self, obj):
        """ saves array data to disk in HDF5s and uploads new datafiles to the 
        server according to the arrays of the given obj. Saves datafile objects 
        to cache """
        data_refs = {} # collects all references to the related data - output
        model_name = self._meta.get_type_by_obj( obj )

        data_attrs = self._meta.get_array_attr_names( model_name )
        attrs_to_sync = self._cache.detect_changed_data_fields( obj )

        for attr in data_attrs: # attr is like 'times', 'signal' etc.

            arr = None
            if attr in attrs_to_sync:
                # 1. get current array and units
                fname = self._meta.app_definitions[model_name]['data_fields'][attr][2]
                if fname == 'self':
                    arr = obj # some NEO objects like signal inherit array
                else:
                    arr = getattr(obj, fname)

            if not type(arr) == type(None): # because of NEO __eq__
                units = Serializer.parse_units(arr)

                datapath = self._cache.save_data(arr)
                json_obj = self._remote.save_data(datapath)

                # update cache data map
                datalink = json_obj['permalink']
                fid = str(get_id_from_permalink( datalink ))

                folder, tempname = os.path.split(datapath)
                new_path = os.path.join(folder, fid + tempname[tempname.find('.'):])
                os.rename(datapath, new_path)
                self._cache.update_data_map(fid, new_path)

                data_refs[ attr ] = {'data': datalink, 'units': units}

            else:
                data_refs[ attr ] = None

        return data_refs
開發者ID:samarkanov,項目名稱:python-gnode-client,代碼行數:42,代碼來源:session.py

示例13: __init__

 def __init__(self, save):
     logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
     if not os.path.isdir("stats"):
         os.makedirs("stats")      
     self.save = save
     self.utils = Utils()
     logging.info("Loading cartesian coordinates...") 
     serializer = Serializer()
     
     rave_env = Environment()
     f = "stats/model/test.urdf"
     rave_env.initializeEnvironment(f)
     self.link_dimensions = rave_env.getRobotLinkDimensions()
     rave_env.destroy()
     
     self.setup_kinematics(serializer, "stats")
     self.process_covariances = []
     for logfile in glob.glob("stats" + "/*.log"):
         self.process_covariances.append(serializer.read_process_covariance(logfile))
     #arr = serializer.deserialize_joint_angles(path="stats", file="state_path1.txt")        
     #self.plot_state_path(serializer, arr)
     #sleep()
     
     self.create_video(serializer, "stats")
     logging.info("plotting paths")  
     try:  
         self.plot_paths(serializer)
     except:
         logging.warn("Paths could not be plotted")        
     logging.info("Reading config") 
     config = serializer.read_config("config.yaml", path="stats")
         
     #try:
     #self.plot_end_effector_paths(serializer, plot_scenery=True, plot_manipulator=True)
     #except:
     #    print "Error. End effector paths could not be plotted"
     
     logging.info("Plotting mean planning times")
     self.plot_mean_planning_times(serializer, 
                                   dir="stats", 
                                   filename="mean_planning_times_per_step*.yaml", 
                                   output="mean_planning_times_per_step.pdf")
     self.plot_mean_planning_times(serializer, 
                                   dir="stats", 
                                   filename="mean_planning_times_per_run*.yaml", 
                                   output="mean_planning_times_per_run.pdf")
     
     self.plot_particles(serializer, particle_limit=config['particle_plot_limit'])
     
     try:
         self.plot_paths(serializer, best_paths=True)
     except:
         logging.warn("Best_paths could not be plotted")
         
     try:
         cart_coords = serializer.load_cartesian_coords(path="stats")
     except:
         logging.warn("Cartesian_coords could not be plotted")
     
     logging.info("Plotting average distance to goal" )       
     try:
         self.plot_average_dist_to_goal(serializer, cart_coords)
     except:
         logging.warn("Average distance to goal could not be plotted")
     
     logging.info("plotting mean rewards")
     try:
         self.plot_mean_rewards(serializer)
     except Exception as e:
         logging.warn("Mean rewards could not be plotted: " + str(e))       
     
     logging.info("Plotting EMD graph...")       
     try:
         self.plot_emd_graph(serializer, cart_coords) 
     except:
         logging.warn("EMD graphs could not be plotted")
     
     logging.info("Plotting histograms...") 
     try:            
         self.save_histogram_plots(serializer, cart_coords) 
     except:
         logging.warn("Histograms could not be plotted")  
開發者ID:hoergems,項目名稱:abt_newt,代碼行數:82,代碼來源:plot_stats.py

示例14: TestSerialize

class TestSerialize(unittest.TestCase):

    @classmethod
    def _clearMsgDefs(self):
        # Clear all non-test messages
        try:
            while True:
                messages.MESSAGES.pop()
        except IndexError:
            pass

    @classmethod
    def setUpClass(self):
        self._clearMsgDefs()
        messages.MESSAGES.append(__import__("test_pb2", globals(), locals(), [], -1))
        self.blank_args = [1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 10, 0, 11, 0, 12, 0, True, 0, "a", "b", "c", "d"]
        self.neg_args = [-1.0, 0, -1.0, 0, -1, -1, -1, -1, 0, 0, 0, 0, -1, -1, -1, -1, 0, 0, 0, 0, -1, -1, -1, -1, 1, 1, "", "", "", ""]
        self.illegal_len_args = [
                                [0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
                                [0, 0, 0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""]
                            ]
        self.too_long_args = [
                                [0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
                                [0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, "", "", "", ""],
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, "", "", "", ""],
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, "", "", "", ""],
                                [0, 0, 0, 0, 0, -32769, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
                                [0, 0, 0, 0, 0, 0, 0, -32769, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -32769, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -32769, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -32769, 0, 0, 0, 0, "", "", "", ""],
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -32769, 0, 0, "", "", "", ""],
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "aaa", "", ""],
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", "aaa"]
                            ]

    def setUp(self):
        self.s = Serializer()

    def test_id(self):
        enc = self.s.serialize("Test", *self.blank_args)
        (name, ) = struct.unpack_from('20s', enc)

        self.assertTrue(name.startswith('test_pb2Test'))

    def test_values(self):
        enc = self.s.serialize("Test", *self.blank_args)

        def f(obj):
            self.assertTrue(obj.IsInitialized())
            for (_, value) in obj.ListFields():
                self.assertIn(value, self.blank_args)

        self.s.add_handler("Test", f)
        self.s.unserialize(enc)

    def test_explicit_call(self):
        enc = self.s.serialize("test.Test", *self.blank_args)

        def f(obj):
            self.assertTrue(obj.IsInitialized())
            for (_, value) in obj.ListFields():
                self.assertIn(value, self.blank_args)

        self.s.add_handler("Test", f)
        self.s.unserialize(enc)

    def test_negative_values(self):
        enc = self.s.serialize("Test", *self.neg_args)

        def f(obj):
            self.assertTrue(obj.IsInitialized())
            for (_, value) in obj.ListFields():
                self.assertIn(value, self.neg_args)

        self.s.add_handler("Test", f)
        self.s.unserialize(enc)

    def test_illegal_length_option(self):
        for args in self.illegal_len_args:
            with self.assertRaises(FieldLengthUnsupportedException):
                self.s.serialize("Test", *args)

    def test_too_long_option(self):
        for args in self.too_long_args:
            with self.assertRaises(FieldTooLongException):
                self.s.serialize("Test", *args)

    def test_unknown_message(self):
        with self.assertRaises(UnknownMessageException):
            self.s.serialize("Unknown")

    def test_unknown_message_explicit(self):
        with self.assertRaises(UnknownMessageException):
#.........這裏部分代碼省略.........
開發者ID:qstokkink,項目名稱:TriblerProtobufSerialization,代碼行數:101,代碼來源:test_serialize.py

示例15: setUp

 def setUp(self):
     self.s = Serializer()
開發者ID:qstokkink,項目名稱:TriblerProtobufSerialization,代碼行數:2,代碼來源:test_serialize.py


注:本文中的serializer.Serializer類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。