-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinputstr_generator.py
More file actions
116 lines (83 loc) · 2.82 KB
/
Copy pathinputstr_generator.py
File metadata and controls
116 lines (83 loc) · 2.82 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
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
'''
Input Generator class
@author: Klint
'''
def make_input_generators(device_map: dict):
input_generators = []
for k,v in device_map.items():
input_generators.append(InputGenerator(k, set(v.keys())))
return input_generators
class InputGenerator:
def __init__(self, device: str, states: set):
self.dev_name = device
self._dev_strdict = self._make_strdict(states)
self._curr_time = 0
def _make_strdict(self, states: set):
to_return = {};
for state in states:
to_return[state] = ''
return to_return
def write_on_state(self, state: str, int_time: int):
# time_left = self._max_size - self._curr_time
# if int_time > time_left: # may throw error here idk yet
# int_time = time_left
for key in self._dev_strdict.keys():
if key == state:
self._dev_strdict[key] += int_time*'1'
else:
self._dev_strdict[key] += int_time*'0'
self._curr_time += int_time
def states(self):
return set(self._dev_strdict.keys())
def _build_str(self):
template = '{},{},{}\n'
to_return = ''
for k,v in self._dev_strdict.items():
to_return += template.format(self.dev_name, k, v)
return to_return
def generate_str(self):
return self._build_str()
def __repr__(self):
to_return = 'curr_size: {}\n'.format(self._curr_time)
to_return += self._build_str()
return to_return
from collections import Counter
'''
Counter, d = {'a': 1, 'b':3}
d['a'] == 1
d['a'] += 11
d['a'] == 12
class NameGenerator:
add_word(s: string)->increment the count of that word if it exists, otherwise set count = 0
get_string(s: string)-> return s + count as string if 0, return s
public void method(this, int x, int y)
a = (this.a)
method2 == (this.method2())
'''
class NameGenerator:
def __init__(self):
self.counter = Counter()
def add_word(self, s):
if s in self.counter:
self.counter[s] += 1
else:
self.counter[s] = 0
def generate_name(self, s):
self.add_word(s)
if self.counter[s] == 0:
return s
else:
return s + ' ({})'.format(self.counter[s])
if __name__ == '__main__':
i1 = InputGenerator('tv', {'on', 'off', 'standby'})
i1.write_on_state('on', 25)
print(i1)
i1.write_on_state('off', 50)
i1.write_on_state('standby', 25)
i_str = i1.generate_str()
print(i_str)
n = NameGenerator()
print(n.generate_name('a'))
print(n.generate_name('a'))
print(n.generate_name('b'))
print(n.generate_name('b'))