summaryrefslogtreecommitdiff
path: root/representer.py
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2021-03-09 09:02:50 +0100
committerAnthon van der Neut <anthon@mnt.org>2021-03-09 09:02:50 +0100
commite73562c6f14d1d71a9fea174d58465e1b13f68af (patch)
tree309851cca7d411b31c27753555871d493282c7f0 /representer.py
parent96839d9f64f4698bdc519cbfbd48d51178460714 (diff)
downloadruamel.yaml-e73562c6f14d1d71a9fea174d58465e1b13f68af.tar.gz
remove python 2 specific code
add future deprecation warning to old style functions
Diffstat (limited to 'representer.py')
-rw-r--r--representer.py358
1 files changed, 100 insertions, 258 deletions
diff --git a/representer.py b/representer.py
index 1b5185a..4a27691 100644
--- a/representer.py
+++ b/representer.py
@@ -1,13 +1,9 @@
# coding: utf-8
-from __future__ import print_function, absolute_import, division
-
-
from ruamel.yaml.error import * # NOQA
from ruamel.yaml.nodes import * # NOQA
-from ruamel.yaml.compat import text_type, binary_type, to_unicode, PY2, PY3
from ruamel.yaml.compat import ordereddict # type: ignore
-from ruamel.yaml.compat import nprint, nprintf # NOQA
+from ruamel.yaml.compat import _F, nprint, nprintf # NOQA
from ruamel.yaml.scalarstring import (
LiteralScalarString,
FoldedScalarString,
@@ -35,11 +31,8 @@ import datetime
import sys
import types
-if PY3:
- import copyreg
- import base64
-else:
- import copy_reg as copyreg # type: ignore
+import copyreg
+import base64
if False: # MYPY
from typing import Dict, List, Any, Union, Text, Optional # NOQA
@@ -54,16 +47,6 @@ class RepresenterError(YAMLError):
pass
-if PY2:
-
- def get_classobj_bases(cls):
- # type: (Any) -> Any
- bases = [cls]
- for base in cls.__bases__:
- bases.extend(get_classobj_bases(base))
- return bases
-
-
class BaseRepresenter(object):
yaml_representers = {} # type: Dict[Any, Any]
@@ -110,15 +93,11 @@ class BaseRepresenter(object):
node = self.represented_objects[self.alias_key]
# if node is None:
# raise RepresenterError(
- # "recursive objects are not allowed: %r" % data)
+ # f"recursive objects are not allowed: {data!r}")
return node
# self.represented_objects[alias_key] = None
self.object_keeper.append(data)
data_types = type(data).__mro__
- if PY2:
- # if type(data) is types.InstanceType:
- if isinstance(data, types.InstanceType):
- data_types = get_classobj_bases(data.__class__) + list(data_types)
if data_types[0] in self.yaml_representers:
node = self.yaml_representers[data_types[0]](self, data)
else:
@@ -132,7 +111,7 @@ class BaseRepresenter(object):
elif None in self.yaml_representers:
node = self.yaml_representers[None](self, data)
else:
- node = ScalarNode(None, text_type(data))
+ node = ScalarNode(None, str(data))
# if alias_key is not None:
# self.represented_objects[alias_key] = node
return node
@@ -257,50 +236,25 @@ class SafeRepresenter(BaseRepresenter):
# so "data is ()" should not be used
if data is None or (isinstance(data, tuple) and data == ()):
return True
- if isinstance(data, (binary_type, text_type, bool, int, float)):
+ if isinstance(data, (bytes, str, bool, int, float)):
return True
return False
def represent_none(self, data):
# type: (Any) -> Any
- return self.represent_scalar(u'tag:yaml.org,2002:null', u'null')
-
- if PY3:
-
- def represent_str(self, data):
- # type: (Any) -> Any
- return self.represent_scalar(u'tag:yaml.org,2002:str', data)
-
- def represent_binary(self, data):
- # type: (Any) -> Any
- if hasattr(base64, 'encodebytes'):
- data = base64.encodebytes(data).decode('ascii')
- else:
- data = base64.encodestring(data).decode('ascii')
- return self.represent_scalar(u'tag:yaml.org,2002:binary', data, style='|')
+ return self.represent_scalar('tag:yaml.org,2002:null', 'null')
- else:
+ def represent_str(self, data):
+ # type: (Any) -> Any
+ return self.represent_scalar('tag:yaml.org,2002:str', data)
- def represent_str(self, data):
- # type: (Any) -> Any
- tag = None
- style = None
- try:
- data = unicode(data, 'ascii')
- tag = u'tag:yaml.org,2002:str'
- except UnicodeDecodeError:
- try:
- data = unicode(data, 'utf-8')
- tag = u'tag:yaml.org,2002:str'
- except UnicodeDecodeError:
- data = data.encode('base64')
- tag = u'tag:yaml.org,2002:binary'
- style = '|'
- return self.represent_scalar(tag, data, style=style)
-
- def represent_unicode(self, data):
- # type: (Any) -> Any
- return self.represent_scalar(u'tag:yaml.org,2002:str', data)
+ def represent_binary(self, data):
+ # type: (Any) -> Any
+ 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='|')
def represent_bool(self, data, anchor=None):
# type: (Any, Optional[Any]) -> Any
@@ -308,20 +262,14 @@ class SafeRepresenter(BaseRepresenter):
value = self.dumper.boolean_representation[bool(data)]
except AttributeError:
if data:
- value = u'true'
+ value = 'true'
else:
- value = u'false'
- return self.represent_scalar(u'tag:yaml.org,2002:bool', value, anchor=anchor)
+ value = 'false'
+ return self.represent_scalar('tag:yaml.org,2002:bool', value, anchor=anchor)
def represent_int(self, data):
# type: (Any) -> Any
- return self.represent_scalar(u'tag:yaml.org,2002:int', text_type(data))
-
- if PY2:
-
- def represent_long(self, data):
- # type: (Any) -> Any
- return self.represent_scalar(u'tag:yaml.org,2002:int', text_type(data))
+ return self.represent_scalar('tag:yaml.org,2002:int', str(data))
inf_value = 1e300
while repr(inf_value) != repr(inf_value * inf_value):
@@ -330,15 +278,15 @@ class SafeRepresenter(BaseRepresenter):
def represent_float(self, data):
# type: (Any) -> Any
if data != data or (data == 0.0 and data == 1.0):
- value = u'.nan'
+ value = '.nan'
elif data == self.inf_value:
- value = u'.inf'
+ value = '.inf'
elif data == -self.inf_value:
- value = u'-.inf'
+ value = '-.inf'
else:
- value = to_unicode(repr(data)).lower()
+ value = repr(data).lower()
if getattr(self.serializer, 'use_version', None) == (1, 1):
- if u'.' not in value and u'e' in value:
+ if '.' not in value and 'e' in value:
# Note that in some cases `repr(data)` represents a float number
# without the decimal parts. For instance:
# >>> repr(1e17)
@@ -346,8 +294,8 @@ class SafeRepresenter(BaseRepresenter):
# Unfortunately, this is not a valid float representation according
# to the definition of the `!!float` tag in YAML 1.1. We fix
# this by adding '.0' before the 'e' symbol.
- value = value.replace(u'e', u'.0e', 1)
- return self.represent_scalar(u'tag:yaml.org,2002:float', value)
+ value = value.replace('e', '.0e', 1)
+ return self.represent_scalar('tag:yaml.org,2002:float', value)
def represent_list(self, data):
# type: (Any) -> Any
@@ -358,38 +306,38 @@ class SafeRepresenter(BaseRepresenter):
# pairs = False
# break
# if not pairs:
- return self.represent_sequence(u'tag:yaml.org,2002:seq', data)
+ return self.represent_sequence('tag:yaml.org,2002:seq', data)
# value = []
# for item_key, item_value in data:
- # value.append(self.represent_mapping(u'tag:yaml.org,2002:map',
+ # value.append(self.represent_mapping('tag:yaml.org,2002:map',
# [(item_key, item_value)]))
- # return SequenceNode(u'tag:yaml.org,2002:pairs', value)
+ # return SequenceNode('tag:yaml.org,2002:pairs', value)
def represent_dict(self, data):
# type: (Any) -> Any
- return self.represent_mapping(u'tag:yaml.org,2002:map', data)
+ return self.represent_mapping('tag:yaml.org,2002:map', data)
def represent_ordereddict(self, data):
# type: (Any) -> Any
- return self.represent_omap(u'tag:yaml.org,2002:omap', data)
+ return self.represent_omap('tag:yaml.org,2002:omap', data)
def represent_set(self, data):
# type: (Any) -> Any
value = {} # type: Dict[Any, None]
for key in data:
value[key] = None
- return self.represent_mapping(u'tag:yaml.org,2002:set', value)
+ return self.represent_mapping('tag:yaml.org,2002:set', value)
def represent_date(self, data):
# type: (Any) -> Any
- value = to_unicode(data.isoformat())
- return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value)
+ value = data.isoformat()
+ return self.represent_scalar('tag:yaml.org,2002:timestamp', value)
def represent_datetime(self, data):
# type: (Any) -> Any
- value = to_unicode(data.isoformat(' '))
- return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value)
+ value = data.isoformat(' ')
+ return self.represent_scalar('tag:yaml.org,2002:timestamp', value)
def represent_yaml_object(self, tag, data, cls, flow_style=None):
# type: (Any, Any, Any, Any) -> Any
@@ -401,25 +349,19 @@ class SafeRepresenter(BaseRepresenter):
def represent_undefined(self, data):
# type: (Any) -> None
- raise RepresenterError('cannot represent an object: %s' % (data,))
+ raise RepresenterError(_F('cannot represent an object: {data!s}', data=data))
SafeRepresenter.add_representer(type(None), SafeRepresenter.represent_none)
SafeRepresenter.add_representer(str, SafeRepresenter.represent_str)
-if PY2:
- SafeRepresenter.add_representer(unicode, SafeRepresenter.represent_unicode)
-else:
- SafeRepresenter.add_representer(bytes, SafeRepresenter.represent_binary)
+SafeRepresenter.add_representer(bytes, SafeRepresenter.represent_binary)
SafeRepresenter.add_representer(bool, SafeRepresenter.represent_bool)
SafeRepresenter.add_representer(int, SafeRepresenter.represent_int)
-if PY2:
- SafeRepresenter.add_representer(long, SafeRepresenter.represent_long)
-
SafeRepresenter.add_representer(float, SafeRepresenter.represent_float)
SafeRepresenter.add_representer(list, SafeRepresenter.represent_list)
@@ -447,117 +389,36 @@ SafeRepresenter.add_representer(None, SafeRepresenter.represent_undefined)
class Representer(SafeRepresenter):
- if PY2:
-
- def represent_str(self, data):
- # type: (Any) -> Any
- tag = None
- style = None
- try:
- data = unicode(data, 'ascii')
- tag = u'tag:yaml.org,2002:str'
- except UnicodeDecodeError:
- try:
- data = unicode(data, 'utf-8')
- tag = u'tag:yaml.org,2002:python/str'
- except UnicodeDecodeError:
- data = data.encode('base64')
- tag = u'tag:yaml.org,2002:binary'
- style = '|'
- return self.represent_scalar(tag, data, style=style)
-
- def represent_unicode(self, data):
- # type: (Any) -> Any
- tag = None
- try:
- data.encode('ascii')
- tag = u'tag:yaml.org,2002:python/unicode'
- except UnicodeEncodeError:
- tag = u'tag:yaml.org,2002:str'
- return self.represent_scalar(tag, data)
-
- def represent_long(self, data):
- # type: (Any) -> Any
- tag = u'tag:yaml.org,2002:int'
- if int(data) is not data:
- tag = u'tag:yaml.org,2002:python/long'
- return self.represent_scalar(tag, to_unicode(data))
-
def represent_complex(self, data):
# type: (Any) -> Any
if data.imag == 0.0:
- data = u'%r' % data.real
+ data = repr(data.real)
elif data.real == 0.0:
- data = u'%rj' % data.imag
+ data = _F('{data_imag!r}j', data_imag=data.imag)
elif data.imag > 0:
- data = u'%r+%rj' % (data.real, data.imag)
+ data = _F('{data_real!r}+{data_imag!r}j', data_real=data.real, data_imag=data.imag)
else:
- data = u'%r%rj' % (data.real, data.imag)
- return self.represent_scalar(u'tag:yaml.org,2002:python/complex', data)
+ data = _F('{data_real!r}{data_imag!r}j', data_real=data.real, data_imag=data.imag)
+ return self.represent_scalar('tag:yaml.org,2002:python/complex', data)
def represent_tuple(self, data):
# type: (Any) -> Any
- return self.represent_sequence(u'tag:yaml.org,2002:python/tuple', data)
+ return self.represent_sequence('tag:yaml.org,2002:python/tuple', data)
def represent_name(self, data):
# type: (Any) -> Any
try:
- name = u'%s.%s' % (data.__module__, data.__qualname__)
+ name = _F(
+ '{modname!s}.{qualname!s}', modname=data.__module__, qualname=data.__qualname__
+ )
except AttributeError:
- # probably PY2
- name = u'%s.%s' % (data.__module__, data.__name__)
- return self.represent_scalar(u'tag:yaml.org,2002:python/name:' + name, "")
+ # ToDo: check if this can be reached in Py3
+ name = _F('{modname!s}.{name!s}', modname=data.__module__, name=data.__name__)
+ return self.represent_scalar('tag:yaml.org,2002:python/name:' + name, "")
def represent_module(self, data):
# type: (Any) -> Any
- return self.represent_scalar(u'tag:yaml.org,2002:python/module:' + data.__name__, "")
-
- if PY2:
-
- def represent_instance(self, data):
- # type: (Any) -> Any
- # For instances of classic classes, we use __getinitargs__ and
- # __getstate__ to serialize the data.
-
- # If data.__getinitargs__ exists, the object must be reconstructed
- # by calling cls(**args), where args is a tuple returned by
- # __getinitargs__. Otherwise, the cls.__init__ method should never
- # be called and the class instance is created by instantiating a
- # trivial class and assigning to the instance's __class__ variable.
-
- # If data.__getstate__ exists, it returns the state of the object.
- # Otherwise, the state of the object is data.__dict__.
-
- # We produce either a !!python/object or !!python/object/new node.
- # If data.__getinitargs__ does not exist and state is a dictionary,
- # we produce a !!python/object node . Otherwise we produce a
- # !!python/object/new node.
-
- cls = data.__class__
- class_name = u'%s.%s' % (cls.__module__, cls.__name__)
- args = None
- state = None
- if hasattr(data, '__getinitargs__'):
- args = list(data.__getinitargs__())
- if hasattr(data, '__getstate__'):
- state = data.__getstate__()
- else:
- state = data.__dict__
- if args is None and isinstance(state, dict):
- return self.represent_mapping(
- u'tag:yaml.org,2002:python/object:' + class_name, state
- )
- if isinstance(state, dict) and not state:
- return self.represent_sequence(
- u'tag:yaml.org,2002:python/object/new:' + class_name, args
- )
- value = {}
- if bool(args):
- value['args'] = args
- value['state'] = state # type: ignore
- return self.represent_mapping(
- u'tag:yaml.org,2002:python/object/new:' + class_name, value
- )
+ return self.represent_scalar('tag:yaml.org,2002:python/module:' + data.__name__, "")
def represent_object(self, data):
# type: (Any) -> Any
@@ -585,7 +446,7 @@ class Representer(SafeRepresenter):
elif hasattr(data, '__reduce__'):
reduce = data.__reduce__()
else:
- raise RepresenterError('cannot represent object: %r' % (data,))
+ raise RepresenterError(_F('cannot represent object: {data!r}', data=data))
reduce = (list(reduce) + [None] * 5)[:5]
function, args, state, listitems, dictitems = reduce
args = list(args)
@@ -598,19 +459,23 @@ class Representer(SafeRepresenter):
if function.__name__ == '__newobj__':
function = args[0]
args = args[1:]
- tag = u'tag:yaml.org,2002:python/object/new:'
+ tag = 'tag:yaml.org,2002:python/object/new:'
newobj = True
else:
- tag = u'tag:yaml.org,2002:python/object/apply:'
+ tag = 'tag:yaml.org,2002:python/object/apply:'
newobj = False
try:
- function_name = u'%s.%s' % (function.__module__, function.__qualname__)
+ function_name = _F(
+ '{fun!s}.{qualname!s}', fun=function.__module__, qualname=function.__qualname__
+ )
except AttributeError:
- # probably PY2
- function_name = u'%s.%s' % (function.__module__, function.__name__)
+ # ToDo: check if this can be reached in Py3
+ function_name = _F(
+ '{fun!s}.{name!s}', fun=function.__module__, name=function.__name__
+ )
if not args and not listitems and not dictitems and isinstance(state, dict) and newobj:
return self.represent_mapping(
- u'tag:yaml.org,2002:python/object:' + function_name, state
+ '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)
@@ -626,31 +491,18 @@ class Representer(SafeRepresenter):
return self.represent_mapping(tag + function_name, value)
-if PY2:
- Representer.add_representer(str, Representer.represent_str)
-
- Representer.add_representer(unicode, Representer.represent_unicode)
-
- Representer.add_representer(long, Representer.represent_long)
-
Representer.add_representer(complex, Representer.represent_complex)
Representer.add_representer(tuple, Representer.represent_tuple)
Representer.add_representer(type, Representer.represent_name)
-if PY2:
- Representer.add_representer(types.ClassType, Representer.represent_name)
-
Representer.add_representer(types.FunctionType, Representer.represent_name)
Representer.add_representer(types.BuiltinFunctionType, Representer.represent_name)
Representer.add_representer(types.ModuleType, Representer.represent_module)
-if PY2:
- Representer.add_multi_representer(types.InstanceType, Representer.represent_instance)
-
Representer.add_multi_representer(object, Representer.represent_object)
Representer.add_multi_representer(type, Representer.represent_name)
@@ -684,17 +536,15 @@ class RoundTripRepresenter(SafeRepresenter):
# type: (Any) -> Any
if len(self.represented_objects) == 0 and not self.serializer.use_explicit_start:
# this will be open ended (although it is not yet)
- return self.represent_scalar(u'tag:yaml.org,2002:null', u'null')
- return self.represent_scalar(u'tag:yaml.org,2002:null', "")
+ return self.represent_scalar('tag:yaml.org,2002:null', 'null')
+ return self.represent_scalar('tag:yaml.org,2002:null', "")
def represent_literal_scalarstring(self, data):
# type: (Any) -> Any
tag = None
style = '|'
anchor = data.yaml_anchor(any=True)
- if PY2 and not isinstance(data, unicode):
- data = unicode(data, 'ascii')
- tag = u'tag:yaml.org,2002:str'
+ tag = 'tag:yaml.org,2002:str'
return self.represent_scalar(tag, data, style=style, anchor=anchor)
represent_preserved_scalarstring = represent_literal_scalarstring
@@ -711,9 +561,7 @@ class RoundTripRepresenter(SafeRepresenter):
and (fold_pos < len(data) and not data[fold_pos + 1].isspace())
):
data = data[:fold_pos] + '\a' + data[fold_pos:]
- if PY2 and not isinstance(data, unicode):
- data = unicode(data, 'ascii')
- tag = u'tag:yaml.org,2002:str'
+ tag = 'tag:yaml.org,2002:str'
return self.represent_scalar(tag, data, style=style, anchor=anchor)
def represent_single_quoted_scalarstring(self, data):
@@ -721,9 +569,7 @@ class RoundTripRepresenter(SafeRepresenter):
tag = None
style = "'"
anchor = data.yaml_anchor(any=True)
- if PY2 and not isinstance(data, unicode):
- data = unicode(data, 'ascii')
- tag = u'tag:yaml.org,2002:str'
+ tag = 'tag:yaml.org,2002:str'
return self.represent_scalar(tag, data, style=style, anchor=anchor)
def represent_double_quoted_scalarstring(self, data):
@@ -731,9 +577,7 @@ class RoundTripRepresenter(SafeRepresenter):
tag = None
style = '"'
anchor = data.yaml_anchor(any=True)
- if PY2 and not isinstance(data, unicode):
- data = unicode(data, 'ascii')
- tag = u'tag:yaml.org,2002:str'
+ tag = 'tag:yaml.org,2002:str'
return self.represent_scalar(tag, data, style=style, anchor=anchor)
def represent_plain_scalarstring(self, data):
@@ -741,15 +585,13 @@ class RoundTripRepresenter(SafeRepresenter):
tag = None
style = ''
anchor = data.yaml_anchor(any=True)
- if PY2 and not isinstance(data, unicode):
- data = unicode(data, 'ascii')
- tag = u'tag:yaml.org,2002:str'
+ tag = 'tag:yaml.org,2002:str'
return self.represent_scalar(tag, data, style=style, anchor=anchor)
def insert_underscore(self, prefix, s, underscore, anchor=None):
# type: (Any, Any, Any, Any) -> Any
if underscore is None:
- return self.represent_scalar(u'tag:yaml.org,2002:int', prefix + s, anchor=anchor)
+ return self.represent_scalar('tag:yaml.org,2002:int', prefix + s, anchor=anchor)
if underscore[0]:
sl = list(s)
pos = len(s) - underscore[0]
@@ -761,7 +603,7 @@ class RoundTripRepresenter(SafeRepresenter):
s = '_' + s
if underscore[2]:
s += '_'
- return self.represent_scalar(u'tag:yaml.org,2002:int', prefix + s, anchor=anchor)
+ return self.represent_scalar('tag:yaml.org,2002:int', prefix + s, anchor=anchor)
def represent_scalar_int(self, data):
# type: (Any) -> Any
@@ -818,31 +660,31 @@ class RoundTripRepresenter(SafeRepresenter):
value = None
anchor = data.yaml_anchor(any=True)
if data != data or (data == 0.0 and data == 1.0):
- value = u'.nan'
+ value = '.nan'
elif data == self.inf_value:
- value = u'.inf'
+ value = '.inf'
elif data == -self.inf_value:
- value = u'-.inf'
+ value = '-.inf'
if value:
- return self.represent_scalar(u'tag:yaml.org,2002:float', value, anchor=anchor)
+ return self.represent_scalar('tag:yaml.org,2002:float', value, anchor=anchor)
if data._exp is None and data._prec > 0 and data._prec == data._width - 1:
# no exponent, but trailing dot
- value = u'{}{:d}.'.format(data._m_sign if data._m_sign else "", abs(int(data)))
+ value = '{}{:d}.'.format(data._m_sign if data._m_sign else "", abs(int(data)))
elif data._exp is None:
# no exponent, "normal" dot
prec = data._prec
ms = data._m_sign if data._m_sign else ""
# -1 for the dot
- value = u'{}{:0{}.{}f}'.format(
+ value = '{}{:0{}.{}f}'.format(
ms, abs(data), data._width - len(ms), data._width - prec - 1
)
if prec == 0 or (prec == 1 and ms != ""):
- value = value.replace(u'0.', u'.')
+ value = value.replace('0.', '.')
while len(value) < data._width:
- value += u'0'
+ value += '0'
else:
# exponent
- m, es = u'{:{}.{}e}'.format(
+ m, es = '{:{}.{}e}'.format(
# data, data._width, data._width - data._prec + (1 if data._m_sign else 0)
data,
data._width,
@@ -855,28 +697,28 @@ class RoundTripRepresenter(SafeRepresenter):
e = int(es)
m1, m2 = m.split('.') # always second?
while len(m1) + len(m2) < data._width - (1 if data._prec >= 0 else 0):
- m2 += u'0'
+ m2 += '0'
if data._m_sign and data > 0:
m1 = '+' + m1
- esgn = u'+' if data._e_sign else ""
+ esgn = '+' if data._e_sign else ""
if data._prec < 0: # mantissa without dot
- if m2 != u'0':
+ if m2 != '0':
e -= len(m2)
else:
m2 = ""
while (len(m1) + len(m2) - (1 if data._m_sign else 0)) < data._width:
- m2 += u'0'
+ m2 += '0'
e -= 1
- value = m1 + m2 + data._exp + u'{:{}0{}d}'.format(e, esgn, data._e_width)
+ value = m1 + m2 + data._exp + '{:{}0{}d}'.format(e, esgn, data._e_width)
elif data._prec == 0: # mantissa with trailing dot
e -= len(m2)
value = (
- m1 + m2 + u'.' + data._exp + u'{:{}0{}d}'.format(e, esgn, data._e_width)
+ m1 + m2 + '.' + data._exp + '{:{}0{}d}'.format(e, esgn, data._e_width)
)
else:
if data._m_lead0 > 0:
- m2 = u'0' * (data._m_lead0 - 1) + m1 + m2
- m1 = u'0'
+ m2 = '0' * (data._m_lead0 - 1) + m1 + m2
+ m1 = '0'
m2 = m2[: -data._m_lead0] # these should be zeros
e += data._m_lead0
while len(m1) < data._prec:
@@ -884,12 +726,12 @@ class RoundTripRepresenter(SafeRepresenter):
m2 = m2[1:]
e -= 1
value = (
- m1 + u'.' + m2 + data._exp + u'{:{}0{}d}'.format(e, esgn, data._e_width)
+ m1 + '.' + m2 + data._exp + '{:{}0{}d}'.format(e, esgn, data._e_width)
)
if value is None:
- value = to_unicode(repr(data)).lower()
- return self.represent_scalar(u'tag:yaml.org,2002:float', value, anchor=anchor)
+ value = repr(data).lower()
+ return self.represent_scalar('tag:yaml.org,2002:float', value, anchor=anchor)
def represent_sequence(self, tag, sequence, flow_style=None):
# type: (Any, Any, Any) -> Any
@@ -962,10 +804,10 @@ class RoundTripRepresenter(SafeRepresenter):
# type: (Any) -> Any
if isinstance(data, CommentedKeySeq):
self.alias_key = None
- return self.represent_sequence(u'tag:yaml.org,2002:seq', data, flow_style=True)
+ return self.represent_sequence('tag:yaml.org,2002:seq', data, flow_style=True)
if isinstance(data, CommentedKeyMap):
self.alias_key = None
- return self.represent_mapping(u'tag:yaml.org,2002:map', data, flow_style=True)
+ return self.represent_mapping('tag:yaml.org,2002:map', data, flow_style=True)
return SafeRepresenter.represent_key(self, data)
def represent_mapping(self, tag, mapping, flow_style=None):
@@ -1043,7 +885,7 @@ class RoundTripRepresenter(SafeRepresenter):
else:
arg = self.represent_data(merge_list)
arg.flow_style = True
- value.insert(merge_pos, (ScalarNode(u'tag:yaml.org,2002:merge', '<<'), arg))
+ value.insert(merge_pos, (ScalarNode('tag:yaml.org,2002:merge', '<<'), arg))
return node
def represent_omap(self, tag, omap, flow_style=None):
@@ -1109,7 +951,7 @@ class RoundTripRepresenter(SafeRepresenter):
def represent_set(self, setting):
# type: (Any) -> Any
flow_style = False
- tag = u'tag:yaml.org,2002:set'
+ tag = 'tag:yaml.org,2002:set'
# return self.represent_mapping(tag, value)
value = [] # type: List[Any]
flow_style = setting.fa.flow_style(flow_style)
@@ -1168,7 +1010,7 @@ class RoundTripRepresenter(SafeRepresenter):
else:
tag = t
else:
- tag = u'tag:yaml.org,2002:map'
+ tag = 'tag:yaml.org,2002:map'
return self.represent_mapping(tag, data)
def represent_list(self, data):
@@ -1183,7 +1025,7 @@ class RoundTripRepresenter(SafeRepresenter):
else:
tag = t
else:
- tag = u'tag:yaml.org,2002:seq'
+ tag = 'tag:yaml.org,2002:seq'
return self.represent_sequence(tag, data)
def represent_datetime(self, data):
@@ -1197,7 +1039,7 @@ class RoundTripRepresenter(SafeRepresenter):
value = data.isoformat(inter)
if _yaml['tz']:
value += _yaml['tz']
- return self.represent_scalar(u'tag:yaml.org,2002:timestamp', to_unicode(value))
+ return self.represent_scalar('tag:yaml.org,2002:timestamp', value)
def represent_tagged_scalar(self, data):
# type: (Any) -> Any