468,773 Members | 2,335 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,773 developers. It's quick & easy.

Access to formatting controls from within __repr__ or __str__?

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

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Never play leapfrog with a unicorn.
Jul 18 '05 #1
3 1627
Dan Sommers wrote:
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.

Jul 18 '05 #2
On 18 Feb 2005 01:25:06 -0800,
"Serge Orlov" <Se*********@gmail.com> wrote:
Dan Sommers wrote:
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


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...).

Thanks you,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Never play leapfrog with a unicorn.
Jul 18 '05 #3
Dan Sommers wrote:
On 18 Feb 2005 01:25:06 -0800,
"Serge Orlov" <Se*********@gmail.com> wrote:
Dan Sommers wrote:

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


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.
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Jim Newton | last post: by
2 posts views Thread by could ildg | last post: by
11 posts views Thread by Steve Holden | last post: by
15 posts views Thread by Jan Danielsson | last post: by
7 posts views Thread by Ben Finney | last post: by
3 posts views Thread by Russ | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.