Created
May 22, 2015 02:44
-
-
Save tekton/736ba476aa416e413d2d to your computer and use it in GitHub Desktop.
operator_string_switch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
The goal here is to create a "switch" like environment to allow for using | |
strings and other representations for dynamic comparison of two values. | |
Uses the operator library: https://docs.python.org/2/library/operator.html | |
""" | |
import operator | |
OPS = { | |
'=': operator.eq, | |
'==': operator.eq, | |
'!=': operator.ne, | |
'>=': operator.gt, | |
'<=': operator.le, | |
'>': operator.gt, | |
'<': operator.lt, | |
'gt': operator.gt, | |
'gte': operator.gt, | |
} | |
def compute(valL, valR, op): | |
print OPS[op](valL, valR) | |
compute("a", "b", "!=") | |
compute(1, 1, "!=") | |
compute(1, 2, "!=") | |
compute(100, -999, "gte") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment