PymateIO-mapping: Difference between revisions
| (4 intermediate revisions by the same user not shown) | |||
| Line 15: | Line 15: | ||
<nowiki>def map(value, istart, istop, ostart, ostop):</nowiki> | <nowiki>def map(value, istart, istop, ostart, ostop):</nowiki> | ||
Function parameters: | {{underline|Function parameters:}} | ||
* '''value''' : The input value (integer or float) within the range istart..istop. | * '''value''' : The input value (integer or float) within the range istart..istop. | ||
| Line 23: | Line 23: | ||
* '''ostop''' : maximal value of the output range (integer or float). | * '''ostop''' : maximal value of the output range (integer or float). | ||
Function results: | {{underline|Function 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''' . | The function will returns the output value (a float) in the range ostart..ostop. It is the resulting value corresponding to the input '''value''' . | ||
| Line 35: | Line 35: | ||
<nowiki>def ranking( value, ranges, transposed=None ):</nowiki> | <nowiki>def ranking( value, ranges, transposed=None ):</nowiki> | ||
{{underline|Function parameters:}} | |||
* '''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. | * '''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. | ||
| Line 41: | Line 41: | ||
* '''transposed''' : (optional) list of transposed value for each entry of the ranges list. When defined, the resulting output (lower value of the range) is replaced with the corresponding entry from the '''transposed''' list. | * '''transposed''' : (optional) list of transposed value for each entry of the ranges list. When defined, the resulting output (lower value of the range) is replaced with the corresponding entry from the '''transposed''' list. | ||
results: | {{underline|Function results:}} | ||
The lower value of the range within the input value falls otherwise `None` when the input value do not fit any range. The resulting value can be replace with its `transposed` equivalent (when `transposed` is defined). | The lower value of the range within the input value falls otherwise `None` when the input value do not fit any range. The resulting value can be replace with its `transposed` equivalent (when `transposed` is defined). | ||
| Line 48: | Line 48: | ||
<syntaxhighlight lang="python" line> | <syntaxhighlight lang="python" line> | ||
from maps import ranking | |||
result = ranking( val, [10,20,30,40] ) | result = ranking( val, [10,20,30,40] ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 59: | Line 61: | ||
=== Example 2 === | === Example 2 === | ||
<syntaxhighlight lang="python" line> | <syntaxhighlight lang="python" line> | ||
from maps import ranking | |||
result = ranking( val, [10,20,30,40], ["A", "B", "C", "D"] ) | result = ranking( val, [10,20,30,40], ["A", "B", "C", "D"] ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 70: | Line 74: | ||
== slice_by() function == | == slice_by() function == | ||
It exists situation where you need to split a long list into smaller sub-lists of a given length. | |||
It is the purpose of the {{fname|slice_by()}} function creating a list of sub-list each having N items. | |||
<nowiki>def slice_by( lst, by_len ):</nowiki> | <nowiki>def slice_by( lst, by_len ):</nowiki> | ||
{{underline|Function parameters:}} | |||
* | * '''lst''' : the source list. | ||
* | * '''by_len''' : the split length of the sub-lists. | ||
=== Example 1 === | |||
<syntaxhighlight lang="python" line> | |||
from maps import slice_by | 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] | 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) ) | print( "slice by 5", slice_by(l,5) ) | ||
</syntaxhighlight> | |||
which prints the following | |||
<nowiki>slice by 5 [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], | |||
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]] | [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], | ||
[21, 22, 23, 24, 25], [26]]</nowiki> | |||
Latest revision as of 00:40, 12 September 2026
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):
Function parameters:
- 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).
Function 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 ):
Function parameters:
- 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 : (optional) list of transposed value for each entry of the ranges list. When defined, the resulting output (lower value of the range) is replaced with the corresponding entry from the transposed list.
Function results:
The lower value of the range within the input value falls otherwise `None` when the input value do not fit any range. The resulting value can be replace with its `transposed` equivalent (when `transposed` is defined).
Example 1
from maps import ranking
result = ranking( val, [10,20,30,40] )- will returns None for val < 10
- will returns 10 for val>= 10 and val < 20
- will returns 20 for val>= 20 and val < 30
- will returns 30 for val>= 30 and val < 40
- will returns 40 for val>= 40
Example 2
from maps import ranking
result = ranking( val, [10,20,30,40], ["A", "B", "C", "D"] )- will returns None for val < 10
- will returns "A" for val>= 10 and val < 20
- will returns "B" for val>= 20 and val < 30
- will returns "C" for val>= 30 and val < 40
- will returns "D" for val>= 40
slice_by() function
It exists situation where you need to split a long list into smaller sub-lists of a given length.
It is the purpose of the slice_by() function creating a list of sub-list each having N items.
def slice_by( lst, by_len ):
Function parameters:
- lst : the source list.
- by_len : the split length of the sub-lists.
Example 1
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) )which prints the following
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]]