Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/python27/lib64/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'^([-+]?)(\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
intpart = intpart.lstrip('0');
[43] Fix | Delete
if sign == '+': sign = ''
[44] Fix | Delete
if fraction: fraction = fraction[1:]
[45] Fix | Delete
if exppart: expo = int(exppart[1:])
[46] Fix | Delete
else: expo = 0
[47] Fix | Delete
return sign, intpart, fraction, expo
[48] Fix | Delete
[49] Fix | Delete
def unexpo(intpart, fraction, expo):
[50] Fix | Delete
"""Remove the exponent by changing intpart and fraction."""
[51] Fix | Delete
if expo > 0: # Move the point left
[52] Fix | Delete
f = len(fraction)
[53] Fix | Delete
intpart, fraction = intpart + fraction[:expo], fraction[expo:]
[54] Fix | Delete
if expo > f:
[55] Fix | Delete
intpart = intpart + '0'*(expo-f)
[56] Fix | Delete
elif expo < 0: # Move the point right
[57] Fix | Delete
i = len(intpart)
[58] Fix | Delete
intpart, fraction = intpart[:expo], intpart[expo:] + fraction
[59] Fix | Delete
if expo < -i:
[60] Fix | Delete
fraction = '0'*(-expo-i) + fraction
[61] Fix | Delete
return intpart, fraction
[62] Fix | Delete
[63] Fix | Delete
def roundfrac(intpart, fraction, digs):
[64] Fix | Delete
"""Round or extend the fraction to size digs."""
[65] Fix | Delete
f = len(fraction)
[66] Fix | Delete
if f <= digs:
[67] Fix | Delete
return intpart, fraction + '0'*(digs-f)
[68] Fix | Delete
i = len(intpart)
[69] Fix | Delete
if i+digs < 0:
[70] Fix | Delete
return '0'*-digs, ''
[71] Fix | Delete
total = intpart + fraction
[72] Fix | Delete
nextdigit = total[i+digs]
[73] Fix | Delete
if nextdigit >= '5': # Hard case: increment last digit, may have carry!
[74] Fix | Delete
n = i + digs - 1
[75] Fix | Delete
while n >= 0:
[76] Fix | Delete
if total[n] != '9': break
[77] Fix | Delete
n = n-1
[78] Fix | Delete
else:
[79] Fix | Delete
total = '0' + total
[80] Fix | Delete
i = i+1
[81] Fix | Delete
n = 0
[82] Fix | Delete
total = total[:n] + chr(ord(total[n]) + 1) + '0'*(len(total)-n-1)
[83] Fix | Delete
intpart, fraction = total[:i], total[i:]
[84] Fix | Delete
if digs >= 0:
[85] Fix | Delete
return intpart, fraction[:digs]
[86] Fix | Delete
else:
[87] Fix | Delete
return intpart[:digs] + '0'*-digs, ''
[88] Fix | Delete
[89] Fix | Delete
def fix(x, digs):
[90] Fix | Delete
"""Format x as [-]ddd.ddd with 'digs' digits after the point
[91] Fix | Delete
and at least one digit before.
[92] Fix | Delete
If digs <= 0, the point is suppressed."""
[93] Fix | Delete
if type(x) != type(''): x = repr(x)
[94] Fix | Delete
try:
[95] Fix | Delete
sign, intpart, fraction, expo = extract(x)
[96] Fix | Delete
except NotANumber:
[97] Fix | Delete
return x
[98] Fix | Delete
intpart, fraction = unexpo(intpart, fraction, expo)
[99] Fix | Delete
intpart, fraction = roundfrac(intpart, fraction, digs)
[100] Fix | Delete
while intpart and intpart[0] == '0': intpart = intpart[1:]
[101] Fix | Delete
if intpart == '': intpart = '0'
[102] Fix | Delete
if digs > 0: return sign + intpart + '.' + fraction
[103] Fix | Delete
else: return sign + intpart
[104] Fix | Delete
[105] Fix | Delete
def sci(x, digs):
[106] Fix | Delete
"""Format x as [-]d.dddE[+-]ddd with 'digs' digits after the point
[107] Fix | Delete
and exactly one digit before.
[108] Fix | Delete
If digs is <= 0, one digit is kept and the point is suppressed."""
[109] Fix | Delete
if type(x) != type(''): x = repr(x)
[110] Fix | Delete
sign, intpart, fraction, expo = extract(x)
[111] Fix | Delete
if not intpart:
[112] Fix | Delete
while fraction and fraction[0] == '0':
[113] Fix | Delete
fraction = fraction[1:]
[114] Fix | Delete
expo = expo - 1
[115] Fix | Delete
if fraction:
[116] Fix | Delete
intpart, fraction = fraction[0], fraction[1:]
[117] Fix | Delete
expo = expo - 1
[118] Fix | Delete
else:
[119] Fix | Delete
intpart = '0'
[120] Fix | Delete
else:
[121] Fix | Delete
expo = expo + len(intpart) - 1
[122] Fix | Delete
intpart, fraction = intpart[0], intpart[1:] + fraction
[123] Fix | Delete
digs = max(0, digs)
[124] Fix | Delete
intpart, fraction = roundfrac(intpart, fraction, digs)
[125] Fix | Delete
if len(intpart) > 1:
[126] Fix | Delete
intpart, fraction, expo = \
[127] Fix | Delete
intpart[0], intpart[1:] + fraction[:-1], \
[128] Fix | Delete
expo + len(intpart) - 1
[129] Fix | Delete
s = sign + intpart
[130] Fix | Delete
if digs > 0: s = s + '.' + fraction
[131] Fix | Delete
e = repr(abs(expo))
[132] Fix | Delete
e = '0'*(3-len(e)) + e
[133] Fix | Delete
if expo < 0: e = '-' + e
[134] Fix | Delete
else: e = '+' + e
[135] Fix | Delete
return s + 'e' + e
[136] Fix | Delete
[137] Fix | Delete
def test():
[138] Fix | Delete
"""Interactive test run."""
[139] Fix | Delete
try:
[140] Fix | Delete
while 1:
[141] Fix | Delete
x, digs = input('Enter (x, digs): ')
[142] Fix | Delete
print x, fix(x, digs), sci(x, digs)
[143] Fix | Delete
except (EOFError, KeyboardInterrupt):
[144] Fix | Delete
pass
[145] Fix | Delete
[146] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function