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