-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
153 lines (112 loc) · 4.74 KB
/
util.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 30 13:09:17 2017
@author: ctoffer
"""
import sys
"""
+====+==========+
| ID | Name |
+====+==========+
| 01 | Jürgen |
+----+----------+
| 02 | Johannes |
+----+----------+
"""
class DataTable:
"""
Header is a list of the names of the table columns
Data is a list of dicts, where each dict contains all headers as keys
"""
def __init__(self, header : list, data, to_dict = lambda x : x):
self.__header = header
self.__data = list(map(to_dict, data))
def __findMax(self, ind):
maxs = [len(colName) for colName in self.__header]
for row in self.__data:
for key in self.__header:
if maxs[ind[key]] < len(row[key]):
maxs[ind[key]] = len(row[key])
return maxs
def printToStream(self, stream = sys.stdout):
ind = {colName : i for i, colName in enumerate(self.__header)}
print(ind)
maxs = self.__findMax(ind)
print(maxs)
pad = lambda s, p = ' ' : p + s + p
div = pad('+'.join([(v + 2) * '-' for v in maxs]), '+') + '\n'
hDiv = pad('+'.join([(v + 2) * '=' for v in maxs]), '+') + '\n'
trafo = lambda row : sorted(row.items(), key = lambda t : ind[t[0]])
rjust = lambda k,v : pad(v.rjust(maxs[ind[k]]))
norm = lambda row : [rjust(k,v) for k,v in trafo(row)]
toRow = lambda row : pad('|'.join(norm(row)), '|') + '\n'
stream.write(hDiv)
stream.write(toRow({v : v for v in self.__header}))
stream.write(hDiv)
for row in self.__data:
stream.write(toRow(row))
stream.write(div)
@staticmethod
def readFromString(table : str, from_dict = lambda x : x):
rows = [v for i, v in enumerate(table.split('\n')) if i % 2 == 1]
toCols = lambda row : [s.strip() for s in row.split('|')]
keys = toCols(rows[0])
grid = [{k : v for k, v in zip(keys, toCols(row)) if k != ''} for row in rows[1:]]
grid = filter(lambda x : len(x) > 0, grid)
return list(map(from_dict, grid))
@staticmethod
def readFromFile(fname, from_dict = lambda x : x):
with open(fname, 'r', encoding = 'utf-8') as fp:
return DataTable.readFromString(fp.read(), from_dict = from_dict)
#==============================================================================
class Cipher:
def __init__(self, key):
self.__key = key
#--------------------------------------------------------------------------
def __enter__ (self):
return self
def __exit__ (self, type, value, traceback):
pass
#--------------------------------------------------------------------------
def encode(self, string):
encoded_chars = []
for i in range(len(string)):
key_c = self.__key[i % len(self.__key)]
encoded_c = chr(ord(string[i]) + ord(key_c) % 256)
encoded_chars.append(encoded_c)
encoded_string = "".join(encoded_chars)
return encoded_string
def decode(self, string):
encoded_chars = []
for i in range(len(string)):
key_c = self.__key[i % len(self.__key)]
encoded_c = chr(ord(string[i]) - ord(key_c) % 256)
encoded_chars.append(encoded_c)
encoded_string = "".join(encoded_chars)
return encoded_string
@staticmethod
def invert(string):
return ''.join([chr(255 - ord(c)) for c in string])
#--------------------------------------------------------------------------
#==============================================================================
class ListChoice:
def __init__(self, li : list):
self.__list = li
self.__len = len(li)
def choose(self, title, selector = lambda x : x):
print(title)
print('-' * len(title))
for i, v in enumerate(self.__list):
print(str(i).rjust(len(str(self.__len))), selector(v))
index = int(input("Choose an element by it's index (enter -1 to cancel): "))
while index >= self.__len:
print('-' * 40)
print("The index %s is not in the list" % index)
print("Please try again")
index = int(input("Choose an element by it's index (enter -1 to cancel): "))
if index == -1:
return None
else:
return self.__list[index]
#==============================================================================