コード例 #1
0
 def represent_datetime(self, data):
     # type: (Any) -> Any
     inter = "T" if data._yaml["t"] else " "
     _yaml = data._yaml
     if _yaml["delta"]:
         data += _yaml["delta"]
         value = data.isoformat(inter)
     else:
         value = data.isoformat(inter)
     if _yaml["tz"]:
         value += _yaml["tz"]
     return self.represent_scalar(u"tag:yaml.org,2002:timestamp",
                                  to_unicode(value))
コード例 #2
0
 def represent_float(self, data):
     # type: (Any) -> Any
     if data != data or (data == 0.0 and data == 1.0):
         value = u".nan"
     elif data == self.inf_value:
         value = u".inf"
     elif data == -self.inf_value:
         value = u"-.inf"
     else:
         value = to_unicode(repr(data)).lower()
         if getattr(self.serializer, "use_version", None) == (1, 1):
             if u"." not in value and u"e" in value:
                 # 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 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)
コード例 #3
0
    def represent_scalar_float(self, data):
        # type: (Any) -> Any
        """ this is way more complicated """
        value = None
        anchor = data.yaml_anchor(any=True)
        if data != data or (data == 0.0 and data == 1.0):
            value = u".nan"
        elif data == self.inf_value:
            value = u".inf"
        elif data == -self.inf_value:
            value = u"-.inf"
        if value:
            return self.represent_scalar(u"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)))
        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(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".")
            while len(value) < data._width:
                value += u"0"
        else:
            # exponent
            m, es = u"{:{}.{}e}".format(
                # data, data._width, data._width - data._prec + (1 if data._m_sign else 0)
                data,
                data._width,
                data._width + (1 if data._m_sign else 0),
            ).split("e")
            w = data._width if data._prec > 0 else (data._width + 1)
            if data < 0:
                w += 1
            m = m[:w]
            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"
            if data._m_sign and data > 0:
                m1 = "+" + m1
            esgn = u"+" if data._e_sign else ""
            if data._prec < 0:  # mantissa without dot
                if m2 != u"0":
                    e -= len(m2)
                else:
                    m2 = ""
                while (len(m1) + len(m2) -
                       (1 if data._m_sign else 0)) < data._width:
                    m2 += u"0"
                    e -= 1
                value = (m1 + m2 + data._exp +
                         u"{:{}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))
            else:
                if data._m_lead0 > 0:
                    m2 = u"0" * (data._m_lead0 - 1) + m1 + m2
                    m1 = u"0"
                    m2 = m2[:-data._m_lead0]  # these should be zeros
                    e += data._m_lead0
                while len(m1) < data._prec:
                    m1 += m2[0]
                    m2 = m2[1:]
                    e -= 1
                value = (m1 + u"." + m2 + data._exp +
                         u"{:{}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)
コード例 #4
0
 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))
コード例 #5
0
 def represent_datetime(self, data):
     # type: (Any) -> Any
     value = to_unicode(data.isoformat(" "))
     return self.represent_scalar(u"tag:yaml.org,2002:timestamp", value)