Author paul.j3
Recipients John.Didion, bethard, manveru, paul.j3, xuanji
Date 2014-03-04.19:01:45
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1393959706.93.0.905114076059.issue11588@psf.upfronthosting.co.za>
In-reply-to
Content
A couple more thoughts on an expanded argument testing mechanism:

- do we need both 'seen_actions' and 'seen_non_default_actions'?  

'seen_actions' is used only to test whether all required actions have been seen.  These 2 sets differ in how positionals with '?*' are categorized.  Positionals like this are always 'seen', even if they just get the default value.  But they are not required (the case of a '*' positional without default needs to be revisited.)

- If 'seen_non_default_actions' is changed to a list (currently its a set), users could test for repeated use on an optional, or even the order of arguments.  

- One way to make this testing mechanism more user-friendly is to provide convenience functions via a decorator.  

For example the decorator could wrap the 'seen_non_default_actions' argument in a 'seen' function.  Such a function could accept either an Action or a 'dest' string, it could accept a single Action, or a list of them, etc.  There could be other functions like 'count', 'unique', 'mutually_exclusive', 'inclusive', etc.

    def testwfnc(func):
        # decorator to register function and provide 'seen'
        name = func.__name__
        def wrapped(parser, seen_actions, *args):
            def seen(*args):
                actions = seen_actions
                if isinstance(args[0], str):
                    actions = [a.dest for a in actions]
                if len(args)>1:
                    return [a in actions for a in args]
                else:
                    return args[0] in actions
            return func(parser, seen)
        parser.register('cross_tests', name, wrapped)
        return wrapped

    #@testwfnc
    def test(parser, seen, *args):
        if seen(a_file):
            print(seen(a_dir, a_pat, a_suf))
            cnt = sum(seen(a_dir, a_pat, a_suf))
        if cnt>0:
            parser.error('FILE cannot have DIR, PATTERN or SUFFIX')
        ...

The attached script experiments with several versions of decorators.  Some sort of testing Class is probably the way to go if we want to provide many convenience methods.
History
Date User Action Args
2014-03-04 19:01:47paul.j3setrecipients: + paul.j3, bethard, xuanji, John.Didion, manveru
2014-03-04 19:01:46paul.j3setmessageid: <1393959706.93.0.905114076059.issue11588@psf.upfronthosting.co.za>
2014-03-04 19:01:46paul.j3linkissue11588 messages
2014-03-04 19:01:46paul.j3create