Crosstips.org Crosstips.org

My fun Crossword solver project. Crosstips.org & Krysstips.se

Kung Fu Kung Fu

Fujian White Crane Kung Fu

Fry-IT

Fry-IT is the company I work for

Photos Photos

Photoalbum, both old and new.

Zope Zope

What I have and am doing with Zope

Receptsamlingen Receptsamlingen

In Swedish only. About my "Collection of Recipes" website.

Contact me Contact me

My contact details and how to contact me.

  Mobile version of this page Mobile version of this page


 

niceboolean() - converts what you say to what you mean

niceboolean, cgi parameter, cgi parameters, remember-filterlogic, listissues, true, false

21st of January 2005

In recent code in the IssueTrackerProduct I needed to have a cgi parameter called remember-filterlogic so the URL might look like this: ListIssues?rememeber-filterlogic=no&Filterlogic=show. Because I want the cgi parameters to look human I had to write the following little utility function:

 def niceboolean(value):
    falseness = ('','no','off','false','none','0', 'f')
    return str(value).lower().strip() not in falseness

It basically converts what you say to what you mean. In Python "f" is a one letter string and would normally mean True, but since humans are involved here it from means something else for a moment. What do you think?

UPDATE: The code has changed since the comment below by Ben Young. The code had a missing not which made the code return True on Off and False on On.

UPDATE 2: The emptystring "" is now also False.


Comment

Ben Young - 21st January 2005  [«« Reply to this]
Doesn't that return True if the value is in falseness? Also I would reverse the sense, so that anything not recognised counts as False, so that garbage != True
Peter - 21st January 2005   [«« Reply to this]
You're absolutely right. I'm such a fool.

The code was written differently in the code I copied it from and in a clumsy editing to make it neater I forgot to check that it still did as expected. Thank you.
Anonymous - 21st January 2005  [«« Reply to this]
To catch errors early, you could explicitly check for `trueness' and raise ValueError when `value' is in neither list.
Peter - 21st January 2005   [«« Reply to this]
I don't understand what you mean. Please explain. Certainly the above code can be done in different ways but what's interesting is what it can do. Here is some output from a unit test:

False -> False
True -> True
1 -> True
0 -> False
'1' -> True
'0' -> False
'On' -> True
'Off' -> False
'False' -> False
'No' -> False
'Yes' -> True
'T' -> True
'F' -> False
Ben Young - 21st January 2005  [«« Reply to this]
I think what anonymous means is, what does the unit tests give for
'fish'
''
'nope'
etc
Peter - 21st January 2005   [«« Reply to this]
'fish' and 'nope' must fall back on the default which is True.

It's a bug that '' isn't considered False I think. Will update the code.
 
Name:
Email:
hide my email address.

Your email address will be encoded to prevent email-extraction spiders from reading it so you won't get spammed if you decide to show your email address.