def __init__(self, amount=None, currency=DEFAULT_CURRENCY,
amount_raw=None):
if not (amount is None) ^ (amount_raw is None):
raise ValueError("You must specify an amount or a raw amount")
if not isinstance(currency, Currency):
raise TypeError("You must specify a valid Currency")
if amount is not None:
if not isinstance(amount, numbers.Real):
raise TypeError("Amount must be an integer or float")
amount_raw = int(amount * 10**currency.precision)
if not isinstance(amount_raw, numbers.Integral):
raise TypeError("Amount must be an integer or float")
self.amount_raw = amount_raw
self.currency = currency
python类Integral()的实例源码
def __init__(self, amount=None, currency=DEFAULT_CURRENCY,
amount_raw=None):
if not (amount is None) ^ (amount_raw is None):
raise ValueError("You must specify an amount or a raw amount")
if not isinstance(currency, Currency):
raise TypeError("You must specify a valid Currency")
if amount is not None:
if not isinstance(amount, numbers.Real):
raise TypeError("Amount must be an integer or float")
amount_raw = int(amount * 10**currency.precision)
if not isinstance(amount_raw, numbers.Integral):
raise TypeError("Amount must be an integer or float")
self.amount_raw = amount_raw
self.currency = currency
def __init__(self, amount=None, currency=DEFAULT_CURRENCY,
amount_raw=None):
if not (amount is None) ^ (amount_raw is None):
raise ValueError("You must specify an amount or a raw amount")
if not isinstance(currency, Currency):
raise TypeError("You must specify a valid Currency")
if amount is not None:
if not isinstance(amount, numbers.Real):
raise TypeError("Amount must be an integer or float")
amount_raw = int(amount * 10**currency.precision)
if not isinstance(amount_raw, numbers.Integral):
raise TypeError("Amount must be an integer or float")
self.amount_raw = amount_raw
self.currency = currency
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)
def _number_to_bucket(value, bucket_size):
result = None
if isinstance(value, numbers.Integral):
start = int(math.ceil(value / bucket_size) * bucket_size - 1)
end = int(math.floor(value / bucket_size) * bucket_size)
result = [start, end]
elif isinstance(value, numbers.Real):
start = float(math.ceil(value / bucket_size) * bucket_size - 1)
end = float(math.floor(value / bucket_size) * bucket_size)
result = [start, end]
elif isinstance(value, basestring):
# @FIXME Implement
result = ''
elif isinstance(value, (datetime.datetime, datetime.date)):
pass
return result
def __call__(self, value):
decimals = pub.options.reprdigits
if isinstance(value, str):
if self._preserve_strings:
return '"%s"' % value
else:
return value
if isinstance(value, (pointerutils.Double, pointerutils.PDouble)):
value = float(value)
if ((decimals > -1) and
isinstance(value, numbers.Real) and
(not isinstance(value, numbers.Integral))):
string = '{0:.{1}f}'.format(value, decimals)
string = string.rstrip('0')
if string.endswith('.'):
string += '0'
return string
else:
return repr(value)
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)
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)
def test_round(self):
"""
Note that the Python 2.x round() function fails these tests. The
Python 3.x round() function passes them, as should our custom
round() function.
"""
self.assertEqual(round(0.1250, 2), 0.12)
self.assertEqual(round(0.1350, 2), 0.14)
self.assertEqual(round(0.1251, 2), 0.13)
self.assertEqual(round(0.125000001, 2), 0.13)
self.assertEqual(round(123.5, 0), 124.0)
self.assertEqual(round(123.5), 124)
self.assertEqual(round(12.35, 2), 12.35)
self.assertEqual(round(12.35, 1), 12.3)
self.assertEqual(round(12.35, 0), 12.0)
self.assertEqual(round(123.5, 1), 123.5)
self.assertTrue(isinstance(round(123.5, 0), float))
self.assertTrue(isinstance(round(123.5), Integral))
def no(mytype, argnums=(1,)):
"""
A shortcut for the disallow_types decorator that disallows only one type
(in any position in argnums).
Example use:
>>> class newstr(object):
... @no('bytes')
... def __add__(self, other):
... pass
>>> newstr(u'1234') + b'1234' #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TypeError: argument can't be bytes
The object can also be passed directly, but passing the string helps
to prevent circular import problems.
"""
if isinstance(argnums, Integral):
argnums = (argnums,)
disallowed_types = [mytype] * len(argnums)
return disallow_types(argnums, disallowed_types)
def isint(obj):
"""
Deprecated. Tests whether an object is a Py3 ``int`` or either a Py2 ``int`` or
``long``.
Instead of using this function, you can use:
>>> from future.builtins import int
>>> isinstance(obj, int)
The following idiom is equivalent:
>>> from numbers import Integral
>>> isinstance(obj, Integral)
"""
return isinstance(obj, numbers.Integral)
def std_PTR_qry(msg):
qs = msg.question
DEBUGLOG( str(len(qs)) + ' questions.')
iparpa = qs[0].to_text().split(' ', 1)[0]
DEBUGLOG('Host: ' + iparpa)
resp = make_response(qry=msg)
hosts = respuestas(iparpa[:-1], 'PTR')
if isinstance(hosts, numbers.Integral):
DEBUGLOG('No host....')
resp = make_response(qry=msg, RCODE=3) # RCODE = 3 NXDOMAIN
return resp
for host in hosts:
DEBUGLOG('Adding ' + host.to_text())
rrset = dns.rrset.from_text(iparpa, 1000, dns.rdataclass.IN, dns.rdatatype.PTR, host.to_text())
resp.answer.append(rrset)
return resp
def std_MX_qry(msg):
qs = msg.question
DEBUGLOG(str(len(qs)) + ' questions.')
iparpa = qs[0].to_text().split(' ', 1)[0]
DEBUGLOG('Host: ' + iparpa)
resp = make_response(qry=msg, RCODE=3) # RCODE = 3 NXDOMAIN
return resp
#Temporal disable MX responses
resp = make_response(qry=msg)
hosts = respuestas(iparpa[:-1], 'MX')
if isinstance(hosts, numbers.Integral):
DEBUGLOG('No host....')
resp = make_response(qry=msg, RCODE=3) # RCODE = 3 NXDOMAIN
return resp
for host in hosts:
DEBUGLOG('Adding ' + host.to_text())
rrset = dns.rrset.from_text(iparpa, 1000, dns.rdataclass.IN, dns.rdatatype.MX, host.to_text())
resp.answer.append(rrset)
return resp
def check_cv(cv=3, y=None, classifier=False):
"""Dask aware version of ``sklearn.model_selection.check_cv``
Same as the scikit-learn version, but works if ``y`` is a dask object.
"""
if cv is None:
cv = 3
# If ``cv`` is not an integer, the scikit-learn implementation doesn't
# touch the ``y`` object, so passing on a dask object is fine
if not is_dask_collection(y) or not isinstance(cv, numbers.Integral):
return model_selection.check_cv(cv, y, classifier)
if classifier:
# ``y`` is a dask object. We need to compute the target type
target_type = delayed(type_of_target, pure=True)(y).compute()
if target_type in ('binary', 'multiclass'):
return StratifiedKFold(cv)
return KFold(cv)
def __init__(self, date=None, calendar=gregorian, format=None):
self.calendar = calendar
if date is None:
self.ordinal = datetime.date.today().toordinal()
self.ymd = calendar.ordinal_to_date(self.ordinal)
elif isinstance(date, Date):
self.ordinal = date.ordinal
self.ymd = calendar.ordinal_to_date(self.ordinal)
elif isinstance(date, datetime.date):
self.ordinal = date.toordinal()
self.ymd = calendar.ordinal_to_date(self.ordinal)
elif isinstance(date, Integral):
self.ordinal = int(date)
self.ymd = calendar.ordinal_to_date(self.ordinal)
elif non_string_sequence(date, Integral) and len(date) == 3:
self.ymd = YMD(*date)
if not calendar.validate(self.ymd):
raise InvalidDate("Invalid {} date: {}".format(calendar.calendar_name, self.ymd))
self.ordinal = calendar.date_to_ordinal(self.ymd)
else:
raise TypeError("Invalid input type for Date")
self.weekday = calendar.weekday(self.ymd, self.ordinal)
self.format = format
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)
def std_PTR_qry(msg):
qs = msg.question
DEBUGLOG( str(len(qs)) + ' questions.')
iparpa = qs[0].to_text().split(' ', 1)[0]
DEBUGLOG('Host: ' + iparpa)
resp = make_response(qry=msg)
hosts = respuestas(iparpa[:-1], 'PTR')
if isinstance(hosts, numbers.Integral):
DEBUGLOG('No host....')
resp = make_response(qry=msg, RCODE=3) # RCODE = 3 NXDOMAIN
return resp
for host in hosts:
DEBUGLOG('Adding ' + host.to_text())
rrset = dns.rrset.from_text(iparpa, 1000, dns.rdataclass.IN, dns.rdatatype.PTR, host.to_text())
resp.answer.append(rrset)
return resp
def std_MX_qry(msg):
qs = msg.question
DEBUGLOG(str(len(qs)) + ' questions.')
iparpa = qs[0].to_text().split(' ', 1)[0]
DEBUGLOG('Host: ' + iparpa)
resp = make_response(qry=msg, RCODE=3) # RCODE = 3 NXDOMAIN
return resp
#Temporal disable MX responses
resp = make_response(qry=msg)
hosts = respuestas(iparpa[:-1], 'MX')
if isinstance(hosts, numbers.Integral):
DEBUGLOG('No host....')
resp = make_response(qry=msg, RCODE=3) # RCODE = 3 NXDOMAIN
return resp
for host in hosts:
DEBUGLOG('Adding ' + host.to_text())
rrset = dns.rrset.from_text(iparpa, 1000, dns.rdataclass.IN, dns.rdatatype.MX, host.to_text())
resp.answer.append(rrset)
return resp
def std_AAAA_qry(msg):
if not Forward:
DEBUGLOG('No host....')
resp = make_response(qry=msg, RCODE=3) # RCODE = 3 NXDOMAIN
return resp
qs = msg.question
DEBUGLOG(str(len(qs)) + ' questions.')
iparpa = qs[0].to_text().split(' ', 1)[0]
DEBUGLOG('Host: ' + iparpa)
resp = make_response(qry=msg)
hosts = respuestas(iparpa[:-1], 'AAAA')
if isinstance(hosts, numbers.Integral):
DEBUGLOG('No host....')
resp = make_response(qry=msg, RCODE=3) # RCODE = 3 NXDOMAIN
return resp
for host in hosts:
DEBUGLOG('Adding ' + host.to_text())
rrset = dns.rrset.from_text(iparpa, 1000, dns.rdataclass.IN, dns.rdatatype.AAAA, host.to_text())
resp.answer.append(rrset)
return resp
def no(mytype, argnums=(1,)):
"""
A shortcut for the disallow_types decorator that disallows only one type
(in any position in argnums).
Example use:
>>> class newstr(object):
... @no('bytes')
... def __add__(self, other):
... pass
>>> newstr(u'1234') + b'1234' #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TypeError: argument can't be bytes
The object can also be passed directly, but passing the string helps
to prevent circular import problems.
"""
if isinstance(argnums, Integral):
argnums = (argnums,)
disallowed_types = [mytype] * len(argnums)
return disallow_types(argnums, disallowed_types)