Skip to content

Commit 1164e8d

Browse files
committed
Improve param documentation
1 parent 62d2665 commit 1164e8d

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
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: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@
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
"""
2830
Maps a number from one range to another. Somewhat similar to the Arduino ``map()`` function, but
2931
returns a floating point result, and constrains the output value to be between ``out_min`` and ``out_max``.
32+
If ``in_min`` is greater than ``in_max`` or ``out_min`` is greater than ``out_max``, the corresponding
33+
range is reversed, allowing, for example, mapping a range of 0-10 to 50-0.
3034
31-
:return: Returns value mapped to new range
35+
:param float in_min: Start value of input range.
36+
:param float in_max: End value of input range.
37+
:param float out_min: Start value of output range.
38+
:param float out_max: End value of output range.
39+
:return: Returns value mapped to new range.
3240
:rtype: float
3341
"""
3442
in_range = in_max - in_min
@@ -46,14 +54,17 @@ def map_range(x, in_min, in_max, out_min, out_max):
4654
return min(max(mapped, out_max), out_min)
4755

4856

49-
def constrain(x, out_min, out_max):
50-
"""Constrains ``x`` to be within the inclusive range ``[out_min, out_max]``.
57+
def constrain(x: float, out_min: float, out_max: float) -> float:
58+
"""Constrains ``x`` to be within the inclusive range [``out_min``, ``out_max``].
5159
Sometimes called ``clip`` or ``clamp`` in other libraries.
5260
``out_min`` should be less than or equal to ``out_max``.
5361
If ``x`` is less than ``out_min``, return ``out_min``.
5462
If ``x`` is greater than ``out_max``, return ``out_max``.
5563
Otherwise just return ``x``.
5664
57-
:return: Returns value constrained to given range
65+
:param float out_min: Lower bound of output range.
66+
:param float out_max: Upper bound of output range.
67+
:return: Returns value constrained to given range.
68+
:rtype: float
5869
"""
5970
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)