Discussion:
[Tutor] Is there a way to store and later use comparison operators (<, <=, =, >=, >) ?
boB Stepp
2015-04-29 20:10:09 UTC
Permalink
Python 2.4.4, Solaris 10.

I have some functions that I believe I could collapse into a single
function if I only knew how:

def choose_compare(operator, value0, value1, pass_color, fail_color):
"""
Perform the comparison indicated by operator. Return pass_color if
true, fail_color if false.
"""
if operator == '<':
return less_than(value0, value1, pass_color, fail_color)
elif operator == '<=':
return less_than_or_equal(value0, value1, pass_color, fail_color)
elif operator == '=':
return equal(value0, value1, pass_color, fail_color)
elif operator == '>':
return greater_than(value0, value1, pass_color, fail_color)
elif operator == '>=':
return greater_than_or_equal(value0, value1, pass_color, fail_color)
else:
print 'WarningMessage = "Invalid comparison operator in
function, choose_compare()***@Please contact script administrator for
assistance.";'

def less_than(value0, value1, pass_color, fail_color):
"""
See if value0 is less than value1. If true, return pass_color. If
false, return fail_color.
"""
if value0 < value1:
return pass_color, True
else:
return fail_color, False

def less_than_or_equal(value0, value1, pass_color, fail_color):
"""
See if value0 is less than or equal to value1. If true, return
pass_color. If false, return fail_color.
"""
if value0 <= value1:
return pass_color, True
else:
return fail_color, False

... 3 more functions ...

I won't give the remaining functions for the other comparison
operators. The string variable, operator, is originally populated from
a data file, which tells what type of comparison needs to be made. The
last two functions I gave (and the ones I omitted giving) all follow
the same exact pattern. I know there has to be some way to replace
these 5 functions with 1, but what experimentation I have done to date
has not worked.

Also, what about the first function above? I could use 2 dictionaries,
1 for calling the 5 functions and one to pass the arguments, but is it
worth doing this? Or, I would not be surprised if there is a much
better way! ~(:>))

Thanks!
--
boB
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Marc Tompkins
2015-04-29 20:46:10 UTC
Permalink
Post by boB Stepp
Python 2.4.4, Solaris 10.
I have some functions that I believe I could collapse into a single
"""
Perform the comparison indicated by operator. Return pass_color if
true, fail_color if false.
"""
return less_than(value0, value1, pass_color, fail_color)
return less_than_or_equal(value0, value1, pass_color, fail_color)
return equal(value0, value1, pass_color, fail_color)
return greater_than(value0, value1, pass_color, fail_color)
return greater_than_or_equal(value0, value1, pass_color, fail_color)
print 'WarningMessage = "Invalid comparison operator in
assistance.";'
"""
See if value0 is less than value1. If true, return pass_color. If
false, return fail_color.
"""
return pass_color, True
return fail_color, False
"""
See if value0 is less than or equal to value1. If true, return
pass_color. If false, return fail_color.
"""
return pass_color, True
return fail_color, False
... 3 more functions ...
I won't give the remaining functions for the other comparison
operators. The string variable, operator, is originally populated from
a data file, which tells what type of comparison needs to be made. The
last two functions I gave (and the ones I omitted giving) all follow
the same exact pattern. I know there has to be some way to replace
these 5 functions with 1, but what experimentation I have done to date
has not worked.
Also, what about the first function above? I could use 2 dictionaries,
1 for calling the 5 functions and one to pass the arguments, but is it
worth doing this? Or, I would not be surprised if there is a much
better way! ~(:>))
Thanks!
Here's what I came up with:

def choose_compare(operator, value0, value1, pass_color, fail_color):
comps = {"=":"==", "<":"<", ">":">", "<=":"<=", ">=":">="}
if operator in comps.keys():
operator = comps[operator]
if eval("{} {} {}".format(value0, operator, value1)):
return pass_color, True
else:
return fail_color, False
else:
print('WarningMessage')

I would ordinarily avoid eval() like the plague, but I think that this
sanitizes the input pretty effectively. I had to make comps a dict instead
of a list because (in your example, anyway) you're using a single equals
sign to check for equality, which throws a Syntax Error (e.g. "if 1 = 2"
instead of "if 1 == 2").
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
boB Stepp
2015-04-29 21:12:29 UTC
Permalink
Post by Marc Tompkins
Post by boB Stepp
Python 2.4.4, Solaris 10.
I have some functions that I believe I could collapse into a single
"""
Perform the comparison indicated by operator. Return pass_color if
true, fail_color if false.
"""
return less_than(value0, value1, pass_color, fail_color)
return less_than_or_equal(value0, value1, pass_color, fail_color)
return equal(value0, value1, pass_color, fail_color)
return greater_than(value0, value1, pass_color, fail_color)
return greater_than_or_equal(value0, value1, pass_color, fail_color)
print 'WarningMessage = "Invalid comparison operator in
assistance.";'
"""
See if value0 is less than value1. If true, return pass_color. If
false, return fail_color.
"""
return pass_color, True
return fail_color, False
"""
See if value0 is less than or equal to value1. If true, return
pass_color. If false, return fail_color.
"""
return pass_color, True
return fail_color, False
... 3 more functions ...
I won't give the remaining functions for the other comparison
operators. The string variable, operator, is originally populated from
a data file, which tells what type of comparison needs to be made. The
last two functions I gave (and the ones I omitted giving) all follow
the same exact pattern. I know there has to be some way to replace
these 5 functions with 1, but what experimentation I have done to date
has not worked.
Also, what about the first function above? I could use 2 dictionaries,
1 for calling the 5 functions and one to pass the arguments, but is it
worth doing this? Or, I would not be surprised if there is a much
better way! ~(:>))
Thanks!
comps = {"=":"==", "<":"<", ">":">", "<=":"<=", ">=":">="}
operator = comps[operator]
return pass_color, True
return fail_color, False
print('WarningMessage')
I would ordinarily avoid eval() like the plague, but I think that this
sanitizes the input pretty effectively. I had to make comps a dict instead
of a list because (in your example, anyway) you're using a single equals
sign to check for equality, which throws a Syntax Error (e.g. "if 1 = 2"
instead of "if 1 == 2").
I could deal with the "=" issue by either reformatting my data file to
use "==" in place of "=", or when I parse the data file, do the
replacement there. A list instead of the dictionary looks a little
easier on my eyes.

The list has me so leery of eval and exec that I totally forgot about
this possibility! There are only two places in my program where I read
information directly into my program: 1) The data file, or 2) how the
user of the planning software names his regions of interest (ROI) in
the planning system software. I will reexamine my checks of (1). For
(2) the planning software already has its own checks, which would
filter out a lot. And I am checking the ROIs to see if they are
present in the data file *exactly* as given in the data file;
otherwise, I reject them.

So I have stumbled (With your gracious help!) into a legitimate use of eval()?

Many thanks, again!
--
boB
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Peter Otten
2015-04-29 22:49:12 UTC
Permalink
Post by boB Stepp
Post by Marc Tompkins
Post by boB Stepp
Python 2.4.4, Solaris 10.
I have some functions that I believe I could collapse into a single
"""
Perform the comparison indicated by operator. Return pass_color if
true, fail_color if false.
"""
return less_than(value0, value1, pass_color, fail_color)
return less_than_or_equal(value0, value1, pass_color, fail_color)
return equal(value0, value1, pass_color, fail_color)
return greater_than(value0, value1, pass_color, fail_color)
return greater_than_or_equal(value0, value1, pass_color, fail_color)
print 'WarningMessage = "Invalid comparison operator in
assistance.";'
"""
See if value0 is less than value1. If true, return pass_color. If
false, return fail_color.
"""
return pass_color, True
return fail_color, False
"""
See if value0 is less than or equal to value1. If true, return
pass_color. If false, return fail_color.
"""
return pass_color, True
return fail_color, False
... 3 more functions ...
I won't give the remaining functions for the other comparison
operators. The string variable, operator, is originally populated from
a data file, which tells what type of comparison needs to be made. The
last two functions I gave (and the ones I omitted giving) all follow
the same exact pattern. I know there has to be some way to replace
these 5 functions with 1, but what experimentation I have done to date
has not worked.
Also, what about the first function above? I could use 2 dictionaries,
1 for calling the 5 functions and one to pass the arguments, but is it
worth doing this? Or, I would not be surprised if there is a much
better way! ~(:>))
Thanks!
comps = {"=":"==", "<":"<", ">":">", "<=":"<=", ">=":">="}
operator = comps[operator]
return pass_color, True
return fail_color, False
print('WarningMessage')
I would ordinarily avoid eval() like the plague, but I think that this
sanitizes the input pretty effectively. I had to make comps a dict
instead of a list because (in your example, anyway) you're using a single
equals
sign to check for equality, which throws a Syntax Error (e.g. "if 1 = 2"
instead of "if 1 == 2").
I could deal with the "=" issue by either reformatting my data file to
use "==" in place of "=", or when I parse the data file, do the
replacement there. A list instead of the dictionary looks a little
easier on my eyes.
The list has me so leery of eval and exec that I totally forgot about
this possibility! There are only two places in my program where I read
information directly into my program: 1) The data file, or 2) how the
user of the planning software names his regions of interest (ROI) in
the planning system software. I will reexamine my checks of (1). For
(2) the planning software already has its own checks, which would
filter out a lot. And I am checking the ROIs to see if they are
present in the data file *exactly* as given in the data file;
otherwise, I reject them.
So I have stumbled (With your gracious help!) into a legitimate use of eval()?
No. To expand on Marks hint here's how to do it without evil eval().

import operator

comps = {
"=": operator.eq,
"<": operator.lt,
">": operator.gt,
# ...
}

def choose_compare(operator, value0, value1, pass_color, fail_color):
op = comps[operator]
if op(value0, value1):
return pass_color, True
else:
return fail_color, False

print(choose_compare("=", 1, 1, "red", "blue"))
print(choose_compare("<", 1, 2, "red", "blue"))
print(choose_compare("<", 2, 1, "red", "blue"))

Rule of thumb: when you think you need eval() you're wrong.

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Danny Yoo
2015-04-29 23:52:53 UTC
Permalink
Hi Bob,

By the way, it sounds like you're starting to learn about how to write
interpreters. If that's the case, you might find PLAI helpful:

http://cs.brown.edu/~sk/Publications/Books/ProgLangs/

helpful. (Full disclosure: the author was my external advisor. :P)


Another good book is EoPL:

http://www.amazon.com/Essentials-Programming-Languages-Daniel-Friedman/dp/0262062798


I have fond memories of those two books.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
boB Stepp
2015-04-30 03:57:26 UTC
Permalink
Post by Peter Otten
Post by boB Stepp
So I have stumbled (With your gracious help!) into a legitimate use of eval()?
No. To expand on Marks hint here's how to do it without evil eval().
import operator
comps = {
"=": operator.eq,
"<": operator.lt,
">": operator.gt,
# ...
}
op = comps[operator]
return pass_color, True
return fail_color, False
print(choose_compare("=", 1, 1, "red", "blue"))
print(choose_compare("<", 1, 2, "red", "blue"))
print(choose_compare("<", 2, 1, "red", "blue"))
Rule of thumb: when you think you need eval() you're wrong.
Thanks, Peter! The lure of eval() once more avoided...
--
boB
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Mark Lawrence
2015-04-29 21:42:40 UTC
Permalink
Post by boB Stepp
Python 2.4.4, Solaris 10.
I have some functions that I believe I could collapse into a single
"""
Perform the comparison indicated by operator. Return pass_color if
true, fail_color if false.
"""
return less_than(value0, value1, pass_color, fail_color)
return less_than_or_equal(value0, value1, pass_color, fail_color)
return equal(value0, value1, pass_color, fail_color)
return greater_than(value0, value1, pass_color, fail_color)
return greater_than_or_equal(value0, value1, pass_color, fail_color)
print 'WarningMessage = "Invalid comparison operator in
assistance.";'
"""
See if value0 is less than value1. If true, return pass_color. If
false, return fail_color.
"""
return pass_color, True
return fail_color, False
"""
See if value0 is less than or equal to value1. If true, return
pass_color. If false, return fail_color.
"""
return pass_color, True
return fail_color, False
... 3 more functions ...
I won't give the remaining functions for the other comparison
operators. The string variable, operator, is originally populated from
a data file, which tells what type of comparison needs to be made. The
last two functions I gave (and the ones I omitted giving) all follow
the same exact pattern. I know there has to be some way to replace
these 5 functions with 1, but what experimentation I have done to date
has not worked.
Also, what about the first function above? I could use 2 dictionaries,
1 for calling the 5 functions and one to pass the arguments, but is it
worth doing this? Or, I would not be surprised if there is a much
better way! ~(:>))
Thanks!
This isn't a job for Bicycle Repair Man!!! It smacks to me of
dictionaries and the operator module but I'm too bone idle to look it up
myself, so try here https://docs.python.org/3/library/operator.html
D'oh :)
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
boB Stepp
2015-04-30 03:49:46 UTC
Permalink
Post by Mark Lawrence
This isn't a job for Bicycle Repair Man!!!
<chuckle!> Not even if we only use the latest, greatest,
computer-aided bicycle repair technology???
Post by Mark Lawrence
... It smacks to me of dictionaries
and the operator module but I'm too bone idle to look it up myself, so try
here https://docs.python.org/3/library/operator.html
D'oh :)
So little time, so much Python standard library! I *do* search and
search and ... search before I ask, but my Google-fu is often weak,
especially when I am searching for hints as to how to solve a problem
where I am unsure of the correct technical terminology to use in the
search. OTH, I have sometimes spent hours searching, both online and
in what books I have, and thus spared y'all many, ... , many other
questions that I might have otherwise asked!

One problem I have with searching the Python documentation is this:
https://docs.python.org/release/2.4.4/lib/lib.html
I never would have guessed beforehand that I would be needing to look
under: "3. Python Runtime Services! I spend most of my time on this
page as this is my most limiting version of Python that I must deal
with.

This page is *not* well-formatted and it all runs together. Even when
I *know* something is there, I find myself having to Ctrl-F and
entering the term I am looking for. And when I am not sure what I am
looking for, I don't usually come up with the correct term. The later
Python docs are much easier on the eyes, I do say!

Anyway, Mark, thanks for the link! This looks quite straightforward
and I will be able to side-step the evils of eval() once again.
--
boB
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Ben Finney
2015-04-30 05:15:26 UTC
Permalink
Post by boB Stepp
https://docs.python.org/release/2.4.4/lib/lib.html
If you actually need to read the documentation specifically for a Python
version that has not been supported since 2008, then I agree that is a
problem.

The documentation has improved since then. If you want newer-looking
documentation, maybe you should not expect it from a Python released
nearlt a decade ago?
--
\ “Not to be absolutely certain is, I think, one of the essential |
`\ things in rationality.” —Bertrand Russell |
_o__) |
Ben Finney

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
h
boB Stepp
2015-04-30 23:54:21 UTC
Permalink
Post by Ben Finney
Post by boB Stepp
https://docs.python.org/release/2.4.4/lib/lib.html
If you actually need to read the documentation specifically for a Python
version that has not been supported since 2008, then I agree that is a
problem.
I'm pretty much stuck with these relics of Pythons past.
Post by Ben Finney
The documentation has improved since then. If you want newer-looking
documentation, maybe you should not expect it from a Python released
nearlt a decade ago?
No, I am not looking for fancy aesthetics. I am looking for
intelligent use of whitespace, so that everything does not run
together into a nearly indistinguishable blob. Bold main section
headings would be nice as well. All of this has been possible forever
and evermore. Readability counts! ~(:>)
--
boB
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Ben Finney
2015-05-01 00:50:53 UTC
Permalink
Post by boB Stepp
Post by Ben Finney
If you actually need to read the documentation specifically for a
Python version that has not been supported since 2008, then I agree
that is a problem.
I'm pretty much stuck with these relics of Pythons past.
Then you are, unfortunately, stuck with the documentation for that
version. It will not be updated any more, because support for that
ancient version has ended.
Post by boB Stepp
Post by Ben Finney
The documentation has improved since then. If you want newer-looking
documentation, maybe you should not expect it from a Python released
nearlt a decade ago?
No, I am not looking for fancy aesthetics. I am looking for
intelligent use of whitespace, so that everything does not run
together into a nearly indistinguishable blob. Bold main section
headings would be nice as well. All of this has been possible forever
and evermore. Readability counts! ~(:>)
So you might like to borrow the Python time machine and complain to the
people of the mid-2000s.

Complaining about it today, when the currently-maintained documentation
does not have these problems, is futile.
--
\ “Quidquid latine dictum sit, altum viditur.” (“Whatever is |
`\ said in Latin, sounds profound.”) —anonymous |
_o__) |
Ben Finney

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://ma
Laura Creighton
2015-04-30 06:02:33 UTC
Permalink
Python 2.4 is really old, right now. OpenCSW has 2.6.9
http://www.opencsw.org/package/python/

Any chance you could use that?

Laura

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
boB Stepp
2015-05-01 03:55:37 UTC
Permalink
Post by Laura Creighton
Python 2.4 is really old, right now. OpenCSW has 2.6.9
http://www.opencsw.org/package/python/
Any chance you could use that?
Laura, I may just attempt this on the dev machine. I have mentioned
before that the production environment is running Python 2.6.x (I
think it is 2.6.6, but don't remember for certain.). Surely updating
Python on the dev to one very near the prod env is an improvement? And
if I am going to do that, I might as well install Git. And I will
probably throw in gVim, too. I have been pondering everyone's comments
to date, and it seems to me that I should be doing all of my
development on the development environment machine and it is thus
justifiable to add the tools I need to effectively do this (Again,
thanks for your thoughts, Cameron!). This seems more *sane*! Plus it
will save me tons of time.

Even though the link you gave was for a package add, I just *know*
there will be missing dependencies. Guess I will have to learn how to
resolve all the issues that come up. Should be fun!

Thanks, Laura!
--
boB
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Continue reading on narkive:
Loading...