From 09cf076ab48d79f6c4bbcd6d1f606e1747d5da6b Mon Sep 17 00:00:00 2001 From: troido Date: Fri, 6 Mar 2020 11:48:33 +0100 Subject: moved utils into main code directory --- asciifarmclient/utils.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 asciifarmclient/utils.py (limited to 'asciifarmclient/utils.py') 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] -- cgit