diff options
| author | troido <troido@protonmail.com> | 2020-03-06 11:48:33 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-03-06 11:48:33 +0100 |
| commit | 09cf076ab48d79f6c4bbcd6d1f606e1747d5da6b (patch) | |
| tree | bcc8ea0cd149d97d25d19c1be7e78d267e214049 /asciifarmclient | |
| parent | 77d1959b873e4f75bcc9878a6bdcc4707465f632 (diff) | |
moved utils into main code directory
Diffstat (limited to 'asciifarmclient')
| -rw-r--r-- | asciifarmclient/common/utils.py | 47 | ||||
| -rw-r--r-- | asciifarmclient/display.py | 2 | ||||
| -rw-r--r-- | asciifarmclient/listselector.py | 2 | ||||
| -rw-r--r-- | asciifarmclient/utils.py | 12 |
4 files changed, 14 insertions, 49 deletions
diff --git a/asciifarmclient/common/utils.py b/asciifarmclient/common/utils.py deleted file mode 100644 index 95ac32b..0000000 --- a/asciifarmclient/common/utils.py +++ /dev/null @@ -1,47 +0,0 @@ - -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 concat(arr): - """Takes a list of sequences, returns the concatenation of the sequences """ - if isinstance(arr[0], str): - return "".join(arr) - if isinstance(arr[0], bytes): - return b"".join(arr) - if isinstance(arr[0], list): - l = [] - for s in arr: - l += s - return l - if isinstance(arr[0], tuple): - l = [] - for s in arr: - l += s - return tuple(l) - else: - raise ValueError("type {} can't be concatenated".format(type(arr[0]))) - - -def writeFileSafe(filename, data, tempname=None): - if tempname is None: - tempname = filename + ".tempfile" - with open(tempname, 'w') as f: - f.write(data) - os.rename(tempname, filename) - - -def readFile(filepath): - with open(filepath, "r") as f: - text = f.read() - return text - - -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] diff --git a/asciifarmclient/display.py b/asciifarmclient/display.py index ae23b05..e1e658e 100644 --- a/asciifarmclient/display.py +++ b/asciifarmclient/display.py @@ -6,7 +6,7 @@ from ratuil.layout import Layout from ratuil.bufferedscreen import BufferedScreen as Screen #from ratuil.screen import Screen from ratuil.textstyle import TextStyle -from asciifarmclient.common.utils import get +from .utils import get from .listselector import ListSelector diff --git a/asciifarmclient/listselector.py b/asciifarmclient/listselector.py index 65e6e8c..33954a4 100644 --- a/asciifarmclient/listselector.py +++ b/asciifarmclient/listselector.py @@ -1,5 +1,5 @@ -from asciifarmclient.common import utils +from . import utils class ListSelector: 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] |
