-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringTranslator.py
More file actions
33 lines (26 loc) · 1.08 KB
/
Copy pathStringTranslator.py
File metadata and controls
33 lines (26 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from ast import literal_eval
# Allows arrays to be translated to strings and then back to arrays
class StringTranslator:
def __init__(self): # Static function
raise Exception("static")
# Encodes array as a string
@classmethod
def encode(cls, array):
return str(array)
# Decodes string back to an array
@classmethod
def decode(cls, string):
return literal_eval(string)
'''
You might ask why such a small class even needs to exist.
Originally, this class was much larger until I found these simple
and easy implementations. Now, it is in part legacy code and in
part a cleaner implementation that allows both GameClient and
GameServer to use the same functions.
Another question is why this class is static, as this is generally
not supported in Python. I have talked to professional developers about
whether static classes like this should exist, or whether I should simply
just list functions without a class structure. From this inquiry, I've found
that both implementations are equally acceptable, as both are simple to import
and work fine.
'''