snippetpythonMinor
Filter for reading data over a serial connection
Viewed 0 times
readingserialfilterfordataoverconnection
Problem
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
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.
__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.min :
return int(input)
except : passSolution
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.min :In python you can do:
if self.min < int(input) <= self.maxreturn int(input)
except : passDon'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]Code Snippets
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 : passreturn [int(text) for text in input if min < int(text) < max]Context
StackExchange Code Review Q#10709, answer score: 4
Revisions (0)
No revisions yet.