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 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 -def convert_Decimal(x, encoder):
    15      """
    16      Called when an instance of L{decimal.Decimal} is about to be encoded to
    17      an AMF stream.
    18
    19      @param x: The L{decimal.Decimal} instance to encode.
    20      @param encoder: The L{pyamf.BaseEncoder} instance about to perform the
    21          operation.
    22      @return: If the encoder is in 'strict' mode then C{x} will be converted to
    23          a float. Otherwise an L{pyamf.EncodeError} with a friendly message is
    24          raised.
    25      """
    26      if encoder is not None and isinstance(encoder, pyamf.BaseEncoder):
    27          if encoder.strict is False:
    28              return float(x)
    29
    30      raise pyamf.EncodeError('Unable to encode decimal.Decimal instances as '
    31          'there is no way to guarantee exact conversion. Use strict=False to '
    32          'convert to a float.')
    33
    34  if hasattr(decimal, 'Decimal'):
    35      pyamf.add_type(decimal.Decimal, convert_Decimal)
    36