Last active
June 5, 2019 10:07
-
-
Save gurunars/e33af66da6b473d49d87a73cecc20261 to your computer and use it in GitHub Desktop.
Loopy and non loopy solutions
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
import json | |
def find_meaning(param): | |
return 11 | |
def try_luck(param): | |
return 7 | |
def my_loopy_thing(some_argument): | |
output, error = None, None | |
for _ in range(1): | |
# meaning phase | |
tempo = find_meaning(some_argument) | |
if tempo == 42: | |
output = tempo | |
break | |
elif tempo == 404: | |
error = "NOT FOUND" | |
break | |
# luck phase | |
tempo = try_luck(tempo) | |
if tempo == 7: | |
output = tempo | |
break | |
elif tempo == 13: | |
error = "NOT LUCKY" | |
break | |
return json.dumps({ | |
output: output, | |
error: error | |
}) | |
from enum import Enum | |
class Result(Enum): | |
OK = 1 | |
ERROR = 2 | |
CONTINUE = 3 | |
def chain_and_serialize_stuff(*calls): | |
def wrapper(cursor): | |
rtype = Result.CONTINUE | |
queue = list(calls) | |
while rtype == Result.CONTINUE: | |
call = queue.pop(0) | |
cursor, rtype = call(cursor) | |
if rtype == Result.ERROR: | |
result = { "error": cursor } | |
else: | |
result = { "output": cursor } | |
return json.dumps(result) | |
return wrapper | |
def meaning_phase(some_argument)chain_and_serialize_stuff: | |
tempo = find_meaning(some_argument) | |
if tempo == 42: | |
return tempo, Result.OK | |
elif tempo == 404: | |
return "NOT FOUND", Result.ERROR | |
else: | |
return tempo, Result.CONTINUE | |
def luck_phase(some_other_argument): | |
tempo = try_luck(some_other_argument) | |
if tempo == 7: | |
return tempo, Result.OK | |
elif tempo == 13: | |
return "NOT LUCKY", Result.ERROR | |
else: | |
return tempo, Result.CONTINUE | |
my_non_loopy_thing = chain_and_serialize_stuff( | |
meaning_phase, | |
luck_phase | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment