4
\$\begingroup\$

This is a simple filter that I have been using for a project reading data over a serial connection, and thought it would be good to use it as my first attempt to write docstrings. Does anyone have any suggestions? I have been reading PEP 257. As it is a class, should the keyword arguments come after the __init__ ?

If there is a better way to write any part of it (not only the docstrings), I would appreciate it if people could point me in the right direction.

class Filter(object) :
    """Return x if x is greater than min and less than max - else return None. 

    Keyword arguments:
    min -- the minimum (default 0)
    max -- the maximum (default 0)

    Notes:
    Accepts integers and integer strings
    """
    def __init__(self, min=0, max=0) :
        self.min = min
        self.max = max
    def __call__(self, input) :
        try :
            if int(input) <= self.max and int(input) >= self.min : 
                return int(input)
        except : pass
\$\endgroup\$
1
  • \$\begingroup\$ For better commentary, how about including a sample of usage? \$\endgroup\$ Apr 8, 2012 at 22:36

2 Answers 2

4
\$\begingroup\$
class Filter(object) :
    """Return x if x is greater than min and less than max - else return None. 

How about "if x is between min and mix"?

    Keyword arguments:
    min -- the minimum (default 0)
    max -- the maximum (default 0)

    Notes:
    Accepts integers and integer strings
    """
    def __init__(self, min=0, max=0) :

min and max are builtin python functions. I suggest not using the same names if you can help it.

        self.min = min
        self.max = max
    def __call__(self, input) :
        try :
            if int(input) <= self.max and int(input) >= self.min : 

In python you can do: if self.min < int(input) <= self.max

                return int(input)

        except : pass

Don't ever do this. DON'T EVER DO THIS! ARE YOU LISTENING TO ME? DON'T EVER DO THIS.

You catch and ignore any possible exception. But in python everything raises exceptions, and you'll not be notified if something goes wrong. Instead, catch only the specific exceptions you are interested in, in this, case ValueError.

Futhermore, having a filter that tries to accept string or integers like this just a bad idea. Convert your input to integers before you use this function, not afterwards.

The interface you provide doesn't seem all that helpful. The calling code is going to have to deal with None which will be a pain. Usually in python we'd do something operating on a list or as a generator.

Perhaps you want something like this:

return [int(text) for text in input if min < int(text) < max]
\$\endgroup\$
4
  • \$\begingroup\$ I know I was being super lazy with the except:pass, it got the job done... but you words have not fallen on deaf ears :) Ideally I didn't want it to return anything if it was not within the range. Again the conversion of str -> int was out of lazyness, I had another function for that. So I should keep things clean, and shouldn't mix things that shouldn't be mixed... \$\endgroup\$
    – beoliver
    Apr 8, 2012 at 21:44
  • \$\begingroup\$ One question... what exactly is the 'interface'. I didn't think Python did interfaces. \$\endgroup\$
    – beoliver
    Apr 8, 2012 at 21:49
  • \$\begingroup\$ @user969617, interface refers to how you use the function. Stuff like names, return values, parameters, that sorta thing. \$\endgroup\$ Apr 8, 2012 at 21:56
  • \$\begingroup\$ @Winston... gotcha \$\endgroup\$
    – beoliver
    Apr 8, 2012 at 22:01
1
\$\begingroup\$

I'd suggest noting that the filter will always return an int, regardless of what you pass in.

I'd also avoid using a bare except: - masking ALL exceptions is generally a bad idea. Instead, catch the exceptions that you want to ignore (e.g. ValueError and TypeError for integer conversion errors) and do something to handle them (either throwing your own exception or returning a default value).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.