-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathTextStorage.py
More file actions
29 lines (21 loc) · 738 Bytes
/
TextStorage.py
File metadata and controls
29 lines (21 loc) · 738 Bytes
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
from collections import namedtuple
from typing import List
import os
KeyValuePair = namedtuple('KeyValuePair', ['key', 'value'])
class TextStorage:
def __init__(self):
self.pairs: List[KeyValuePair] = []
def read(self, filename):
with open(filename, 'r') as fp:
for line in fp:
self.pairs.append(self.parse_tokens(line))
def write(self, filename):
with open(filename, 'w') as fp:
for pair in self.pairs:
fp.write(f'{pair.key} {pair.value}{os.linesep}')
def parse_tokens(self, string: str) -> KeyValuePair:
key, value = string.strip().split()
return KeyValuePair(
key=key,
value=value
)