Created
April 3, 2018 14:11
-
-
Save arkenidar/c4973e10b602d612c22af1b4b6391464 to your computer and use it in GitHub Desktop.
select first or all callables based on check of conditions specified in callables
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
#select.py | |
#select first or all callables based on check of conditions specified in callables | |
def select(cases, mode='first'): # all or first | |
returned=[] | |
for icase in cases: | |
if icase[0](): | |
returned.append(icase[1]()) | |
if mode=='first': return returned[0] | |
return returned | |
def testSelect(x, mode): | |
print(x, | |
select([ | |
[lambda: x<10 and x>0, lambda: 'x<10 and x>0'], | |
[lambda: x==10, lambda: 'x==10'], | |
[lambda: x>=10, lambda: 'x>=10'], | |
[lambda: True, lambda: 'always true'], | |
], mode) | |
) | |
print('***************************************************************') | |
testSelect(20,'all') | |
print('***************************************************************') | |
testSelect(0,'first') | |
print('***************************************************************') | |
testSelect(10,'all') | |
print('***************************************************************') | |
testSelect(10,'first') | |
print('***************************************************************') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment