PymateIO-mapping
Abstract
The map() function is very popular and useful function in Arduino world.
Pymate includes the maps.py library regrouping interpolation, categorization and other functions sourced from MC Hobby libraries.
map() function
The map() function is used to perform linear interpolation along the line identified by two points (start and stop).
The maps() results is floating point value. It can be casted to integer with int(maps( ... )).
def map(value, istart, istop, ostart, ostop):
with:
- value : The input value (integer or float) within the range istart..istop.
- istart : minimal value of input range (integer or float).
- istop : maximal value of the input range (integer or float).
- ostart : minimal value of output range (integer or float).
- ostop : maximal value of the output range (integer or float).
Results:
The function will returns the output value (a float) in the range ostart..ostop. It is the resulting value corresponding to the input __value__ .
ranking() function
The ranking() function is used to categorize an input value within a limited set of ranges.
The function can also transpose the categorized value with a list of output values (when defined in the call).
def ranking( value, ranges, transposed=None ):
with:
- value : the input value compared to the various ranges. When falling into a range, the resulting output value is the lower value of the range (or corresponding __transposed__ value when defined). ___None___ is return when the input value is below the very first range.
- ranges : list of increasing values defining the n ranges. Ranges can be read as follows range[0]..range[1], range[1]..range[2], range[2]..range[3], ... range[n-1]..range[n]
- transposed : (optionnel) liste des valeurs transposées correspondant à aux intervalles. Si définit c'est la valeur transposée qui est retournée n lieu et place de la borne inférieure de l'intervalle.
retourne:
La borne inférieure de l'intervalle dans lequel la valeur `value` tombe ou `None`. Si une liste de valeurs transposée existe alors le résultat (borne inférieure) est extrait de la liste `transposed` avant d'être retourné
Exemple: ``` result = ranking( val, [10,20,30,40] ) ```
- retourne None si val < 10
- retourne 10 si val>= 10 et val < 20
- retourne 20 si val>= 20 et val < 30
- retourne 30 si val>= 30 et val < 40
- retourne 40 si val>= 40
``` result = ranking( val, [10,20,30,40], ["A", "B", "C", "D"] ) ```
- retourne None si val < 10
- retourne "A" si val>= 10 et val < 20
- retourne "B" si val>= 20 et val < 30
- retourne "C" si val>= 30 et val < 40
- retourne "D" si val>= 40
remarks: l'appel pourrait également être écrit `result = ranking( val, [10,20,30,40], "ABCD" )`
slice_by() function
La fonction `slice_by()` permet diviser un liste en sous-listes de N éléments.
def slice_by( lst, by_len ):
where:
- __lst__ : la liste source.
- __by_len__ : le nombre d'élément dans chaque sous-liste.
Exemple:
``` python
from maps import slice_by
l = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26] print( "slice by 5", slice_by(l,5) ) ```
ce qui affiche le resultat suivant:
``` slice by 5 [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25], [26]] ```