diff options
Diffstat (limited to 'asciifarmclient/utils.py')
| -rw-r--r-- | asciifarmclient/utils.py | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/asciifarmclient/utils.py b/asciifarmclient/utils.py new file mode 100644 index 0000000..471dd89 --- /dev/null +++ b/asciifarmclient/utils.py @@ -0,0 +1,12 @@ + +import os + +def clamp(val, lower, upper): + """ val if it's between lower and upper, else the closest of the two""" + return max(min(val, upper), lower) + +def get(collection, i, default=None): + """ Get an element in an indexed collection, or the default in case the index is out of bounds """ + if i < 0 or i >= len(collection): + return default + return collection[i] |
