added 'message_in()' function and test
This commit is contained in:
parent
8e1cdee3bf
commit
b83cbb719b
@ -3,7 +3,7 @@ Module implementing message related functions and classes.
|
|||||||
"""
|
"""
|
||||||
import pathlib
|
import pathlib
|
||||||
import yaml
|
import yaml
|
||||||
from typing import Type, TypeVar, ClassVar, Optional, Any, Union, Final, Literal
|
from typing import Type, TypeVar, ClassVar, Optional, Any, Union, Final, Literal, Iterable
|
||||||
from dataclasses import dataclass, asdict, field
|
from dataclasses import dataclass, asdict, field
|
||||||
from .tags import Tag, TagLine, TagError, match_tags
|
from .tags import Tag, TagLine, TagError, match_tags
|
||||||
|
|
||||||
@ -57,6 +57,20 @@ def source_code(text: str, include_delims: bool = False) -> list[str]:
|
|||||||
return code_sections
|
return code_sections
|
||||||
|
|
||||||
|
|
||||||
|
def message_in(message: MessageInst, messages: Iterable[MessageInst]) -> bool:
|
||||||
|
"""
|
||||||
|
Searches the given message list for a message with the same file
|
||||||
|
name as the given one (i. e. it compares Message.file_path.name).
|
||||||
|
If the given message has no file_path, False is returned.
|
||||||
|
"""
|
||||||
|
if not message.file_path:
|
||||||
|
return False
|
||||||
|
for m in messages:
|
||||||
|
if m.file_path and m.file_path.name == message.file_path.name:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
@dataclass(kw_only=True)
|
@dataclass(kw_only=True)
|
||||||
class MessageFilter:
|
class MessageFilter:
|
||||||
"""
|
"""
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import pathlib
|
|||||||
import tempfile
|
import tempfile
|
||||||
from typing import cast
|
from typing import cast
|
||||||
from .test_main import CmmTestCase
|
from .test_main import CmmTestCase
|
||||||
from chatmastermind.message import source_code, Message, MessageError, Question, Answer, AILine, ModelLine, MessageFilter
|
from chatmastermind.message import source_code, Message, MessageError, Question, Answer, AILine, ModelLine, MessageFilter, message_in
|
||||||
from chatmastermind.tags import Tag, TagLine
|
from chatmastermind.tags import Tag, TagLine
|
||||||
|
|
||||||
|
|
||||||
@ -761,3 +761,17 @@ class MessageFilterTagsTestCase(CmmTestCase):
|
|||||||
self.assertSetEqual(tags_pref, {Tag('atag1')})
|
self.assertSetEqual(tags_pref, {Tag('atag1')})
|
||||||
tags_cont = self.message.filter_tags(contain='2')
|
tags_cont = self.message.filter_tags(contain='2')
|
||||||
self.assertSetEqual(tags_cont, {Tag('btag2')})
|
self.assertSetEqual(tags_cont, {Tag('btag2')})
|
||||||
|
|
||||||
|
|
||||||
|
class MessageInTestCase(CmmTestCase):
|
||||||
|
def setUp(self) -> None:
|
||||||
|
self.message1 = Message(Question('This is a question.'),
|
||||||
|
tags={Tag('atag1'), Tag('btag2')},
|
||||||
|
file_path=pathlib.Path('/tmp/foo/bla'))
|
||||||
|
self.message2 = Message(Question('This is a question.'),
|
||||||
|
tags={Tag('atag1'), Tag('btag2')},
|
||||||
|
file_path=pathlib.Path('/tmp/bla/foo'))
|
||||||
|
|
||||||
|
def test_message_in(self) -> None:
|
||||||
|
self.assertTrue(message_in(self.message1, [self.message1]))
|
||||||
|
self.assertFalse(message_in(self.message1, [self.message2]))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user