def represent_datetime(self, data):
     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))
 def represent_datetime(self, data):
     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))
 def represent_float(self, data):
     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()
         # 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.
         if u'.' not in value and u'e' in value:
             value = value.replace(u'e', u'.0e', 1)
     return self.represent_scalar(u'tag:yaml.org,2002:float', value)
Beispiel #4
0
 def represent_float(self, data):
     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()
         # 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.
         if u'.' not in value and u'e' in value:
             value = value.replace(u'e', u'.0e', 1)
     return self.represent_scalar(u'tag:yaml.org,2002:float', value)
 def represent_long(self, data):
     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_datetime(self, data):
     value = to_unicode(data.isoformat(' '))
     return self.represent_scalar(u'tag:yaml.org,2002:timestamp', 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)
Beispiel #8
0
    def represent_scalar_float(self, data):
        # type: (Any) -> Any
        """ this is way more complicated """
        value = None
        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)
        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 u'',
                                      abs(int(data)))
        elif data._exp is None:
            # no exponent, "normal" dot
            prec = data._prec
            if prec < 1:
                prec = 1
            # print('dw2', data._width, prec)
            ms = data._m_sign if data._m_sign else u''
            # -1 for the dot
            value = u'{}{:0{}.{}f}'.format(ms, abs(data),
                                           data._width - len(ms),
                                           data._width - prec - 1)
            while len(value) < data._width:
                value += u'0'
        else:
            # exponent
            m, es = u'{:{}e}'.format(data, data._width).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 u''
            if data._prec < 0:  # mantissa without dot
                if m2 != u'0':
                    e -= len(m2)
                else:
                    m2 = u''
                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)
Beispiel #9
0
 def represent_long(self, data):
     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))
Beispiel #10
0
 def represent_datetime(self, data):
     value = to_unicode(data.isoformat(' '))
     return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value)