My __init__
method accepts another function as an argument called func_convert
:
class Adc:
"""Reads data from ADC
"""
def __init__(self, func_convert):
"""Setup the ADC
Parameters
----------
func_convert : [???]
user-supplied conversion function
"""
self._func_convert = func_convert
def read(self):
data = 0 #some fake data
return self._func_convert(data)
The argument func_convert
allows a custom scaling function to be supplied once at instantiation that gets called to convert the data each time it's read. The function must accept a single int argument and return a single float. One possible example would be:
def adc_to_volts(value):
return value * 3.0 / 2**16 - 1.5
adc = Adc(adc_to_volts)
volts = adc.read()
Is there a standard way to document what the expected signature of func_convert
is in the parameters section of the __init__
docstring? If it makes a difference, I'm using numpy docstring style (I think).
I don't know if this standard exists for docstrings - you can of course explain what the function needs in simple sentences, but I assume you want a standard, documentation generator-friendly way to do this.
If you don't mind switching tools, this is possible using type hints and the Callable
object from the typing module:
from typing import Callable
class Adc:
"""
Reads data from ADC
"""
def __init__(self, func_convert: Callable[[int], float]) -> None:
self._func_convert = func_convert
def read(self):
data = 0 # some fake data
return self._func_convert(data)