473,396 Members | 1,933 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,396 software developers and data experts.

Making Variable Text Output More Pythonic?

Hi,

I have some classes that print variable outputs depending on their
internal state, like so:

def __str__(self):
out = []
if self.opt1: out += ['option 1 is %s' % self.opt1']
if self.opt2: out += ['option 2 is %s' % self.opt2']
....
return '\n'.join(out)

Is there any way to make this cleaner?
Jun 27 '08 #1
4 909
Casey <ca***********@gmail.comwrites:
Hi,

I have some classes that print variable outputs depending on their
internal state, like so:

def __str__(self):
out = []
if self.opt1: out += ['option 1 is %s' % self.opt1']
if self.opt2: out += ['option 2 is %s' % self.opt2']
....
return '\n'.join(out)

Is there any way to make this cleaner?
Maybe.

Have a dictionary of options rather than individual attributes;
options not in the dictionary are not set. E.g.

mask = {
'opt1': 'option 1 is %s',
'opt2': 'option 2 is %s',
...
}

def __str__(self):
return '\n'.join(mask[o] % v for o,v in self.options.iteritems())

--
Arnaud
Jun 27 '08 #2
Arnaud's code wont work if self.opt1 is None, an empty list, an empty
tuple, False, etc, because all these evaluate to false. They wont
print the internal state of these variables. [Just an informational
notice, this may be the behavior you expect]

Secondly, I'm not sure if you know the variable names from before hand
in which case Casey's approach will work, or you need to know them via
introspection. http://www.ibm.com/developerworks/library/l-pyint.html
[Scroll down to attributes].

On May 16, 1:44*am, Arnaud Delobelle <arno...@googlemail.comwrote:
Casey <casey.mcgi...@gmail.comwrites:
Hi,
I have some classes that print variable outputs depending on their
internal state, like so:
def __str__(self):
* * out = []
* * if self.opt1: out += ['option 1 is %s' % self.opt1']
* * if self.opt2: out += ['option 2 is %s' % self.opt2']
* * ....
* * return '\n'.join(out)
Is there any way to make this cleaner?

Maybe.

Have a dictionary of options rather than individual attributes;
options not in the dictionary are not set. E.g.

mask = {
* * 'opt1': 'option 1 is %s',
* * 'opt2': 'option 2 is %s',
* * ...
* * }

def __str__(self):
* * return '\n'.join(mask[o] % v for o,v in self.options.iteritems())

--
Arnaud
Jun 27 '08 #3
afrobeard <af*******@gmail.comwrites:
Arnaud's code wont work if self.opt1 is None, an empty list, an empty
tuple, False, etc, because all these evaluate to false. They wont
print the internal state of these variables. [Just an informational
notice, this may be the behavior you expect]
??? My suggestion is to get rid of attributes altogether and does not
test any truth values.
Secondly, I'm not sure if you know the variable names from before hand
in which case Casey's approach will work, or you need to know them via
introspection. http://www.ibm.com/developerworks/library/l-pyint.html
[Scroll down to attributes].

On May 16, 1:44*am, Arnaud Delobelle <arno...@googlemail.comwrote:
>Casey <casey.mcgi...@gmail.comwrites:
Hi,
I have some classes that print variable outputs depending on their
internal state, like so:
def __str__(self):
* * out = []
* * if self.opt1: out += ['option 1 is %s' % self.opt1']
* * if self.opt2: out += ['option 2 is %s' % self.opt2']
* * ....
* * return '\n'.join(out)
Is there any way to make this cleaner?

Maybe.

Have a dictionary of options rather than individual attributes;
options not in the dictionary are not set. E.g.

mask = {
* * 'opt1': 'option 1 is %s',
* * 'opt2': 'option 2 is %s',
* * ...
* * }

def __str__(self):
* * return '\n'.join(mask[o] % v for o,v in self.options.iteritems())

--
Arnaud
Jun 27 '08 #4
I-T
A thousand apologies for my ignorance. I'll try not to get names mixed
up again in the future.

On May 16, 8:42*pm, Arnaud Delobelle <arno...@googlemail.comwrote:
afrobeard <afrobe...@gmail.comwrites:
Arnaud's code wont work if self.opt1 is None, an empty list, an empty
tuple, False, etc, because all these evaluate to false. They wont
print the internal state of these variables. [Just an informational
notice, this may be the behavior you expect]

??? My suggestion is to get rid of attributes altogether and does not
test any truth values.
Secondly, I'm not sure if you know the variable names from before hand
in which case Casey's approach will work, or you need to know them via
introspection.http://www.ibm.com/developerworks/library/l-pyint.html
[Scroll down to attributes].
On May 16, 1:44*am, Arnaud Delobelle <arno...@googlemail.comwrote:
Casey <casey.mcgi...@gmail.comwrites:
Hi,
I have some classes that print variable outputs depending on their
internal state, like so:
def __str__(self):
* * out = []
* * if self.opt1: out += ['option 1 is %s' % self.opt1']
* * if self.opt2: out += ['option 2 is %s' % self.opt2']
* * ....
* * return '\n'.join(out)
Is there any way to make this cleaner?
Maybe.
Have a dictionary of options rather than individual attributes;
options not in the dictionary are not set. E.g.
mask = {
* * 'opt1': 'option 1 is %s',
* * 'opt2': 'option 2 is %s',
* * ...
* * }
def __str__(self):
* * return '\n'.join(mask[o] % v for o,v in self.options.iteritems())
--
Arnaud
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark Wilson CPU | last post by:
This must be easy, but I'm missing something... I want to execute a Perl script, and capture ALL its output into a PHP variable. Here are my 2 files: -------------------------------------...
5
by: rkmr.em | last post by:
Hi I create a variable in a decorator. i want to be able to access that variable in the function to be decorated. How to do this? thanks
16
by: Andrea Gavana | last post by:
Hi Diez & All, Do you mind explaining "why" you find it *buttugly*? I am asking just out of curiosity, obviously. I am so biased towards wxPython that I won't make any comment on this thread...
10
by: Jason | last post by:
I'm making a program that will convert decimal inputs (in this case, in inches) and output a fractional answer. At the moment, I'm only able to output the fractional answer in three parts: A whole...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.