Simple test
Ensure your device works with this simple test.
examples/functools_simpletest.py
1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2# SPDX-FileCopyrightText: Copyright (c) 2022 Alec Delaney
3#
4# SPDX-License-Identifier: Unlicense
5
6"""Example usage of the circuitpython_functools module."""
7
8from circuitpython_functools import cache, lru_cache
9
10
11@lru_cache(maxsize=3)
12def add(a, b, *, c=1, d=2):
13 """Perform addition."""
14 return a + b + c + d
15
16
17@cache
18def subtract(first_value, second_value):
19 """Perform subtraction."""
20 return first_value - second_value
21
22
23print(add(1, 1, c=2, d=3))
24print(add(1, 1, c=2, d=3)) # Repeat call, will use cached result
25print(add(1, 0, c=3, d=4))
26print(subtract(42, 20))
27print(add(1, b=1, c=2, d=3))
28print(add(1, b=0, c=2, d=3))
29
30# Clear all the caches so we don't use too much memory
31add.cache_clear()
32subtract.cache_clear()
33
34print(add(1, 0, c=3, d=4)) # Cache was cleared, so this is caclulated again
35print(subtract(18, 4))