[reportlab-users] What arguments does fp_str expect?

Robin Becker robin at reportlab.com
Tue Mar 24 12:55:22 EDT 2009


Peter wrote:

> Hi all,

>

> Earlier today, I had a problem in the fp_str function in

> reportlab/lib/utils.py where it was being passed three floats held as

> strings (perhaps an RGB color tuple, I have not checked yet). The

> root problem may have been elsewhere (possibly in my own code), but it

> would help to know what arguments this function expects. There is no

> docstring, but the comment at the top suggests string arguments might

> be expected.


fp_str is supposed to convert floats/ints into strings. It's not supposed to be
able to convert arbitrary objects (including strings). Probably all this ought
to disappear now that we have fixed point. The older versions of acrobat reader
used 16.16 fixed point arithmetic so the precision required in the files was
strictly limited.

The *a argument in the python version indicates that we convert (or expect to
convert) more than one value. As a special feature if there is only one argument
and that argument is of type list/sequence then we make the arguments the
contents of a[0].



>

> This sample code shows the issue I am having. This is what I expected

> to happen:

>

>>>> from reportlab.lib.utils import fp_str

>>>> fp_str([0.123, 1.23, 12.3])

> '.123 1.23 12.3'

>>>> fp_str((0.123, 1.23, 12.3))

> '.123 1.23 12.3'

>>>> fp_str(["0.123", "1.23", "12.3"])

> '.123 1.23 12.3'

>>>> fp_str(("0.123", "1.23", "12.3"))

> '.123 1.23 12.3'

>

> However, the last two examples fail with a TypeError from the abs line

> (see below):

>

>>>> abs("0.123")

> Traceback (most recent call last):

> File "<stdin>", line 1, in <module>

> TypeError: bad operand type for abs(): 'str'

>

> This is my copy of the relevant part of reportlab/lib/utils.py, with

> two lines of

> code changed to include a float() conversion. Otherwise it looks the same

> as that in SVN I think:

> http://svn.reportlab.com/cgi-bin/viewvc.cgi/public/reportlab/trunk/src/reportlab/lib/utils.py?view=log

>

> try:

> #raise ImportError

> ### NOTE! FP_STR SHOULD PROBABLY ALWAYS DO A PYTHON STR()

> CONVERSION ON ARGS

> ### IN CASE THEY ARE "LAZY OBJECTS". ACCELLERATOR DOESN'T DO THIS (YET)

> try:

> from _rl_accel import fp_str # in case of builtin version

> except ImportError:

> from reportlab.lib._rl_accel import fp_str # specific

> except ImportError:

> from math import log

> _log_10 = lambda x,log=log,_log_e_10=log(10.0): log(x)/_log_e_10

> _fp_fmts = "%.0f", "%.1f", "%.2f", "%.3f", "%.4f", "%.5f", "%.6f"

> import re

> _tz_re = re.compile('0+$')

> del re

> def fp_str(*a):

> """Returns a space separated string of the given numbers."""

> #PJC: extend this please

> if len(a)==1 and isSeqType(a[0]): a = a[0]

> s = []

> A = s.append

> for i in a:

> sa =abs(float(i)) #PJC change to add float

> if sa<=1e-7: A('0')

> else:

> l = sa<=1 and 6 or min(max(0,(6-int(_log_10(sa)))),6)

> n = _fp_fmts[l]%float(i) #PJC change to add float

> if l:

> n = _tz_re.sub('',n)

> try:

> if n[-1]=='.': n = n[:-1]

> except:

> print i, n

> raise

> A((n[0]!='0' or len(n)==1) and n or n[1:])

> return string.join(s)

>

>

> So, should the fp_str function cope with a sequence of numbers as

> strings (i.e. a list or tuple of strings)? If so, is my simple minded

> fix appropriate for when the _rl_accel module isn't being used?

>

> If not, then I will have to digg a little deeper into my problem to

> work out why I have a list/tuple of strings instead of a list/tuple of

> floats.

>

> Thanks,

>

> Peter

> _______________________________________________

> reportlab-users mailing list

> reportlab-users at reportlab.com

> http://two.pairlist.net/mailman/listinfo/reportlab-users

>



--
Robin Becker


More information about the reportlab-users mailing list