tags:

views:

112

answers:

3

Hi,

I have function defined this way:

def f1 (a, b, c = None, d = None):
    .....

How do I check that a, b are not equal to some value. E.g. I want to check they are not empty strings like, "" or " "

Thinking about something like.

arguments = locals()
for item in arguments:
    check_attribute(item, arguments[item])

And then check if arguments are not "", " ". But in this case it will also try to check None values (what I don't want to do).

+5  A: 

Why can't you refer to the values by their names?

def f1 (a, b, c=None, d=None):
    if not a.strip():
        print('a is not empty')

If you have many arguments it is worth changing function signature to:

def f2 (*args, c=None, d=None):
    for var in args:
        if not var.strip():
            raise ValueError('all elements should be non-empty')
SilentGhost
These break for `None`, against the OP's simple and explicit specs. And the second one total alters the logic -- `f2('fee', 'fie', 'foo')` does **not** set argument `c` (as the same call to `f1` would); in addition, it does not tell the user **what** argument is in error (by name, as the original could), and does **not** check `c` nor `d` -- no real benefits to compensate for these many downsides, compared to the OP's (and my A's) "loop on `locals`" alternative.
Alex Martelli
@Alex: I think `None` only refers to the `c` and `d` and not `a` and `b`. I don't really understand why changing function signature is bad, we don't know how `f1` was used, it surely is better playing with `locals()`.
SilentGhost
@Silent, I agree playing with locals is better, but I suspect you mean that your solution is better _than_ playing with locals -- the reverse of what you're actually saying;-). There is no reason for avoiding `locals` when appropriate, and it's perfectly appropriate here, since giving the relevant argument names in warnings or errors is clearly a big plus. Changing the signature needlessly as you're doing require equally needless revolution of the function's body, e.g. using `args[1]` in lieu of `b`, complex logic to decide what to do if there are five arguments *plus* `c`, and so on!
Alex Martelli
+3  A: 

A typical approach would be:

import sys

...

def check_attribute(name, value):
    """Gives warnings on stderr if the value is an empty or whitespace string.

       All other values, including None, are OK and give no warning.
    """
    if isinstance(value, basestring) and (not value or value.isspace()):
        print>>sys.stderr, "Invalid value %r for argument %r" % (value, name)

or, of course, you could issue warnings, or raise exceptions if the problem is very serious according to your application's semantics.

One should probably delegate all of the checking to a single function, instead of looping in the function whose args you're checking (the latter would be sticking "checking code" smack in the middle of application logic -- better keep it out or the way...):

def check_arguments(d):
    for name, value in d.iteritems():
        check_attribute(name, value)

and the function would be just:

def f1 (a, b, c=None, d=None):
    check_arguments(locals())
    ...

You could, alternatively, write a decorator in order to be able to code

@checked_arguments
def f1 (a, b, c=None, d=None):
   ...

(to get checking code even more "out of the way"), but this might be considered overkill unless you really have a lot of functions requiring exactly this kind of checks!

Argument-name introspection (while feasible, thanks to module inspect) is far less simple in a decorator than within the function itself, which is why my favorite design approach would be to eschew the decorator approach in this case (simplicity is seriously good;-).

Edit -- showing how to implement a decorator, since the OP explicitly asked for one (though without clarifying why).

The main problem (in Python 2.6 and earlier) is for the wrapper to construct a mapping equivalent to the locals() which Python makes for you, but needs to be done explicitly in a generic wrapper.

But -- if you use the new 2.7, inspect.getcallargs does it for you! So, the problem becomes much simpler, and the decorator perhaps worth doing in many more cases (if you're in 2.6 or earlier, I still recommend eschewing the decorator approach, which would be substantially more complicated, for such specialized uses).

So, here is all you need, in Python 2.7 (reusing the check_arguments function I defined above):

import functools
import inspect

def checked_arguments(f):
  @functools.wraps(f)
  def wrapper(*a, **k):
    d = inspect.getcallargs(f, *a, **k)
    check_arguments(d)
    return f(*a, **k)
  return wrapper

The difficulty in pre-2.7 versions comes entirely from the difficulty of implementing the equivalent of inspect.getcallargs -- so, I hope that, if you really need decorators of this kind, you can simply download Python 2.7 from www.python.org and install it on your box!-) (If you do, you'll also get many more goodies besides, as well as a longer support cycle than just about any previous Python version, since 2.7 is slated to be the last release in the Python 2.* line).

Alex Martelli
can you please show how to declare a decorator for the case
Oleg Tarasenko
@Oleg, not right now, as I'm about to starti the 800-miles drive North to OSCON. Seems a rather different question than your original one and my answer is already very long, why don't you open another question on the subject? If no good answer by the time I get into a motel with internet, I'll happily answer that one, too.
Alex Martelli
@Oleg, pausing for a while in Petaluma, and I did get wifi, so I'm preparing the A's edit that you requested (you're in luck -- the new Python 2.7 reduces the difficulty _a lot_!-). Hang on...
Alex Martelli
A: 
for key, value in locals().items():
    if value is not None:
        check_attribute(key, value)

Though as others have said already, you can just check the arguments directly by name.

JAB