Skip to content

Instantly share code, notes, and snippets.

@mjdorma
Last active August 29, 2015 13:57
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 mjdorma/9756431 to your computer and use it in GitHub Desktop.
Save mjdorma/9756431 to your computer and use it in GitHub Desktop.
begin.utils.choices
"""Assert a choice for the value of an argument.
Example::
@begin.start
@choices(foo=['meh', 'one'])
def main(foo):
print(foo)
"""
import begin
def choices(**kwargs):
"Validate arguments values are in a set"
def assert_choice(name, options):
def assert_wrapper(value):
if value not in options:
raise TypeError("'%s' choices are: '%s'" % (name, "', '".join(options)))
return value
new_kwargs = {}
for name, options in kwargs.items():
new_kwargs[name] = assert_choice(name, options)
return begin.convert(**new_kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment