Package pyamf :: Package adapters :: Module _decimal

[hide private]
[frames] | no frames]

# Source Code for Module pyamf.adapters._decimal

     1  # Copyright (c) 2007-2009 The PyAMF Project.
     2  # See LICENSE.txt for details.
     3
     4  """
     5  Adapter for the C{decimal} module.
     6
     7  @since: 0.4
     8  """
     9
    10  import decimal
    11
    12  import pyamf
    13
    14
    15 -def convert_Decimal(x, encoder):
    16      """
    17      Called when an instance of L{decimal.Decimal} is about to be encoded to
    18      an AMF stream.
    19
    20      @param x: The L{decimal.Decimal} instance to encode.
    21      @param encoder: The L{pyamf.BaseEncoder} instance about to perform the
    22          operation.
    23      @return: If the encoder is in 'strict' mode then C{x} will be converted to
    24          a float. Otherwise an L{pyamf.EncodeError} with a friendly message is
    25          raised.
    26      """
    27      if encoder is not None and isinstance(encoder, pyamf.BaseEncoder):
    28          if encoder.strict is False:
    29              return float(x)
    30
    31      raise pyamf.EncodeError('Unable to encode decimal.Decimal instances as '
    32          'there is no way to guarantee exact conversion. Use strict=False to '
    33          'convert to a float.')
    34
    35  if hasattr(decimal, 'Decimal'):
    36      pyamf.add_type(decimal.Decimal, convert_Decimal)
    37