-
Notifications
You must be signed in to change notification settings - Fork 0
/
classtest.py
66 lines (54 loc) · 2.15 KB
/
classtest.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
class Channel:
def __init__(self, name, length, channel, lcm):
self.name = name
self.length = length
self.contents = ['00' for i in range(length)]
self.channel = channel
self.lcm = lcm
def __repr__(self):
return f' Channel name: {self.name}'
def print_contents(self):
print(self.contents)
def modify_sample(self, sample_position: int, sample: str) -> None:
'''Modify a single sample at a given position'''
self.contents[sample_position] = sample
def modify_range(self, start: int, end: int, values: list[str]):
'''Modify a samples at a given range'''
self.contents[start:end] = values
def read_sample(self, sample_position: int) -> str:
'''Read a single sample at a given position'''
return self.contents[sample_position]
def read_range(self, start: int, end: int) -> list[str]:
'''Read samples in a given range'''
return self.contents[start:end]
def count_non_null_samples(
self,
begin: int,
end: int,
null_sample: str = '00') -> int:
'''Count non-null samples within a given range. You can specify the null sample'''
count = 0
for element in self.contents[begin : end]:
if element != null_sample:
count += 1
return count
def convert_to_string(self) -> str:
'''Convert contents of this channel to BMS format'''
content_as_str = ''.join(self.contents)
divided = [
(content_as_str[i:i+self.lcm*2])
for i in range(0, len(content_as_str), self.lcm*2)
]
output = ''
for ind, measure in enumerate(divided):
output = output + '#' + str(ind).zfill(3) + self.channel + ':' + measure + '\n'
return output
def main():
channel_test = Channel('MC1', length=64*6, lcm=64, channel='17')
print(channel_test)
channel_test.modify_sample(3, 'AA')
print(channel_test.count_non_null_samples(begin=7, end=9))
print('Converted to string:')
print(channel_test.convert_to_string())
if __name__ == '__main__':
main()