def test_total_seconds(self):
     class MockTimedelta(object):
         days = 1
         seconds = 1
         microseconds = 100
     timedelta = MockTimedelta()
     expected = (timedelta.days * 86400.0) + float(timedelta.seconds) + (timedelta.microseconds / 1000000.0)
     assert compat.total_seconds(timedelta) == expected
Example #2
0
    def test_total_seconds(self):
        class MockTimedelta(object):
            days = 1
            seconds = 1
            microseconds = 100

        timedelta = MockTimedelta()
        expected = (timedelta.days * 86400.0) + float(
            timedelta.seconds) + (timedelta.microseconds / 1000000.0)
        assert compat.total_seconds(timedelta) == expected
 def default(self, obj):
     # For Date Time string spec, see ECMA 262
     # http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
     if isinstance(obj, Promise):
         return force_text(obj)
     elif isinstance(obj, datetime.datetime):
         representation = obj.isoformat()
         if obj.microsecond:
             representation = representation[:23] + representation[26:]
         if representation.endswith('+00:00'):
             representation = representation[:-6] + 'Z'
         return representation
     elif isinstance(obj, datetime.date):
         return obj.isoformat()
     elif isinstance(obj, datetime.time):
         if timezone and timezone.is_aware(obj):
             raise ValueError("JSON can't represent timezone-aware times.")
         representation = obj.isoformat()
         if obj.microsecond:
             representation = representation[:12]
         return representation
     elif isinstance(obj, datetime.timedelta):
         return six.text_type(total_seconds(obj))
     elif isinstance(obj, decimal.Decimal):
         # Serializers will coerce decimals to strings by default.
         return float(obj)
     elif isinstance(obj, uuid.UUID):
         return six.text_type(obj)
     elif isinstance(obj, QuerySet):
         return tuple(obj)
     elif isinstance(obj, six.binary_type):
         # Best-effort for binary blobs. See #4187.
         return obj.decode('utf-8')
     elif hasattr(obj, 'tolist'):
         # Numpy arrays and array scalars.
         return obj.tolist()
     elif hasattr(obj, '__getitem__'):
         try:
             return dict(obj)
         except:
             pass
     elif hasattr(obj, '__iter__'):
         return tuple(item for item in obj)
     elif (coreapi is not None) and isinstance(obj, (coreapi.Document, coreapi.Error)):
         raise RuntimeError(
             'Cannot return a coreapi object from a JSON view. '
             'You should be using a schema renderer instead for this view.'
         )
     return super(JSONEncoder, self).default(obj)
 def default(self, obj):
     # For Date Time string spec, see ECMA 262
     # http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
     if isinstance(obj, Promise):
         return force_text(obj)
     elif isinstance(obj, datetime.datetime):
         representation = obj.isoformat()
         if representation.endswith('+00:00'):
             representation = representation[:-6] + 'Z'
         return representation
     elif isinstance(obj, datetime.date):
         return obj.isoformat()
     elif isinstance(obj, datetime.time):
         if timezone and timezone.is_aware(obj):
             raise ValueError("JSON can't represent timezone-aware times.")
         representation = obj.isoformat()
         if obj.microsecond:
             representation = representation[:12]
         return representation
     elif isinstance(obj, datetime.timedelta):
         return six.text_type(total_seconds(obj))
     elif isinstance(obj, decimal.Decimal):
         # Serializers will coerce decimals to strings by default.
         return float(obj)
     elif isinstance(obj, uuid.UUID):
         return six.text_type(obj)
     elif isinstance(obj, QuerySet):
         return tuple(obj)
     elif isinstance(obj, six.binary_type):
         # Best-effort for binary blobs. See #4187.
         return obj.decode('utf-8')
     elif hasattr(obj, 'tolist'):
         # Numpy arrays and array scalars.
         return obj.tolist()
     elif hasattr(obj, '__getitem__'):
         try:
             return dict(obj)
         except:
             pass
     elif hasattr(obj, '__iter__'):
         return tuple(item for item in obj)
     elif (coreapi is not None) and isinstance(obj, (coreapi.Document, coreapi.Error)):
         raise RuntimeError(
             'Cannot return a coreapi object from a JSON view. '
             'You should be using a schema renderer instead for this view.'
         )
     return super(JSONEncoder, self).default(obj)
Example #5
0
 def default(self, obj):
     # For Date Time string spec, see ECMA 262
     # http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
     if isinstance(obj, Promise):
         return force_text(obj)
     elif isinstance(obj, datetime.datetime):
         representation = obj.isoformat()
         if obj.microsecond:
             representation = representation[:23] + representation[26:]
         if representation.endswith('+00:00'):
             representation = representation[:-6] + 'Z'
         return representation
     elif isinstance(obj, datetime.date):
         return obj.isoformat()
     elif isinstance(obj, datetime.time):
         if timezone and timezone.is_aware(obj):
             raise ValueError("JSON can't represent timezone-aware times.")
         representation = obj.isoformat()
         if obj.microsecond:
             representation = representation[:12]
         return representation
     elif isinstance(obj, datetime.timedelta):
         return six.text_type(total_seconds(obj))
     elif isinstance(obj, decimal.Decimal):
         # Serializers will coerce decimals to strings by default.
         return float(obj)
     elif isinstance(obj, uuid.UUID):
         return six.text_type(obj)
     elif isinstance(obj, QuerySet):
         return tuple(obj)
     elif hasattr(obj, 'tolist'):
         # Numpy arrays and array scalars.
         return obj.tolist()
     elif hasattr(obj, '__getitem__'):
         try:
             return dict(obj)
         except:
             pass
     elif hasattr(obj, '__iter__'):
         return tuple(item for item in obj)
     return super(JSONEncoder, self).default(obj)
 def default(self, obj):
     # For Date Time string spec, see ECMA 262
     # http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
     if isinstance(obj, Promise):
         return force_text(obj)
     elif isinstance(obj, datetime.datetime):
         representation = obj.isoformat()
         if obj.microsecond:
             representation = representation[:23] + representation[26:]
         if representation.endswith('+00:00'):
             representation = representation[:-6] + 'Z'
         return representation
     elif isinstance(obj, datetime.date):
         return obj.isoformat()
     elif isinstance(obj, datetime.time):
         if timezone and timezone.is_aware(obj):
             raise ValueError("JSON can't represent timezone-aware times.")
         representation = obj.isoformat()
         if obj.microsecond:
             representation = representation[:12]
         return representation
     elif isinstance(obj, datetime.timedelta):
         return six.text_type(total_seconds(obj))
     elif isinstance(obj, decimal.Decimal):
         # Serializers will coerce decimals to strings by default.
         return float(obj)
     elif isinstance(obj, uuid.UUID):
         return six.text_type(obj)
     elif isinstance(obj, QuerySet):
         return tuple(obj)
     elif hasattr(obj, 'tolist'):
         # Numpy arrays and array scalars.
         return obj.tolist()
     elif hasattr(obj, '__getitem__'):
         try:
             return dict(obj)
         except:
             pass
     elif hasattr(obj, '__iter__'):
         return tuple(item for item in obj)
     return super(JSONEncoder, self).default(obj)