473,378 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 1804
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Jim Newton | last post by:
hi all, does anyone know what print does if there is no __str__ method? i'm trying ot override the __repr__. If anyone can give me some advice it would be great to have. I have defined a...
2
by: could ildg | last post by:
What's the difference between __repr__ and __str__? When will __repr__ be useful?
11
by: Steve Holden | last post by:
I was messing about with formatting and realized that the right kind of object could quite easily tell me exactly what accesses are made to the mapping in a string % mapping operation. This is a...
15
by: Jan Danielsson | last post by:
Sorry, but I Just Don't Get It. I did search the 'net, I did read the FAQ, but I'm too dumb to understand. As far as I can gather, __str__ is just a representation of the object. For instance: ...
7
by: Ben Finney | last post by:
Howdy all, The builtin types have __repr__ attributes that return something nice, that looks like the syntax one would use to create that particular instance. The default __repr__ for custom...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
3
by: Russ | last post by:
I'd like to get output formatting for my own classes that mimics the built-in output formatting. For example, >>> x = 4.54 >>> print "%4.2f" % x 4.54 In other words, if I substitute a class...
16
by: Neil | last post by:
I posted a few days ago that it seems to me that the Access 2007 rich text feature does not support: a) full text justification; b) programmatic manipulation. I was hoping that someone might...
7
by: Roc Zhou | last post by:
Now I have to design a class that overload __getattr__, but after that, I found the __repr__ have been affected. This is a simple example model: #!/usr/bin/env python class test: def...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.