Пример #1
0
    def default(self, o, *args):
        if isinstance(o, Datetime) or o.__class__.__name__ == Datetime.__name__:
            return {
                '__time__': [o.year, o.month, o.day, o.hour, o.minute, o.second, o.microsecond]
            }
        elif isinstance(o, Timedelta) or o.__class__.__name__ == Timedelta.__name__:
            return {
                '__delta__': [o._timedelta.days, o._timedelta.seconds]
            }
        elif isinstance(o, bytes):
            return {
                '__bytes__': o.hex()
            }
        elif isinstance(o, decimal.Decimal) or o.__class__.__name__ == decimal.Decimal.__name__:
            #return format(o, f'.{MAX_LOWER_PRECISION}f')
            return {
                '__fixed__': str(fix_precision(o))
            }

        elif isinstance(o, ContractingDecimal) or o.__class__.__name__ == ContractingDecimal.__name__:
            #return format(o._d, f'.{MAX_LOWER_PRECISION}f')
            return {
                '__fixed__': str(fix_precision(o._d))
            }
        elif isinstance(o, float):
            #return format(o, f'.{MAX_LOWER_PRECISION}f')
            _o = format(o, f'.{MAX_LOWER_PRECISION}f')
            return {
                '__fixed__': str(fix_precision(decimal.Decimal(_o)))
            }
        #else:
        #    return safe_repr(o)

        return super().default(o)
Пример #2
0
    def test_fix_precision_cuts_decimals_if_high_but_not_too_high(self):
        e = Decimal(
            '12345678901234567890123456789.123456789012345678901234567890')
        f = Decimal(
            '12345678901234567890123456789.12345678901234567890123456789')

        self.assertEqual(fix_precision(e), f)
Пример #3
0
 def test_fix_precision_cuts_all_decimals_if_too_high(self):
     e = Decimal('123456789012345678901234567890.123456')
     self.assertEqual(fix_precision(e), MAX_DECIMAL)
Пример #4
0
 def test_fix_precision_doesnt_cut_high(self):
     e = Decimal('12345678901234567890123456789')
     self.assertEqual(fix_precision(e), e)
Пример #5
0
    def test_fix_precision_cuts_too_low(self):
        d = Decimal('1.123456789012345678901234567890123')
        e = Decimal('1.12345678901234567890123456789')

        self.assertEqual(fix_precision(d), e)