Files
tryton/trytond/tools/decimal_.py
T
Cédric Krier 4cf1624a91 Make decistmt a tool
issue4273
review9801002
2014-10-27 23:12:45 +01:00

37 lines
1.1 KiB
Python

# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import tokenize
from io import StringIO
# code snippet taken from http://docs.python.org/library/tokenize.html
def decistmt(s):
"""Substitute Decimals for floats in a string of statements.
>>> from decimal import Decimal
>>> s = 'print +21.3e-5*-.1234/81.7'
>>> decistmt(s)
u"print +Decimal (u'21.3e-5')*-Decimal (u'.1234')/Decimal (u'81.7')"
>>> exec(s)
-3.21716034272e-07
>>> exec(decistmt(s))
-3.217160342717258261933904529E-7
"""
result = []
# tokenize the string
g = tokenize.generate_tokens(StringIO(s.decode('utf-8')).readline)
for toknum, tokval, _, _, _ in g:
# replace NUMBER tokens
if toknum == tokenize.NUMBER and '.' in tokval:
result.extend([
(tokenize.NAME, 'Decimal'),
(tokenize.OP, '('),
(tokenize.STRING, repr(tokval)),
(tokenize.OP, ')')
])
else:
result.append((toknum, tokval))
return tokenize.untokenize(result)