Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
File: fpformat.py
"""General floating point formatting functions.
[0] Fix | Delete
[1] Fix | Delete
Functions:
[2] Fix | Delete
fix(x, digits_behind)
[3] Fix | Delete
sci(x, digits_behind)
[4] Fix | Delete
[5] Fix | Delete
Each takes a number or a string and a number of digits as arguments.
[6] Fix | Delete
[7] Fix | Delete
Parameters:
[8] Fix | Delete
x: number to be formatted; or a string resembling a number
[9] Fix | Delete
digits_behind: number of digits behind the decimal point
[10] Fix | Delete
"""
[11] Fix | Delete
from warnings import warnpy3k
[12] Fix | Delete
warnpy3k("the fpformat module has been removed in Python 3.0", stacklevel=2)
[13] Fix | Delete
del warnpy3k
[14] Fix | Delete
[15] Fix | Delete
import re
[16] Fix | Delete
[17] Fix | Delete
__all__ = ["fix","sci","NotANumber"]
[18] Fix | Delete
[19] Fix | Delete
# Compiled regular expression to "decode" a number
[20] Fix | Delete
decoder = re.compile(r'^([-+]?)0*(\d*)((?:\.\d*)?)(([eE][-+]?\d+)?)$')
[21] Fix | Delete
# \0 the whole thing
[22] Fix | Delete
# \1 leading sign or empty
[23] Fix | Delete
# \2 digits left of decimal point
[24] Fix | Delete
# \3 fraction (empty or begins with point)
[25] Fix | Delete
# \4 exponent part (empty or begins with 'e' or 'E')
[26] Fix | Delete
[27] Fix | Delete
try:
[28] Fix | Delete
class NotANumber(ValueError):
[29] Fix | Delete
pass
[30] Fix | Delete
except TypeError:
[31] Fix | Delete
NotANumber = 'fpformat.NotANumber'
[32] Fix | Delete
[33] Fix | Delete
def extract(s):
[34] Fix | Delete
"""Return (sign, intpart, fraction, expo) or raise an exception:
[35] Fix | Delete
sign is '+' or '-'
[36] Fix | Delete
intpart is 0 or more digits beginning with a nonzero
[37] Fix | Delete
fraction is 0 or more digits
[38] Fix | Delete
expo is an integer"""
[39] Fix | Delete
res = decoder.match(s)
[40] Fix | Delete
if res is None: raise NotANumber, s
[41] Fix | Delete
sign, intpart, fraction, exppart = res.group(1,2,3,4)
[42] Fix | Delete
if sign == '+': sign = ''
[43] Fix | Delete
if fraction: fraction = fraction[1:]
[44] Fix | Delete
if exppart: expo = int(exppart[1:])
[45] Fix | Delete
else: expo = 0
[46] Fix | Delete
return sign, intpart, fraction, expo
[47] Fix | Delete
[48] Fix | Delete
def unexpo(intpart, fraction, expo):
[49] Fix | Delete
"""Remove the exponent by changing intpart and fraction."""
[50] Fix | Delete
if expo > 0: # Move the point left
[51] Fix | Delete
f = len(fraction)
[52] Fix | Delete
intpart, fraction = intpart + fraction[:expo], fraction[expo:]
[53] Fix | Delete
if expo > f:
[54] Fix | Delete
intpart = intpart + '0'*(expo-f)
[55] Fix | Delete
elif expo < 0: # Move the point right
[56] Fix | Delete
i = len(intpart)
[57] Fix | Delete
intpart, fraction = intpart[:expo], intpart[expo:] + fraction
[58] Fix | Delete
if expo < -i:
[59] Fix | Delete
fraction = '0'*(-expo-i) + fraction
[60] Fix | Delete
return intpart, fraction
[61] Fix | Delete
[62] Fix | Delete
def roundfrac(intpart, fraction, digs):
[63] Fix | Delete
"""Round or extend the fraction to size digs."""
[64] Fix | Delete
f = len(fraction)
[65] Fix | Delete
if f <= digs:
[66] Fix | Delete
return intpart, fraction + '0'*(digs-f)
[67] Fix | Delete
i = len(intpart)
[68] Fix | Delete
if i+digs < 0:
[69] Fix | Delete
return '0'*-digs, ''
[70] Fix | Delete
total = intpart + fraction
[71] Fix | Delete
nextdigit = total[i+digs]
[72] Fix | Delete
if nextdigit >= '5': # Hard case: increment last digit, may have carry!
[73] Fix | Delete
n = i + digs - 1
[74] Fix | Delete
while n >= 0:
[75] Fix | Delete
if total[n] != '9': break
[76] Fix | Delete
n = n-1
[77] Fix | Delete
else:
[78] Fix | Delete
total = '0' + total
[79] Fix | Delete
i = i+1
[80] Fix | Delete
n = 0
[81] Fix | Delete
total = total[:n] + chr(ord(total[n]) + 1) + '0'*(len(total)-n-1)
[82] Fix | Delete
intpart, fraction = total[:i], total[i:]
[83] Fix | Delete
if digs >= 0:
[84] Fix | Delete
return intpart, fraction[:digs]
[85] Fix | Delete
else:
[86] Fix | Delete
return intpart[:digs] + '0'*-digs, ''
[87] Fix | Delete
[88] Fix | Delete
def fix(x, digs):
[89] Fix | Delete
"""Format x as [-]ddd.ddd with 'digs' digits after the point
[90] Fix | Delete
and at least one digit before.
[91] Fix | Delete
If digs <= 0, the point is suppressed."""
[92] Fix | Delete
if type(x) != type(''): x = repr(x)
[93] Fix | Delete
try:
[94] Fix | Delete
sign, intpart, fraction, expo = extract(x)
[95] Fix | Delete
except NotANumber:
[96] Fix | Delete
return x
[97] Fix | Delete
intpart, fraction = unexpo(intpart, fraction, expo)
[98] Fix | Delete
intpart, fraction = roundfrac(intpart, fraction, digs)
[99] Fix | Delete
while intpart and intpart[0] == '0': intpart = intpart[1:]
[100] Fix | Delete
if intpart == '': intpart = '0'
[101] Fix | Delete
if digs > 0: return sign + intpart + '.' + fraction
[102] Fix | Delete
else: return sign + intpart
[103] Fix | Delete
[104] Fix | Delete
def sci(x, digs):
[105] Fix | Delete
"""Format x as [-]d.dddE[+-]ddd with 'digs' digits after the point
[106] Fix | Delete
and exactly one digit before.
[107] Fix | Delete
If digs is <= 0, one digit is kept and the point is suppressed."""
[108] Fix | Delete
if type(x) != type(''): x = repr(x)
[109] Fix | Delete
sign, intpart, fraction, expo = extract(x)
[110] Fix | Delete
if not intpart:
[111] Fix | Delete
while fraction and fraction[0] == '0':
[112] Fix | Delete
fraction = fraction[1:]
[113] Fix | Delete
expo = expo - 1
[114] Fix | Delete
if fraction:
[115] Fix | Delete
intpart, fraction = fraction[0], fraction[1:]
[116] Fix | Delete
expo = expo - 1
[117] Fix | Delete
else:
[118] Fix | Delete
intpart = '0'
[119] Fix | Delete
else:
[120] Fix | Delete
expo = expo + len(intpart) - 1
[121] Fix | Delete
intpart, fraction = intpart[0], intpart[1:] + fraction
[122] Fix | Delete
digs = max(0, digs)
[123] Fix | Delete
intpart, fraction = roundfrac(intpart, fraction, digs)
[124] Fix | Delete
if len(intpart) > 1:
[125] Fix | Delete
intpart, fraction, expo = \
[126] Fix | Delete
intpart[0], intpart[1:] + fraction[:-1], \
[127] Fix | Delete
expo + len(intpart) - 1
[128] Fix | Delete
s = sign + intpart
[129] Fix | Delete
if digs > 0: s = s + '.' + fraction
[130] Fix | Delete
e = repr(abs(expo))
[131] Fix | Delete
e = '0'*(3-len(e)) + e
[132] Fix | Delete
if expo < 0: e = '-' + e
[133] Fix | Delete
else: e = '+' + e
[134] Fix | Delete
return s + 'e' + e
[135] Fix | Delete
[136] Fix | Delete
def test():
[137] Fix | Delete
"""Interactive test run."""
[138] Fix | Delete
try:
[139] Fix | Delete
while 1:
[140] Fix | Delete
x, digs = input('Enter (x, digs): ')
[141] Fix | Delete
print x, fix(x, digs), sci(x, digs)
[142] Fix | Delete
except (EOFError, KeyboardInterrupt):
[143] Fix | Delete
pass
[144] Fix | Delete
[145] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function