parameters: (cls, dec)
docstring: Converts a finite Decimal instance to a rational number, exactly.
docstring_summary: Converts a finite Decimal instance to a rational number, exactly.
docstring_tokens: Converts a finite Decimal instance to a rational number exactly .
function: def from_decimal(cls, dec):
          """Converts a finite Decimal instance to a rational number, exactly."""
          from decimal import Decimal
          if isinstance(dec, numbers.Integral):
              dec = Decimal(int(dec))
          elif not isinstance(dec, Decimal):
              raise TypeError(
                  "%s.from_decimal() only takes Decimals, not %r (%s)" %
                  (cls.__name__, dec, type(dec).__name__))
          if not dec.is_finite():
              # Catches infinities and nans.
              raise TypeError("Cannot convert %s to %s." % (dec, cls.__name__))
          sign, digits, exp = dec.as_tuple()
          digits = int(''.join(map(str, digits)))
          if sign:
              digits = -digits
          if exp >= 0:
              return cls(digits * 10 ** exp)
          else:
              return cls(digits, 10 ** -exp)
function_tokens: def from_decimal ( cls , dec ) : from decimal import Decimal if isinstance ( dec , numbers . Integral ) : dec = Decimal ( int ( dec ) ) elif not isinstance ( dec , Decimal ) : raise TypeError ( "%s.from_decimal() only takes Decimals, not %r (%s)" % ( cls . __name__ , dec , type ( dec ) . __name__ ) ) if not dec . is_finite ( ) : # Catches infinities and nans. raise TypeError ( "Cannot convert %s to %s." % ( dec , cls . __name__ ) ) sign , digits , exp = dec . as_tuple ( ) digits = int ( '' . join ( map ( str , digits ) ) ) if sign : digits = - digits if exp >= 0 : return cls ( digits * 10 ** exp ) else : return cls ( digits , 10 ** - exp )