Skip to content

Commit 0821801

Browse files
committed
Improve param documentation
1 parent 62d2665 commit 0821801

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# SPDX-FileCopyrightText: 2021 Dan Halbert for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
15
name: Run Tests
26

37
on: [pull_request, push]

adafruit_simplemath.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,21 @@
2323
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SimpleMath.git"
2424

2525

26-
def map_range(x, in_min, in_max, out_min, out_max):
26+
def map_range(
27+
x: float, in_min: float, in_max: float, out_min: float, out_max: float
28+
) -> float:
2729
"""
28-
Maps a number from one range to another. Somewhat similar to the Arduino ``map()`` function, but
29-
returns a floating point result, and constrains the output value to be between ``out_min`` and ``out_max``.
30+
Maps a number from one range to another. Somewhat similar to the Arduino ``map()`` function,
31+
but returns a floating point result, and constrains the output value to be between
32+
``out_min`` and ``out_max``.
33+
If ``in_min`` is greater than ``in_max`` or ``out_min`` is greater than ``out_max``,
34+
the corresponding range is reversed, allowing, for example, mapping a range of 0-10 to 50-0.
3035
31-
:return: Returns value mapped to new range
36+
:param float in_min: Start value of input range.
37+
:param float in_max: End value of input range.
38+
:param float out_min: Start value of output range.
39+
:param float out_max: End value of output range.
40+
:return: Returns value mapped to new range.
3241
:rtype: float
3342
"""
3443
in_range = in_max - in_min
@@ -46,14 +55,17 @@ def map_range(x, in_min, in_max, out_min, out_max):
4655
return min(max(mapped, out_max), out_min)
4756

4857

49-
def constrain(x, out_min, out_max):
50-
"""Constrains ``x`` to be within the inclusive range ``[out_min, out_max]``.
58+
def constrain(x: float, out_min: float, out_max: float) -> float:
59+
"""Constrains ``x`` to be within the inclusive range [``out_min``, ``out_max``].
5160
Sometimes called ``clip`` or ``clamp`` in other libraries.
5261
``out_min`` should be less than or equal to ``out_max``.
5362
If ``x`` is less than ``out_min``, return ``out_min``.
5463
If ``x`` is greater than ``out_max``, return ``out_max``.
5564
Otherwise just return ``x``.
5665
57-
:return: Returns value constrained to given range
66+
:param float out_min: Lower bound of output range.
67+
:param float out_max: Upper bound of output range.
68+
:return: Returns value constrained to given range.
69+
:rtype: float
5870
"""
5971
return max(out_min, min(x, out_max))

tests/constrain_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from adafruit_simplemath import constrain
66

7+
78
def test_constrain():
89
assert constrain(1, 1, 10) == 1
910
assert constrain(10, 1, 10) == 10

tests/map_range_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from adafruit_simplemath import map_range
66

7+
78
def test_map_range():
89
assert map_range(1, 0, 10, 0, 100) == 10.0
910
assert map_range(-1, 0, 10, 0, 100) == 0

0 commit comments

Comments
 (0)