Skip to content

Instantly share code, notes, and snippets.

@dpineiden
Last active December 10, 2018 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpineiden/c0639537bb0d35aaf37fa92f3237722b to your computer and use it in GitHub Desktop.
Save dpineiden/c0639537bb0d35aaf37fa92f3237722b to your computer and use it in GitHub Desktop.
Choice an option from dict
def choice_input(choices_dict,
type_opt_lambda=lambda x: True,
xprint=print):
commands = choices_dict
reverse_commands = {give_name(value): key
for key, value in commands.items()}
options = ["%s -> %s" % (key, give_name(value))
for key, value in commands.items()]
key = ""
msg_type = type_opt_lambda(None)
option_nr = 0
while key not in commands:
[xprint(option) for option in options]
option = input("Choose an option: \n")
command, key, option_nr = get_command(
commands,
reverse_commands,
option)
msg_type = type_opt_lambda(option_nr)
return command, option, msg_type
def get_command(commands, reverse_commands, option):
option_nr = 0
if option.isdigit():
option = int(option)
if option in commands:
command = commands.get(option)
option_nr = option
else:
command = 'PRINT'
option_nr = option
else:
# command is the value
result_k, key, command = multikey(commands, option)
result_v = False
if result_k:
option = key
else:
# command is the key
result_v, key = multivalue(reverse_commands, option)
command = commands.get(key)
if result_v:
option = key
if result_k or result_v:
option_nr = reverse_commands.get(command)
return command, option, option_nr
def multikey(commands, key):
"""
Given an string key, look for the value in commands, if key
has upper or lower case, consider that.
"""
result = False
final_key = key
command = None
if key in commands:
command = commands.get(key)
result = True
elif key.lower() in commands:
final_key = key.lower()
command = commands.get(final_key)
result = True
elif key.upper() in commands:
final_key = key.upper()
command = commands.get(final_key)
result = True
# here command is the value of the key
return result, final_key, command
def multivalue(reverse_commands, value):
"""
Given an string value, look for the key in commands, if value
has upper or lower case, consider that.
"""
result = False
final_value = value
key = None
if value in reverse_commands:
key = reverse_commands.get(value)
result = True
elif value.lower() in reverse_commands:
final_value = value.lower()
key = reverse_commands.get(final_value)
result = True
elif value.upper() in reverse_commands:
final_value = value.upper()
key = reverse_commands.get(final_value)
result = True
# here command is the key
return result, key
def give_name(value):
if callable(value):
return value.__name__
else:
return str(value)
from networktools.library import choice_input
if __name__ == "__main__":
commands = {0: "PRINT",
1: "AUTH_MSG",
2: "ADD_SOCK",
3: "ACTIVE_SOCK",
4: 'CONNECT_SOCK',
5: "SEND_MSG"}
opt = lambda x: 'LOW' if x in [0,1,2] else 'HIGH'
command, key, msg_type = choice_input(commands, type_opt_lambda=opt)
print("====="*5)
print(command, key, msg_type)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment