Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2....
File: numbers.py
# Copyright 2007 Google, Inc. All Rights Reserved.
[0] Fix | Delete
# Licensed to PSF under a Contributor Agreement.
[1] Fix | Delete
[2] Fix | Delete
"""Abstract Base Classes (ABCs) for numbers, according to PEP 3141.
[3] Fix | Delete
[4] Fix | Delete
TODO: Fill out more detailed documentation on the operators."""
[5] Fix | Delete
[6] Fix | Delete
from __future__ import division
[7] Fix | Delete
from abc import ABCMeta, abstractmethod, abstractproperty
[8] Fix | Delete
[9] Fix | Delete
__all__ = ["Number", "Complex", "Real", "Rational", "Integral"]
[10] Fix | Delete
[11] Fix | Delete
class Number(object):
[12] Fix | Delete
"""All numbers inherit from this class.
[13] Fix | Delete
[14] Fix | Delete
If you just want to check if an argument x is a number, without
[15] Fix | Delete
caring what kind, use isinstance(x, Number).
[16] Fix | Delete
"""
[17] Fix | Delete
__metaclass__ = ABCMeta
[18] Fix | Delete
__slots__ = ()
[19] Fix | Delete
[20] Fix | Delete
# Concrete numeric types must provide their own hash implementation
[21] Fix | Delete
__hash__ = None
[22] Fix | Delete
[23] Fix | Delete
[24] Fix | Delete
## Notes on Decimal
[25] Fix | Delete
## ----------------
[26] Fix | Delete
## Decimal has all of the methods specified by the Real abc, but it should
[27] Fix | Delete
## not be registered as a Real because decimals do not interoperate with
[28] Fix | Delete
## binary floats (i.e. Decimal('3.14') + 2.71828 is undefined). But,
[29] Fix | Delete
## abstract reals are expected to interoperate (i.e. R1 + R2 should be
[30] Fix | Delete
## expected to work if R1 and R2 are both Reals).
[31] Fix | Delete
[32] Fix | Delete
class Complex(Number):
[33] Fix | Delete
"""Complex defines the operations that work on the builtin complex type.
[34] Fix | Delete
[35] Fix | Delete
In short, those are: a conversion to complex, .real, .imag, +, -,
[36] Fix | Delete
*, /, abs(), .conjugate, ==, and !=.
[37] Fix | Delete
[38] Fix | Delete
If it is given heterogenous arguments, and doesn't have special
[39] Fix | Delete
knowledge about them, it should fall back to the builtin complex
[40] Fix | Delete
type as described below.
[41] Fix | Delete
"""
[42] Fix | Delete
[43] Fix | Delete
__slots__ = ()
[44] Fix | Delete
[45] Fix | Delete
@abstractmethod
[46] Fix | Delete
def __complex__(self):
[47] Fix | Delete
"""Return a builtin complex instance. Called for complex(self)."""
[48] Fix | Delete
[49] Fix | Delete
# Will be __bool__ in 3.0.
[50] Fix | Delete
def __nonzero__(self):
[51] Fix | Delete
"""True if self != 0. Called for bool(self)."""
[52] Fix | Delete
return self != 0
[53] Fix | Delete
[54] Fix | Delete
@abstractproperty
[55] Fix | Delete
def real(self):
[56] Fix | Delete
"""Retrieve the real component of this number.
[57] Fix | Delete
[58] Fix | Delete
This should subclass Real.
[59] Fix | Delete
"""
[60] Fix | Delete
raise NotImplementedError
[61] Fix | Delete
[62] Fix | Delete
@abstractproperty
[63] Fix | Delete
def imag(self):
[64] Fix | Delete
"""Retrieve the imaginary component of this number.
[65] Fix | Delete
[66] Fix | Delete
This should subclass Real.
[67] Fix | Delete
"""
[68] Fix | Delete
raise NotImplementedError
[69] Fix | Delete
[70] Fix | Delete
@abstractmethod
[71] Fix | Delete
def __add__(self, other):
[72] Fix | Delete
"""self + other"""
[73] Fix | Delete
raise NotImplementedError
[74] Fix | Delete
[75] Fix | Delete
@abstractmethod
[76] Fix | Delete
def __radd__(self, other):
[77] Fix | Delete
"""other + self"""
[78] Fix | Delete
raise NotImplementedError
[79] Fix | Delete
[80] Fix | Delete
@abstractmethod
[81] Fix | Delete
def __neg__(self):
[82] Fix | Delete
"""-self"""
[83] Fix | Delete
raise NotImplementedError
[84] Fix | Delete
[85] Fix | Delete
@abstractmethod
[86] Fix | Delete
def __pos__(self):
[87] Fix | Delete
"""+self"""
[88] Fix | Delete
raise NotImplementedError
[89] Fix | Delete
[90] Fix | Delete
def __sub__(self, other):
[91] Fix | Delete
"""self - other"""
[92] Fix | Delete
return self + -other
[93] Fix | Delete
[94] Fix | Delete
def __rsub__(self, other):
[95] Fix | Delete
"""other - self"""
[96] Fix | Delete
return -self + other
[97] Fix | Delete
[98] Fix | Delete
@abstractmethod
[99] Fix | Delete
def __mul__(self, other):
[100] Fix | Delete
"""self * other"""
[101] Fix | Delete
raise NotImplementedError
[102] Fix | Delete
[103] Fix | Delete
@abstractmethod
[104] Fix | Delete
def __rmul__(self, other):
[105] Fix | Delete
"""other * self"""
[106] Fix | Delete
raise NotImplementedError
[107] Fix | Delete
[108] Fix | Delete
@abstractmethod
[109] Fix | Delete
def __div__(self, other):
[110] Fix | Delete
"""self / other without __future__ division
[111] Fix | Delete
[112] Fix | Delete
May promote to float.
[113] Fix | Delete
"""
[114] Fix | Delete
raise NotImplementedError
[115] Fix | Delete
[116] Fix | Delete
@abstractmethod
[117] Fix | Delete
def __rdiv__(self, other):
[118] Fix | Delete
"""other / self without __future__ division"""
[119] Fix | Delete
raise NotImplementedError
[120] Fix | Delete
[121] Fix | Delete
@abstractmethod
[122] Fix | Delete
def __truediv__(self, other):
[123] Fix | Delete
"""self / other with __future__ division.
[124] Fix | Delete
[125] Fix | Delete
Should promote to float when necessary.
[126] Fix | Delete
"""
[127] Fix | Delete
raise NotImplementedError
[128] Fix | Delete
[129] Fix | Delete
@abstractmethod
[130] Fix | Delete
def __rtruediv__(self, other):
[131] Fix | Delete
"""other / self with __future__ division"""
[132] Fix | Delete
raise NotImplementedError
[133] Fix | Delete
[134] Fix | Delete
@abstractmethod
[135] Fix | Delete
def __pow__(self, exponent):
[136] Fix | Delete
"""self**exponent; should promote to float or complex when necessary."""
[137] Fix | Delete
raise NotImplementedError
[138] Fix | Delete
[139] Fix | Delete
@abstractmethod
[140] Fix | Delete
def __rpow__(self, base):
[141] Fix | Delete
"""base ** self"""
[142] Fix | Delete
raise NotImplementedError
[143] Fix | Delete
[144] Fix | Delete
@abstractmethod
[145] Fix | Delete
def __abs__(self):
[146] Fix | Delete
"""Returns the Real distance from 0. Called for abs(self)."""
[147] Fix | Delete
raise NotImplementedError
[148] Fix | Delete
[149] Fix | Delete
@abstractmethod
[150] Fix | Delete
def conjugate(self):
[151] Fix | Delete
"""(x+y*i).conjugate() returns (x-y*i)."""
[152] Fix | Delete
raise NotImplementedError
[153] Fix | Delete
[154] Fix | Delete
@abstractmethod
[155] Fix | Delete
def __eq__(self, other):
[156] Fix | Delete
"""self == other"""
[157] Fix | Delete
raise NotImplementedError
[158] Fix | Delete
[159] Fix | Delete
def __ne__(self, other):
[160] Fix | Delete
"""self != other"""
[161] Fix | Delete
# The default __ne__ doesn't negate __eq__ until 3.0.
[162] Fix | Delete
return not (self == other)
[163] Fix | Delete
[164] Fix | Delete
Complex.register(complex)
[165] Fix | Delete
[166] Fix | Delete
[167] Fix | Delete
class Real(Complex):
[168] Fix | Delete
"""To Complex, Real adds the operations that work on real numbers.
[169] Fix | Delete
[170] Fix | Delete
In short, those are: a conversion to float, trunc(), divmod,
[171] Fix | Delete
%, <, <=, >, and >=.
[172] Fix | Delete
[173] Fix | Delete
Real also provides defaults for the derived operations.
[174] Fix | Delete
"""
[175] Fix | Delete
[176] Fix | Delete
__slots__ = ()
[177] Fix | Delete
[178] Fix | Delete
@abstractmethod
[179] Fix | Delete
def __float__(self):
[180] Fix | Delete
"""Any Real can be converted to a native float object.
[181] Fix | Delete
[182] Fix | Delete
Called for float(self)."""
[183] Fix | Delete
raise NotImplementedError
[184] Fix | Delete
[185] Fix | Delete
@abstractmethod
[186] Fix | Delete
def __trunc__(self):
[187] Fix | Delete
"""trunc(self): Truncates self to an Integral.
[188] Fix | Delete
[189] Fix | Delete
Returns an Integral i such that:
[190] Fix | Delete
* i>0 iff self>0;
[191] Fix | Delete
* abs(i) <= abs(self);
[192] Fix | Delete
* for any Integral j satisfying the first two conditions,
[193] Fix | Delete
abs(i) >= abs(j) [i.e. i has "maximal" abs among those].
[194] Fix | Delete
i.e. "truncate towards 0".
[195] Fix | Delete
"""
[196] Fix | Delete
raise NotImplementedError
[197] Fix | Delete
[198] Fix | Delete
def __divmod__(self, other):
[199] Fix | Delete
"""divmod(self, other): The pair (self // other, self % other).
[200] Fix | Delete
[201] Fix | Delete
Sometimes this can be computed faster than the pair of
[202] Fix | Delete
operations.
[203] Fix | Delete
"""
[204] Fix | Delete
return (self // other, self % other)
[205] Fix | Delete
[206] Fix | Delete
def __rdivmod__(self, other):
[207] Fix | Delete
"""divmod(other, self): The pair (self // other, self % other).
[208] Fix | Delete
[209] Fix | Delete
Sometimes this can be computed faster than the pair of
[210] Fix | Delete
operations.
[211] Fix | Delete
"""
[212] Fix | Delete
return (other // self, other % self)
[213] Fix | Delete
[214] Fix | Delete
@abstractmethod
[215] Fix | Delete
def __floordiv__(self, other):
[216] Fix | Delete
"""self // other: The floor() of self/other."""
[217] Fix | Delete
raise NotImplementedError
[218] Fix | Delete
[219] Fix | Delete
@abstractmethod
[220] Fix | Delete
def __rfloordiv__(self, other):
[221] Fix | Delete
"""other // self: The floor() of other/self."""
[222] Fix | Delete
raise NotImplementedError
[223] Fix | Delete
[224] Fix | Delete
@abstractmethod
[225] Fix | Delete
def __mod__(self, other):
[226] Fix | Delete
"""self % other"""
[227] Fix | Delete
raise NotImplementedError
[228] Fix | Delete
[229] Fix | Delete
@abstractmethod
[230] Fix | Delete
def __rmod__(self, other):
[231] Fix | Delete
"""other % self"""
[232] Fix | Delete
raise NotImplementedError
[233] Fix | Delete
[234] Fix | Delete
@abstractmethod
[235] Fix | Delete
def __lt__(self, other):
[236] Fix | Delete
"""self < other
[237] Fix | Delete
[238] Fix | Delete
< on Reals defines a total ordering, except perhaps for NaN."""
[239] Fix | Delete
raise NotImplementedError
[240] Fix | Delete
[241] Fix | Delete
@abstractmethod
[242] Fix | Delete
def __le__(self, other):
[243] Fix | Delete
"""self <= other"""
[244] Fix | Delete
raise NotImplementedError
[245] Fix | Delete
[246] Fix | Delete
# Concrete implementations of Complex abstract methods.
[247] Fix | Delete
def __complex__(self):
[248] Fix | Delete
"""complex(self) == complex(float(self), 0)"""
[249] Fix | Delete
return complex(float(self))
[250] Fix | Delete
[251] Fix | Delete
@property
[252] Fix | Delete
def real(self):
[253] Fix | Delete
"""Real numbers are their real component."""
[254] Fix | Delete
return +self
[255] Fix | Delete
[256] Fix | Delete
@property
[257] Fix | Delete
def imag(self):
[258] Fix | Delete
"""Real numbers have no imaginary component."""
[259] Fix | Delete
return 0
[260] Fix | Delete
[261] Fix | Delete
def conjugate(self):
[262] Fix | Delete
"""Conjugate is a no-op for Reals."""
[263] Fix | Delete
return +self
[264] Fix | Delete
[265] Fix | Delete
Real.register(float)
[266] Fix | Delete
[267] Fix | Delete
[268] Fix | Delete
class Rational(Real):
[269] Fix | Delete
""".numerator and .denominator should be in lowest terms."""
[270] Fix | Delete
[271] Fix | Delete
__slots__ = ()
[272] Fix | Delete
[273] Fix | Delete
@abstractproperty
[274] Fix | Delete
def numerator(self):
[275] Fix | Delete
raise NotImplementedError
[276] Fix | Delete
[277] Fix | Delete
@abstractproperty
[278] Fix | Delete
def denominator(self):
[279] Fix | Delete
raise NotImplementedError
[280] Fix | Delete
[281] Fix | Delete
# Concrete implementation of Real's conversion to float.
[282] Fix | Delete
def __float__(self):
[283] Fix | Delete
"""float(self) = self.numerator / self.denominator
[284] Fix | Delete
[285] Fix | Delete
It's important that this conversion use the integer's "true"
[286] Fix | Delete
division rather than casting one side to float before dividing
[287] Fix | Delete
so that ratios of huge integers convert without overflowing.
[288] Fix | Delete
[289] Fix | Delete
"""
[290] Fix | Delete
return self.numerator / self.denominator
[291] Fix | Delete
[292] Fix | Delete
[293] Fix | Delete
class Integral(Rational):
[294] Fix | Delete
"""Integral adds a conversion to long and the bit-string operations."""
[295] Fix | Delete
[296] Fix | Delete
__slots__ = ()
[297] Fix | Delete
[298] Fix | Delete
@abstractmethod
[299] Fix | Delete
def __long__(self):
[300] Fix | Delete
"""long(self)"""
[301] Fix | Delete
raise NotImplementedError
[302] Fix | Delete
[303] Fix | Delete
def __index__(self):
[304] Fix | Delete
"""Called whenever an index is needed, such as in slicing"""
[305] Fix | Delete
return long(self)
[306] Fix | Delete
[307] Fix | Delete
@abstractmethod
[308] Fix | Delete
def __pow__(self, exponent, modulus=None):
[309] Fix | Delete
"""self ** exponent % modulus, but maybe faster.
[310] Fix | Delete
[311] Fix | Delete
Accept the modulus argument if you want to support the
[312] Fix | Delete
3-argument version of pow(). Raise a TypeError if exponent < 0
[313] Fix | Delete
or any argument isn't Integral. Otherwise, just implement the
[314] Fix | Delete
2-argument version described in Complex.
[315] Fix | Delete
"""
[316] Fix | Delete
raise NotImplementedError
[317] Fix | Delete
[318] Fix | Delete
@abstractmethod
[319] Fix | Delete
def __lshift__(self, other):
[320] Fix | Delete
"""self << other"""
[321] Fix | Delete
raise NotImplementedError
[322] Fix | Delete
[323] Fix | Delete
@abstractmethod
[324] Fix | Delete
def __rlshift__(self, other):
[325] Fix | Delete
"""other << self"""
[326] Fix | Delete
raise NotImplementedError
[327] Fix | Delete
[328] Fix | Delete
@abstractmethod
[329] Fix | Delete
def __rshift__(self, other):
[330] Fix | Delete
"""self >> other"""
[331] Fix | Delete
raise NotImplementedError
[332] Fix | Delete
[333] Fix | Delete
@abstractmethod
[334] Fix | Delete
def __rrshift__(self, other):
[335] Fix | Delete
"""other >> self"""
[336] Fix | Delete
raise NotImplementedError
[337] Fix | Delete
[338] Fix | Delete
@abstractmethod
[339] Fix | Delete
def __and__(self, other):
[340] Fix | Delete
"""self & other"""
[341] Fix | Delete
raise NotImplementedError
[342] Fix | Delete
[343] Fix | Delete
@abstractmethod
[344] Fix | Delete
def __rand__(self, other):
[345] Fix | Delete
"""other & self"""
[346] Fix | Delete
raise NotImplementedError
[347] Fix | Delete
[348] Fix | Delete
@abstractmethod
[349] Fix | Delete
def __xor__(self, other):
[350] Fix | Delete
"""self ^ other"""
[351] Fix | Delete
raise NotImplementedError
[352] Fix | Delete
[353] Fix | Delete
@abstractmethod
[354] Fix | Delete
def __rxor__(self, other):
[355] Fix | Delete
"""other ^ self"""
[356] Fix | Delete
raise NotImplementedError
[357] Fix | Delete
[358] Fix | Delete
@abstractmethod
[359] Fix | Delete
def __or__(self, other):
[360] Fix | Delete
"""self | other"""
[361] Fix | Delete
raise NotImplementedError
[362] Fix | Delete
[363] Fix | Delete
@abstractmethod
[364] Fix | Delete
def __ror__(self, other):
[365] Fix | Delete
"""other | self"""
[366] Fix | Delete
raise NotImplementedError
[367] Fix | Delete
[368] Fix | Delete
@abstractmethod
[369] Fix | Delete
def __invert__(self):
[370] Fix | Delete
"""~self"""
[371] Fix | Delete
raise NotImplementedError
[372] Fix | Delete
[373] Fix | Delete
# Concrete implementations of Rational and Real abstract methods.
[374] Fix | Delete
def __float__(self):
[375] Fix | Delete
"""float(self) == float(long(self))"""
[376] Fix | Delete
return float(long(self))
[377] Fix | Delete
[378] Fix | Delete
@property
[379] Fix | Delete
def numerator(self):
[380] Fix | Delete
"""Integers are their own numerators."""
[381] Fix | Delete
return +self
[382] Fix | Delete
[383] Fix | Delete
@property
[384] Fix | Delete
def denominator(self):
[385] Fix | Delete
"""Integers have a denominator of 1."""
[386] Fix | Delete
return 1
[387] Fix | Delete
[388] Fix | Delete
Integral.register(int)
[389] Fix | Delete
Integral.register(long)
[390] Fix | Delete
[391] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function