class DiaryManager: def __init__(self): self.tag_dict = TagDict() self.word_set = WordSet() self.record_list = RecordList() def add_record( self, dt: Optional[datetime] = None, tag: str = '', is_active: Optional[bool] = None, note: str = '', update: bool = False, ): tag_id = self._tag_to_tag_id(tag) self.word_set.check_note(note, update) self.record_list.append(Record(dt, tag_id, is_active, note)) def update_record( self, index: int, dt: Optional[datetime], tag: str, is_active: Optional[bool], note: str, update: bool = False, ): tag_id = self._tag_to_tag_id(tag) self.word_set.check_note(note, update) self.record_list[index] = Record(dt, tag_id, is_active, note) def _tag_to_tag_id(self, tag) -> int: return self.tag_dict.get_id(tag) def tag_stat(self) -> str: tag_id_stat = self.record_list.get_tag_id_stat() for tag_id in self.tag_dict: if tag_id not in tag_id_stat: tag_id_stat[tag_id] = 0 result = [(v, self.tag_dict[k]) for k, v in tag_id_stat.items()] result.sort(reverse=True) return '\n'.join(f'{item[0]} {item[1]}' for item in result) def set_tag_id(self, new_id: int, tag: str): old_id = self.tag_dict.set_id(new_id, tag) self.record_list.swap_tag_id(old_id, new_id) def tag_filter(self, tag: Optional[str] = None) -> Tuple[List[int], List[str]]: if tag is None: return ( list(range(len(self.record_list))), [str(record) for record in self.record_list], ) else: return self.record_list.filter_record( tag_id=self.tag_dict.get_id(tag))
class TestWordSet(unittest.TestCase): def setUp(self) -> None: db.load() self._word_set = WordSet() def test_check_note(self): with self.assertRaises(Exception): self._word_set.check_note('test') def test_check_note_update(self): self._word_set.check_note('test', update=True) def tearDown(self) -> None: db.clear() db.save()
def test_wordset(): # given inp = [ 'your', 'computer', 'input', 'input', ] sentence = 'Your bla bla bla rodent input.' occurences = 2 # when result = WordSet(inp).count_occurrences(sentence) # then assert occurences == result
def test_mouse_vs_mouse(): # given computer_mouse_mock = WordSet([]) animal_mock = WordSet([]) computer_mouse_mock.count_occurrences = MagicMock(return_value=10) animal_mock.count_occurrences = MagicMock(return_value=5) inp = object() expected = ['computer-mouse'] # when output = list(mouse_vs_mouse(computer_mouse_mock, animal_mock, [inp])) # then assert expected == output computer_mouse_mock.count_occurrences.assert_called_once_with(inp) animal_mock.count_occurrences.assert_called_once_with(inp)
if computer_mouse_wordset.count_occurrences(sentence) >= animal_wordset.count_occurrences(sentence): yield 'computer-mouse' else: yield 'animal' def file_reader(file_name): """Return N rows of the file, where N is in first row of the file (N is not included) :param file_name: absolute or relative file name :return: content of the file """ with open(file_name, 'rt') as input_file: n = int(input_file.readline()) # TODO: handle if not a integer for _ in range(n): yield input_file.readline() if __name__ == '__main__': parser = argparse.ArgumentParser(description='Mouse vs Mouse') parser.add_argument('input', help='input file') parser.add_argument('--animal', help='animal set', default='animal.txt') parser.add_argument('--computer-mouse', help='computer mouse set', default='computer-mouse.txt') args = parser.parse_args() computer_mouse_wordset = WordSet(open(args.computer_mouse, 'rt').readlines()) animal_wordset = WordSet(open(args.animal, 'rt').readlines()) for output in mouse_vs_mouse(computer_mouse_wordset, animal_wordset, file_reader(args.input)): print(output)
def __init__(self): self.tag_dict = TagDict() self.word_set = WordSet() self.record_list = RecordList()
def test_complex(self): td = TagDict() td.add('tag') rl = RecordList() dt = datetime(year=2021, month=1, day=15, hour=21, minute=13) rl.append(Record(dt, note='test')) ws = WordSet() with self.assertRaises(Exception): ws.check_note('test') ws.check_note('test', update=True) self.assertEqual('0) tag', str(td)) self.assertEqual('[15.01.2021 21:13:00] <tag> test', str(rl)) ws.check_note('test') db.save() db.clear() self.assertEqual('', str(td)) self.assertEqual('', str(rl)) with self.assertRaises(Exception): ws.check_note('test') db.load() self.assertEqual('0) tag', str(td)) self.assertEqual('[15.01.2021 21:13:00] <tag> test', str(rl)) ws.check_note('test')
def setUp(self) -> None: db.load() self._word_set = WordSet()