[reportlab-users] What arguments does fp_str expect?
Peter
peter at maubp.freeserve.co.uk
Tue Mar 24 13:32:29 EDT 2009
On Tue, Mar 24, 2009 at 4:55 PM, Robin Becker <robin at reportlab.com> wrote:
>
> 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].
OK - thanks!
That means the error I was seeing was triggered elsewhere. I'll have
to investigate further...
Could you update the fp_str function's docstring with this information?
It took me a while to work out what the code was actually doing. My
instinct would have been something more like this:
def fp_str(*a):
if len(a)==1 and isSeqType(a[0]): a = a[0]
s = []
A = s.append
for value in a:
if abs(value) <= 1e-6:
A('0')
else:
#Can use at most 6 dp, don't want leading or trailing zeros
A(('%.6f' % value).strip("0"))
return " ".join(s)
assert fp_str([0.000000123, 0.00000123, 0.0000123, 0.000123 , 0.123,
1.23, 12.3, -12.3]) \
== '0 .000001 .000012 .000123 .123 1.23 12.3 -12.3'
Presumably your somewhat more cryptic version is much faster, and
avoids international floating point issues (comma versus period)?
As an aside, utils.pyt contains several uses of string.join(a_list,
a_sep) rather than a_sep.join(a_list), which might be worth fixing.
Peter
More information about the reportlab-users
mailing list