'RepresenterError']
else: #if node is None: # raise RepresenterError("recursive objects are not allowed: %r" % data) #self.represented_objects[alias_key] = None else: else: if None in self.yaml_multi_representers: node = self.yaml_multi_representers[None](self, data) elif None in self.yaml_representers: node = self.yaml_representers[None](self, data) else: node = ScalarNode(None, str(data)) #if alias_key is not None: # self.represented_objects[alias_key] = node
def add_representer(cls, data_type, representer):
def add_multi_representer(cls, data_type, representer):
node.flow_style = self.default_flow_style else:
except TypeError: pass best_style = False node.flow_style = self.default_flow_style else:
return False
return True return True
return self.represent_scalar('tag:yaml.org,2002:null', 'null')
if hasattr(base64, 'encodebytes'): data = base64.encodebytes(data).decode('ascii') else: data = base64.encodestring(data).decode('ascii') return self.represent_scalar('tag:yaml.org,2002:binary', data, style='|')
else:
value = '.inf' value = '-.inf' else: # Note that in some cases `repr(data)` represents a float number # without the decimal parts. For instance: # >>> repr(1e17) # '1e17' # Unfortunately, this is not a valid float representation according # to the definition of the `!!float` tag. We fix this by adding # '.0' before the 'e' symbol.
#pairs = (len(data) > 0 and isinstance(data, list)) #if pairs: # for item in data: # if not isinstance(item, tuple) or len(item) != 2: # pairs = False # break #if not pairs: #value = [] #for item_key, item_value in data: # value.append(self.represent_mapping(u'tag:yaml.org,2002:map', # [(item_key, item_value)])) #return SequenceNode(u'tag:yaml.org,2002:pairs', value)
value = {} for key in data: value[key] = None return self.represent_mapping('tag:yaml.org,2002:set', value)
value = data.isoformat() return self.represent_scalar('tag:yaml.org,2002:timestamp', value)
if hasattr(data, '__getstate__'): state = data.__getstate__() else: state = data.__dict__.copy() return self.represent_mapping(tag, state, flow_style=flow_style)
raise RepresenterError("cannot represent an object: %s" % data)
SafeRepresenter.represent_none)
SafeRepresenter.represent_str)
SafeRepresenter.represent_binary)
SafeRepresenter.represent_bool)
SafeRepresenter.represent_int)
SafeRepresenter.represent_float)
SafeRepresenter.represent_list)
SafeRepresenter.represent_list)
SafeRepresenter.represent_dict)
SafeRepresenter.represent_set)
SafeRepresenter.represent_date)
SafeRepresenter.represent_datetime)
SafeRepresenter.represent_undefined)
if data.imag == 0.0: data = '%r' % data.real elif data.real == 0.0: data = '%rj' % data.imag elif data.imag > 0: data = '%r+%rj' % (data.real, data.imag) else: data = '%r%rj' % (data.real, data.imag) return self.represent_scalar('tag:yaml.org,2002:python/complex', data)
return self.represent_sequence('tag:yaml.org,2002:python/tuple', data)
name = '%s.%s' % (data.__module__, data.__name__) return self.represent_scalar('tag:yaml.org,2002:python/name:'+name, '')
return self.represent_scalar( 'tag:yaml.org,2002:python/module:'+data.__name__, '')
# We use __reduce__ API to save the data. data.__reduce__ returns # a tuple of length 2-5: # (function, args, state, listitems, dictitems)
# For reconstructing, we calls function(*args), then set its state, # listitems, and dictitems if they are not None.
# A special case is when function.__name__ == '__newobj__'. In this # case we create the object with args[0].__new__(*args).
# Another special case is when __reduce__ returns a string - we don't # support it.
# We produce a !!python/object, !!python/object/new or # !!python/object/apply node.
cls = type(data) if cls in copyreg.dispatch_table: reduce = copyreg.dispatch_table[cls](data) elif hasattr(data, '__reduce_ex__'): reduce = data.__reduce_ex__(2) elif hasattr(data, '__reduce__'): reduce = data.__reduce__() else: raise RepresenterError("cannot represent object: %r" % data) reduce = (list(reduce)+[None]*5)[:5] function, args, state, listitems, dictitems = reduce args = list(args) if state is None: state = {} if listitems is not None: listitems = list(listitems) if dictitems is not None: dictitems = dict(dictitems) if function.__name__ == '__newobj__': function = args[0] args = args[1:] tag = 'tag:yaml.org,2002:python/object/new:' newobj = True else: tag = 'tag:yaml.org,2002:python/object/apply:' newobj = False function_name = '%s.%s' % (function.__module__, function.__name__) if not args and not listitems and not dictitems \ and isinstance(state, dict) and newobj: return self.represent_mapping( 'tag:yaml.org,2002:python/object:'+function_name, state) if not listitems and not dictitems \ and isinstance(state, dict) and not state: return self.represent_sequence(tag+function_name, args) value = {} if args: value['args'] = args if state or not isinstance(state, dict): value['state'] = state if listitems: value['listitems'] = listitems if dictitems: value['dictitems'] = dictitems return self.represent_mapping(tag+function_name, value)
# Provide uniform representation across different Python versions. data_type = type(data) tag = 'tag:yaml.org,2002:python/object/apply:%s.%s' \ % (data_type.__module__, data_type.__name__) items = [[key, value] for key, value in data.items()] return self.represent_sequence(tag, [items])
Representer.represent_complex)
Representer.represent_tuple)
Representer.represent_name)
Representer.represent_ordered_dict)
Representer.represent_name)
Representer.represent_name)
Representer.represent_module)
Representer.represent_object)
|