Enum class with ToString functionality

  • Thread starter Bruno Desthuilliers
  • Start date
J

J. Cliff Dyer

Zara said:
You should implement __str__ (or __repr__) method in your class,

class TestOutcomes:
PASSED = 0
FAILED = 1
ABORTED = 2

def __str__(self):
textResultAsString="Unknown"
if testResult == TestOutcomes.PASSED:
testResultAsString = "Passed"
elif testResult == TestOutcomes.FAILED :
testResultAsString = "Failed"
else:
testResultAsString = "Aborted"
return testResultAsString

Regards,

Zara
This code cannot output "Unknown," because you use an else: at the end
of your if-chain to represent a specific (non-catch-all) case.

s/else:/elif testResult == TestOutcomes.ABORTED:/

Cheers,
Cliff
 
S

Scott David Daniels

Hi,

I have the following class -

class TestOutcomes:
PASSED = 0
FAILED = 1
ABORTED = 2

plus the following code -

testResult = TestOutcomes.PASSED

testResultAsString
if testResult == TestOutcomes.PASSED:
testResultAsString = "Passed"
elif testResult == TestOutcomes.FAILED :
testResultAsString = "Failed"
else:
testResultAsString = "Aborted"

But it would be much nicer if I had a function to covert to string as
part of the TestOutcomes class. How would I implement this?

Thanks,

Barry

class Outcome(object):
PASSED, FAILED, ABORTED = range(3)

@classmethod
def named(class_, value):
for name in dir(class_):
if name[0] != '_' and getattr(class_, name) == value:
return name
raise ValueError('Unknown value %r' % value)

Outcome.named(2)


-Scott David Daniels
(e-mail address removed)
 
S

Steven D'Aprano

Looking at the documentation it looks excellent. But I don't understand
the 0.4.2 version number, particularly when you refer to it as robust.

Perhaps Ben should have followed the usual practice of commercial, closed-
source software developers and started counting his version numbers at
one instead of zero, miss a few releases, use randomly large increments
occasionally, and ended up with a current version number of 2.7.1 for the
exact same level of functionality.

Or he could have called it "Enum XP" (for "eXtra Programming" perhaps) or
"Enum 2007". The next minor release could be "Enum 2008", and the next
major update could be called "Enum Professional Capowie!!!".
 
N

Neil Cerutti

Perhaps Ben should have followed the usual practice of
commercial, closed- source software developers and started
counting his version numbers at one instead of zero, miss a few
releases, use randomly large increments occasionally, and ended
up with a current version number of 2.7.1 for the exact same
level of functionality.

Or he could have called it "Enum XP" (for "eXtra Programming"
perhaps) or "Enum 2007". The next minor release could be "Enum
2008", and the next major update could be called "Enum
Professional Capowie!!!".

I like the (priviledged) code names adopted by the Linux
community for special versions, e.g., Enum 2.7.1 (Flapjacks).
This would really tie the Enum-using community together, and make
messages about it much cuter.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top