Skip to content

Commit

Permalink
Update Python style
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZhihao-723 committed Jul 7, 2024
1 parent 5250f90 commit 8627d0c
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 136 deletions.
35 changes: 16 additions & 19 deletions clp_ffi_py/ir/query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ class QueryBuilderException(Exception):

class QueryBuilder:
"""
This class serves as an interface for conveniently constructing Query
objects utilized in CLP IR streaming search. It provides methods for
configuring and resetting search parameters.
This class serves as an interface for conveniently constructing Query objects utilized in CLP IR
streaming search. It provides methods for configuring and resetting search parameters.
For more details about the search query CLP IR stream supports, see
:class:`~clp_ffi_py.ir.native.Query` and
:class:`~clp_ffi_py.wildcard_query.WildcardQuery`.
:class:`~clp_ffi_py.ir.native.Query` and :class:`~clp_ffi_py.wildcard_query.WildcardQuery`.
"""

def __init__(self) -> None:
Expand Down Expand Up @@ -60,26 +58,25 @@ def wildcard_queries(self) -> List[WildcardQuery]:

def set_search_time_lower_bound(self, ts: int) -> QueryBuilder:
"""
:param ts: Start of the search time range (inclusive) as a UNIX epoch
timestamp in milliseconds.
:param ts: Start of the search time range (inclusive) as a UNIX epoch timestamp in
milliseconds.
:return: self.
"""
self._search_time_lower_bound = ts
return self

def set_search_time_upper_bound(self, ts: int) -> QueryBuilder:
"""
:param ts: End of the search time range (inclusive) as a UNIX epoch
timestamp in milliseconds.
:param ts: End of the search time range (inclusive) as a UNIX epoch timestamp in
milliseconds.
:return: self.
"""
self._search_time_upper_bound = ts
return self

def set_search_time_termination_margin(self, ts: int) -> QueryBuilder:
"""
:param ts: The search time termination margin as a UNIX epoch timestamp
in milliseconds.
:param ts: The search time termination margin as a UNIX epoch timestamp in milliseconds.
:return: self.
"""
self._search_time_termination_margin = ts
Expand All @@ -92,8 +89,8 @@ def set_search_time_termination_margin(self, ts: int) -> QueryBuilder:
)
def add_wildcard_query(self, wildcard_query: str, case_sensitive: bool = False) -> QueryBuilder:
"""
Constructs and adds a :class:`~clp_ffi_py.wildcard_query.WildcardQuery`
to the wildcard query list.
Constructs and adds a :class:`~clp_ffi_py.wildcard_query.WildcardQuery` to the wildcard
query list.
:param wildcard_query: The wildcard query string to add.
:param case_sensitive: Whether to perform case-sensitive matching.
Expand All @@ -106,8 +103,8 @@ def add_wildcard_query(self, wildcard_query: WildcardQuery) -> QueryBuilder:
"""
Adds the given wildcard query to the wildcard query list.
:param wildcard_query: The wildcard query to add. It can be any derived
class of :class:`~clp_ffi_py.wildcard_query.WildcardQuery`.
:param wildcard_query: The wildcard query to add. It can be any derived class of
:class:`~clp_ffi_py.wildcard_query.WildcardQuery`.
:return: self.
"""
...
Expand Down Expand Up @@ -189,10 +186,10 @@ def reset(self) -> QueryBuilder:

def build(self) -> Query:
"""
:raises QueryBuilderException: If the search time range lower bound
exceeds the search time range upper bound.
:return: A :class:`~clp_ffi_py.ir.native.Query` object initialized
with the parameters set by the builder.
:raises QueryBuilderException: If the search time range lower bound exceeds the search time
range upper bound.
:return: A :class:`~clp_ffi_py.ir.native.Query` object initialized with the parameters set
by the builder.
"""
if self._search_time_lower_bound > self._search_time_upper_bound:
raise QueryBuilderException(
Expand Down
33 changes: 15 additions & 18 deletions clp_ffi_py/ir/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@

class ClpIrStreamReader(Iterator[LogEvent]):
"""
This class represents a stream reader used to read/decode encoded log events
from a CLP IR stream. It also provides method(s) to instantiate a log event
generator with a customized search query.
This class represents a stream reader used to read/decode encoded log events from a CLP IR
stream. It also provides method(s) to instantiate a log event generator with a customized search
query.
:param istream: Input stream that contains encoded CLP IR.
:param decoder_buffer_size: Initial size of the decoder buffer.
:param enable_compression: A flag indicating whether the istream is
compressed using `zstd`.
:param allow_incomplete_stream: If set to `True`, an incomplete CLP IR
stream is not treated as an error. Instead, encountering such a stream
is seen as reaching its end without raising any exceptions.
:param enable_compression: A flag indicating whether the istream is compressed using `zstd`.
:param allow_incomplete_stream: If set to `True`, an incomplete CLP IR stream is not treated as
an error. Instead, encountering such a stream is seen as reaching its end without raising
any exceptions.
"""

DEFAULT_DECODER_BUFFER_SIZE: int = 65536
Expand Down Expand Up @@ -52,19 +51,17 @@ def read_next_log_event(self) -> Optional[LogEvent]:
- Next unread log event represented as an instance of LogEvent.
- None if the end of IR stream is reached.
:raise Exception:
If :meth:`~clp_ffi_py.ir.native.Decoder.decode_next_log_event`
fails.
If :meth:`~clp_ffi_py.ir.native.Decoder.decode_next_log_event` fails.
"""
return Decoder.decode_next_log_event(
self._decoder_buffer, allow_incomplete_stream=self._allow_incomplete_stream
)

def read_preamble(self) -> None:
"""
Try to decode the preamble and set `metadata`. If `metadata` has been
set already, it will instantly return. It is separated from `__init__`
so that the input stream does not need to be readable on a reader's
construction, but until the user starts to iterate logs.
Try to decode the preamble and set `metadata`. If `metadata` has been set already, it will
instantly return. It is separated from `__init__` so that the input stream does not need to
be readable on a reader's construction, but until the user starts to iterate logs.
:raise Exception:
If :meth:`~clp_ffi_py.ir.native.Decoder.decode_preamble` fails.
Expand All @@ -85,10 +82,10 @@ def search(self, query: Query) -> Generator[LogEvent, None, None]:
"""
Searches and yields log events that match a specific search query.
:param query: The input query object used to match log events. Check the
document of :class:`~clp_ffi_py.ir.Query` for more details.
:yield: The next unread encoded log event that matches the given search
query from the IR stream.
:param query: The input query object used to match log events. Check the document of
:class:`~clp_ffi_py.ir.Query` for more details.
:yield: The next unread encoded log event that matches the given search query from the IR
stream.
"""
if False is self.has_metadata():
self.read_preamble()
Expand Down
7 changes: 3 additions & 4 deletions clp_ffi_py/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@

def get_formatted_timestamp(timestamp: int, timezone: Optional[tzinfo]) -> str:
"""
Gets the formatted timestamp string from the provided timestamp with the
provided timezone using isoformat.
Gets the formatted timestamp string from the provided timestamp with the provided timezone using
isoformat.
:param timestamp: Timestamp to format.
:param timezone: Timezone of timestamp parameter. If None is given, UTC is
used by default.
:param timezone: Timezone of timestamp parameter. If None is given, UTC is used by default.
:return: String of formatted timestamp.
"""
if timezone is None:
Expand Down
31 changes: 14 additions & 17 deletions clp_ffi_py/wildcard_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@

class WildcardQuery:
"""
An abstract class defining a wildcard query. Users should instantiate a
wildcard query through :class:`SubstringWildcardQuery` or
:class:`FullStringWildcardQuery`.
An abstract class defining a wildcard query. Users should instantiate a wildcard query through
:class:`SubstringWildcardQuery` or :class:`FullStringWildcardQuery`.
A wildcard string may contain the following types of wildcards:
1. '*': match 0 or more characters.
2. '?': match any single character.
Each wildcard can be escaped using a preceding '\\\\' (a single backslash).
Other characters that are escaped are treated as normal characters.
Each wildcard can be escaped using a preceding '\\\\' (a single backslash). Other characters
that are escaped are treated as normal characters.
"""

@deprecated(
Expand Down Expand Up @@ -61,12 +60,11 @@ def case_sensitive(self) -> bool:

class SubstringWildcardQuery(WildcardQuery):
"""
A wildcard query that can match a substring in a log event's message, in
contrast with :class:`FullStringWildcardQuery` where the query needs to
match the entire message.
A wildcard query that can match a substring in a log event's message, in contrast with
:class:`FullStringWildcardQuery` where the query needs to match the entire message.
This class is derived from :class:`WildcardQuery` by adding both a prefix
and a postfix wildcard ("*") to the input wildcard string.
This class is derived from :class:`WildcardQuery` by adding both a prefix and a postfix wildcard
("*") to the input wildcard string.
"""

def __init__(self, substring_wildcard_query: str, case_sensitive: bool = False):
Expand All @@ -84,15 +82,14 @@ def __init__(self, substring_wildcard_query: str, case_sensitive: bool = False):

class FullStringWildcardQuery(WildcardQuery):
"""
A wildcard query where the query must match the entirety of the log event's
message, in contrast with :class:`SubstringWildcardQuery` where the query
only needs to match a substring.
A wildcard query where the query must match the entirety of the log event's message, in contrast
with :class:`SubstringWildcardQuery` where the query only needs to match a substring.
This class is derived from :class:`WildcardQuery` as a more explicit
interface for full-string matches.
This class is derived from :class:`WildcardQuery` as a more explicit interface for full-string
matches.
Users can create a match that's anchored to only one end of the message by
adding a prefix OR postfix wildcard ("*").
Users can create a match that's anchored to only one end of the message by adding a prefix OR
postfix wildcard ("*").
"""

def __init__(self, full_string_wildcard_query: str, case_sensitive: bool = False):
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ archs = ["AMD64"]
make-summary-multi-line = true
pre-summary-newline = true
recursive = true
wrap-summaries = 80
wrap-descriptions = 80
wrap-summaries = 100
wrap-descriptions = 100

[tool.mypy]
explicit_package_bases = true
Expand Down
6 changes: 3 additions & 3 deletions tests/test_ir/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

def add_tests(suite: unittest.TestSuite, loader: unittest.TestLoader, test_class: type) -> None:
"""
Recursively collect tests from concrete classes. Although Test*Base classes
are functionally abstract to the user we cannot properly make them abstract
as `unittest` will still create instances of these classes.
Recursively collect tests from concrete classes. Although Test*Base classes are functionally
abstract to the user we cannot properly make them abstract as `unittest` will still create
instances of these classes.
:param suite: test suite to add found tests to
:param loader: load test from `unittest.TestCase` class
Expand Down
48 changes: 22 additions & 26 deletions tests/test_ir/test_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def _generate_random_query(
self, ref_log_events: List[LogEvent]
) -> Tuple[Query, List[LogEvent]]:
"""
Generates a random query and return all the log events in the given
`log_events` that matches this random query.
Generates a random query and return all the log events in the given `log_events` that
matches this random query.
The derived class might overwrite this method to generate a random query
using customized algorithm. By default, this function returns an empty
Expand All @@ -120,13 +120,13 @@ def _decode_log_stream(
self, log_path: Path, query: Optional[Query]
) -> Tuple[Metadata, List[LogEvent]]:
"""
Decodes the log stream specified by `log_path`, using decoding methods
provided in clp_ffi_py.ir.Decoder.
Decodes the log stream specified by `log_path`, using decoding methods provided in
clp_ffi_py.ir.Decoder.
:param log_path: The path to the log stream.
:param query: Optional search query.
:return: A tuple that contains the decoded metadata and log events
returned from decoding methods.
:return: A tuple that contains the decoded metadata and log events returned from decoding
methods.
"""
with open(str(log_path), "rb") as istream:
decoder_buffer: DecoderBuffer = DecoderBuffer(istream)
Expand All @@ -152,11 +152,9 @@ def _validate_decoded_logs(
Validates decoded logs from the IR stream specified by `log_path`.
:param ref_metadata: Reference metadata.
:param ref_log_events: A list of reference log events sequence (order
sensitive).
:param ref_log_events: A list of reference log events sequence (order sensitive).
:param decoded_metadata: Metadata decoded from the IR stream.
:param decoded_log_events: A list of log events decoded from the IR
stream in sequence.
:param decoded_log_events: A list of log events decoded from the IR stream in sequence.
:param log_path: Local path of the IR stream.
:param seed: Random seed used to generate the log events sequence.
"""
Expand Down Expand Up @@ -249,8 +247,7 @@ def setUp(self) -> None:

class TestCaseDecoderDecompressDefaultQuery(TestCaseDecoderBase):
"""
Tests encoding/decoding methods against uncompressed IR stream with the
default empty query.
Tests encoding/decoding methods against uncompressed IR stream with the default empty query.
"""

# override
Expand All @@ -263,8 +260,7 @@ def setUp(self) -> None:

class TestCaseDecoderDecompressZstdDefaultQuery(TestCaseDecoderBase):
"""
Tests encoding/decoding methods against zstd compressed IR stream with the
default empty query.
Tests encoding/decoding methods against zstd compressed IR stream with the default empty query.
"""

# override
Expand Down Expand Up @@ -300,8 +296,8 @@ def _generate_random_query(

class TestCaseDecoderTimeRangeQuery(TestCaseDecoderTimeRangeQueryBase):
"""
Tests encoding/decoding methods against uncompressed IR stream with the
query that specifies a search timestamp.
Tests encoding/decoding methods against uncompressed IR stream with the query that specifies a
search timestamp.
"""

# override
Expand All @@ -314,8 +310,8 @@ def setUp(self) -> None:

class TestCaseDecoderTimeRangeQueryZstd(TestCaseDecoderTimeRangeQueryBase):
"""
Tests encoding/decoding methods against zstd compressed IR stream with the
query that specifies a search timestamp.
Tests encoding/decoding methods against zstd compressed IR stream with the query that specifies
a search timestamp.
"""

# override
Expand Down Expand Up @@ -345,8 +341,8 @@ def _generate_random_query(

class TestCaseDecoderWildcardQuery(TestCaseDecoderWildcardQueryBase):
"""
Tests encoding/decoding methods against uncompressed IR stream with the
query that specifies wildcard queries.
Tests encoding/decoding methods against uncompressed IR stream with the query that specifies
wildcard queries.
"""

# override
Expand All @@ -359,8 +355,8 @@ def setUp(self) -> None:

class TestCaseDecoderWildcardQueryZstd(TestCaseDecoderWildcardQueryBase):
"""
Tests encoding/decoding methods against zstd compressed IR stream with the
query that specifies a wildcard queries.
Tests encoding/decoding methods against zstd compressed IR stream with the query that specifies
a wildcard queries.
"""

# override
Expand Down Expand Up @@ -400,8 +396,8 @@ def _generate_random_query(

class TestCaseDecoderTimeRangeWildcardQuery(TestCaseDecoderTimeRangeWildcardQueryBase):
"""
Tests encoding/decoding methods against uncompressed IR stream with the
query that specifies both search time range and wildcard queries.
Tests encoding/decoding methods against uncompressed IR stream with the query that specifies
both search time range and wildcard queries.
"""

# override
Expand All @@ -414,8 +410,8 @@ def setUp(self) -> None:

class TestCaseDecoderTimeRangeWildcardQueryZstd(TestCaseDecoderTimeRangeWildcardQueryBase):
"""
Tests encoding/decoding methods against zstd compressed IR stream with the
query that specifies both search time range and wildcard queries.
Tests encoding/decoding methods against zstd compressed IR stream with the query that specifies
both search time range and wildcard queries.
"""

# override
Expand Down
3 changes: 1 addition & 2 deletions tests/test_ir/test_decoder_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ def __launch_test(self, buffer_capacity: Optional[int]) -> None:
Tests the DecoderBuffer by streaming the files inside `test_src_dir`.
:param self
:param buffer_capacity: The buffer capacity used to initialize the
decoder buffer.
:param buffer_capacity: The buffer capacity used to initialize the decoder buffer.
"""
current_dir: Path = Path(__file__).resolve().parent
test_src_dir: Path = current_dir / TestCaseDecoderBuffer.input_src_dir
Expand Down
Loading

0 comments on commit 8627d0c

Please sign in to comment.