Skip to content

Instantly share code, notes, and snippets.

@mjdorma
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save mjdorma/9756431 to your computer and use it in GitHub Desktop.

Select an option

Save mjdorma/9756431 to your computer and use it in GitHub Desktop.

Revisions

  1. mjdorma revised this gist Mar 25, 2014. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions begin.until.choices.py
    Original file line number Diff line number Diff line change
    @@ -17,10 +17,10 @@ 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)
    new_kwargs = {}
    for name, options in kwargs.items():
    new_kwargs[name] = assert_choice(name, options)
    return begin.convert(**new_kwargs)



  2. mjdorma renamed this gist Mar 25, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. mjdorma created this gist Mar 25, 2014.
    26 changes: 26 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    """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)