'Converts a finite Decimal instance to a rational number, exactly.'

@classmethod
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()):
        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)))
