Access to formatting controls from within __repr__ or __str__?

D

Dan Sommers

Hi,

I have a class whose objects represent physical quantities including
uncertainties and units, and I would like more control over the way they
print.

I have a __str__ method which outputs the quantity and its uncertainty,
properly rounded (i.e. the uncertainty to one digit and the quantity to
the same precision as the uncertainty), followed by the units (mostly)
as they accumulated during whatever calculations led to the quantity's
current value. Inside this method, I round the floating point values
appropriately and depend on '%s' to do the Right Thing with the floating
point numbers.

I have a __repr__ method that outputs the three parts in such a way that
eval( repr( v ) ) returns a quantity as equal to v as possible.

I would like to add some other options, e.g., an SI mode that prints the
numbers and units according to SI guidelines, and a LaTeX option that
prints something I can copy and paste into a LaTeX document that uses
the SIunits and/or SIstyle packages and/or my own related macros.

So my question is: Is there a way to pass options "through" a format
string to the __str__ and __repr__ functions? For example, can I define
my own alternate form for use with the '#' formatting character, so that
'%#s' generates output according to SI guidelines?

I know I can always write the formatting methods separately and call
them explicitly, but IMO that's not as clean as using the format control
codes. A collection of "set output format" methods is possible, but
that solution makes me cringe.

Thank you,
Dan
 
S

Serge Orlov

Dan said:
So my question is: Is there a way to pass options "through" a format
string to the __str__ and __repr__ functions? For example, can I
define my own alternate form for use with the '#' formatting
character, so that '%#s' generates output according to SI guidelines?

You can create your own class FmtTemplate like string.Template was done
in python 2.4: http://docs.python.org/lib/node105.html

and have it call obj.__str__("#s") when you use ${obj:#s} format. I'm
not sure why you want to pass format to repr (as far as I understand
repr is mostly for debug purposes) but you can call from template as
${obj:r} --> obj.__repr__()
${obj:#r} --> obj.__repr__("#r")

fmt = FmtTemplate("quantity1: ${q1:#s}, quantity2: ${q2:#s}")
q1=MyQuantity(...)
q2=MyQuantity(...)
print fmt.substitute(q1=q1,q2=q2)

Serge.
 
S

Serge Orlov

Dan said:
That looks interesting. Perhaps that will push me into upgrading to
2.4 sooner rather than later (I'm *not* looking forward to rebuilding
all my extensions...).

You don't need to upgrade to Python 2.4. string.Template is a pure
Python code (about 130 lines) and it looks like it's Python 2.2
compatible. My suggestion was to grab it and hack it to add the
feature you want and then keep it in your own library.

Serge.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top