diff options
| author | troido <troido@hotmail.com> | 2017-12-31 00:10:14 +0100 |
|---|---|---|
| committer | troido <troido@hotmail.com> | 2017-12-31 00:10:14 +0100 |
| commit | 38f3a3807373a19cacc0171dd7d5fd11f44dbd35 (patch) | |
| tree | 9a411f192d5f6e565a7348988f18357ac39e8153 | |
| parent | 2170dc1bbfe845c2b5ae0a34d5608cd836627cec (diff) | |
merged roomedge into portal, moved tcommunicate and utils to common folder and fixed package when starting main directory
| -rw-r--r-- | asciifarm/client/connection.py | 2 | ||||
| -rw-r--r-- | asciifarm/common/tcommunicate.py (renamed from asciifarm/client/tcommunicate.py) | 0 | ||||
| -rw-r--r-- | asciifarm/common/utils.py | 35 |
3 files changed, 36 insertions, 1 deletions
diff --git a/asciifarm/client/connection.py b/asciifarm/client/connection.py index 9ca2ccc..4eb0ffe 100644 --- a/asciifarm/client/connection.py +++ b/asciifarm/client/connection.py @@ -1,7 +1,7 @@ import socket -from .tcommunicate import send, receive +from asciifarm.common.tcommunicate import send, receive class Connection: diff --git a/asciifarm/client/tcommunicate.py b/asciifarm/common/tcommunicate.py index b1fc1b0..b1fc1b0 100644 --- a/asciifarm/client/tcommunicate.py +++ b/asciifarm/common/tcommunicate.py diff --git a/asciifarm/common/utils.py b/asciifarm/common/utils.py new file mode 100644 index 0000000..3df020c --- /dev/null +++ b/asciifarm/common/utils.py @@ -0,0 +1,35 @@ + +import os + +def clamp(val, lower, upper): + """ val if it's between lower and upper, else the closest of the two""" + return min(max(val, lower), upper) + + +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 == None: + tempname = filename + ".tempfile" + with open(tempname, 'w') as f: + f.write(data) + os.rename(tempname, filename) + |
