Skip to content

Commit

Permalink
nice logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ebonnal committed Dec 4, 2023
1 parent 197eade commit d0110df
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 181 deletions.
16 changes: 15 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on: [push]

jobs:
unittest:

runs-on: ubuntu-latest

steps:
Expand All @@ -19,6 +18,21 @@ jobs:
python -m pip install -r requirements.txt
python -m unittest
typing:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Set up Python 3.8
uses: actions/setup-python@v1
with:
python-version: 3.8

- name: unittest
run: |
python -m pip install -r requirements.txt
python -m mypy kioss tests
lint:
runs-on: ubuntu-latest

Expand Down
7 changes: 4 additions & 3 deletions kioss/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from kioss._pipe import APipe, SourcePipe as Pipe
from kioss._util import LOGGER
from kioss import _pipe, _visitor
_pipe.ITERATOR_GENERATING_VISITOR = _visitor.IteratorGeneratingVisitor()
_pipe.EXPLAINING_VISITOR_CLASS = _visitor.ExplainingVisitor
from kioss import _pipe
from kioss._visitors import _explain, _iterator_generation
_pipe.ITERATOR_GENERATING_VISITOR = _iterator_generation.IteratorGeneratingVisitor()
_pipe.EXPLAINING_VISITOR_CLASS = _explain.ExplainingVisitor
2 changes: 0 additions & 2 deletions kioss/_concurrent_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
from dataclasses import dataclass
from queue import Queue
from typing import (
Any,
Callable,
Iterable,
Iterator,
List,
Optional,
Set,
TypeVar,
Expand Down
16 changes: 6 additions & 10 deletions kioss/_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,12 @@ def __init__(self, iterator: Iterator[T], what: str) -> None:
_util.LOGGER.info("iteration over '%s' will be logged.", self.what)

def _log(self) -> None:
_util.LOGGER.info(
"%s `%s` have been yielded in elapsed time '%s' with %s errors produced",
self.yields_count,
self.what,
str(
datetime.fromtimestamp(time.time())
- datetime.fromtimestamp(self.start_time)
),
self.errors_count,
)
errors_summary = f"with {self.errors_count} error{'s' if self.errors_count > 1 else ''} produced"
if self.errors_count > 0:
errors_summary = _util.bold(_util.colorize_in_red(errors_summary))
yields_summary = _util.bold(f"{self.yields_count} {self.what[:-1] if self.yields_count <= 1 and self.what.endswith('s') else self.what} have been yielded")
elapsed_time = f"in elapsed time {datetime.fromtimestamp(time.time()) - datetime.fromtimestamp(self.start_time)}"
_util.LOGGER.info("%s, %s, %s", yields_summary, elapsed_time, errors_summary)

def __next__(self) -> T:
to_be_raised: Optional[Exception] = None
Expand Down
30 changes: 15 additions & 15 deletions kioss/_pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
from kioss import _util
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from kioss import _visitor
from kioss._visitors import _iterator_generation, _explain

T = TypeVar("T")
U = TypeVar("U")
R = TypeVar("R")

ITERATOR_GENERATING_VISITOR: "Optional[_visitor.IteratorGeneratingVisitor]" = None
EXPLAINING_VISITOR_CLASS: "Optional[Type[_visitor.ExplainingVisitor]]" = None
ITERATOR_GENERATING_VISITOR: "Optional[_iterator_generation.IteratorGeneratingVisitor]" = None
EXPLAINING_VISITOR_CLASS: "Optional[Type[_explain.ExplainingVisitor]]" = None

class APipe(Iterable[T], ABC):
upstream: "Optional[APipe]"
Expand All @@ -35,7 +35,7 @@ def __repr__(self) -> str:
return self._accept(EXPLAINING_VISITOR_CLASS())

@abstractmethod
def _accept(self, visitor: "_visitor.AVisitor") -> Any:
def _accept(self, visitor: "_iterator_generation.AVisitor") -> Any:
raise NotImplementedError()

def __add__(self, other: "APipe[T]") -> "APipe[T]":
Expand Down Expand Up @@ -259,7 +259,7 @@ def __init__(self, source: Callable[[], Iterator[T]]):
)
self.source = source

def _accept(self, visitor: "_visitor.AVisitor") -> Any:
def _accept(self, visitor: "_iterator_generation.AVisitor") -> Any:
return visitor.visitSourcePipe(self)

def __str__(self) -> str:
Expand All @@ -271,7 +271,7 @@ def __init__(self, upstream: APipe[T], predicate: Callable[[T], bool]):
self.upstream: APipe[T] = upstream
self.predicate = predicate

def _accept(self, visitor: "_visitor.AVisitor") -> Any:
def _accept(self, visitor: "_iterator_generation.AVisitor") -> Any:
return visitor.visitFilterPipe(self)

def __str__(self) -> str:
Expand All @@ -284,7 +284,7 @@ def __init__(self, upstream: APipe[T], func: Callable[[T], R], n_threads: int):
self.func = func
self.n_threads = n_threads

def _accept(self, visitor: "_visitor.AVisitor") -> Any:
def _accept(self, visitor: "_iterator_generation.AVisitor") -> Any:
return visitor.visitMapPipe(self)

def __str__(self) -> str:
Expand All @@ -295,7 +295,7 @@ def __init__(self, upstream: APipe[T], what: str = "elements"):
self.upstream: APipe[T] = upstream
self.what = what

def _accept(self, visitor: "_visitor.AVisitor") -> Any:
def _accept(self, visitor: "_iterator_generation.AVisitor") -> Any:
return visitor.visitLogPipe(self)

def __str__(self) -> str:
Expand All @@ -306,7 +306,7 @@ def __init__(self, upstream: APipe[Iterator[T]], n_threads: int):
self.upstream: APipe[Iterator[T]] = upstream
self.n_threads = n_threads

def _accept(self, visitor: "_visitor.AVisitor") -> Any:
def _accept(self, visitor: "_iterator_generation.AVisitor") -> Any:
return visitor.visitFlattenPipe(self)

def __str__(self) -> str:
Expand All @@ -318,7 +318,7 @@ def __init__(self, upstream: APipe[T], size: int, period: float):
self.size = size
self.period = period

def _accept(self, visitor: "_visitor.AVisitor") -> Any:
def _accept(self, visitor: "_iterator_generation.AVisitor") -> Any:
return visitor.visitBatchPipe(self)

def __str__(self) -> str:
Expand All @@ -335,29 +335,29 @@ def __init__(
self.classes = classes
self.when = when

def _accept(self, visitor: "_visitor.AVisitor") -> Any:
def _accept(self, visitor: "_iterator_generation.AVisitor") -> Any:
return visitor.visitCatchPipe(self)

def __str__(self) -> str:
return f"Catch(exceptions istances of classes [{', '.join(map(lambda class_: class_.__name__, self.classes))}]{', with an additional `when` condition' if self.when is not None else ''})"
return f"Catch(exceptions instances of classes [{', '.join(map(lambda class_: class_.__name__, self.classes))}]{', with an additional `when` condition' if self.when is not None else ''})"

class ChainPipe(APipe[T]):
def __init__(self, upstream: APipe[T], others: List[APipe]):
self.upstream: APipe[T] = upstream
self.others = others

def _accept(self, visitor: "_visitor.AVisitor") -> Any:
def _accept(self, visitor: "_iterator_generation.AVisitor") -> Any:
return visitor.visitChainPipe(self)

def __str__(self) -> str:
return f"Chain(with other pipes: [{', '.join(map(str, map(id, self.others)))}])" # TODO itricate explains
return f"Chain({len(self.others)+1} pipes)" # TODO itricate explains

class SlowPipe(APipe[T]):
def __init__(self, upstream: APipe[T], freq: float):
self.upstream: APipe[T] = upstream
self.freq = freq

def _accept(self, visitor: "_visitor.AVisitor") -> Any:
def _accept(self, visitor: "_iterator_generation.AVisitor") -> Any:
return visitor.visitSlowPipe(self)

def __str__(self) -> str:
Expand Down
11 changes: 10 additions & 1 deletion kioss/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,13 @@ def duck_check_type_is_iterator(expected_iterator: Any) -> TypeGuard[Iterator]:
f"Provided object is not an iterator because it implements the __iter__ but not the __next__ one."
)

return True
return True

def colorize_in_red(s: str) -> str:
return f"\033[91m{s}\033[0m"

def colorize_in_grey(s: str) -> str:
return f"\033[90m{s}\033[0m"

def bold(s: str) -> str:
return f"\033[1m{s}\033[0m"
148 changes: 0 additions & 148 deletions kioss/_visitor.py

This file was deleted.

41 changes: 41 additions & 0 deletions kioss/_visitors/_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from abc import ABC, abstractmethod
from typing import Any

from kioss import _pipe

class AVisitor(ABC):
@abstractmethod
def visitSourcePipe(self, pipe: _pipe.SourcePipe) -> Any:
raise NotImplementedError()

@abstractmethod
def visitMapPipe(self, pipe: _pipe.MapPipe) -> Any:
raise NotImplementedError()

@abstractmethod
def visitFlattenPipe(self, pipe: _pipe.FlattenPipe) -> Any:
raise NotImplementedError()

@abstractmethod
def visitChainPipe(self, pipe: _pipe.ChainPipe) -> Any:
raise NotImplementedError()

@abstractmethod
def visitFilterPipe(self, pipe: _pipe.FilterPipe) -> Any:
raise NotImplementedError()

@abstractmethod
def visitBatchPipe(self, pipe: _pipe.BatchPipe) -> Any:
raise NotImplementedError()

@abstractmethod
def visitSlowPipe(self, pipe: _pipe.SlowPipe) -> Any:
raise NotImplementedError()

@abstractmethod
def visitCatchPipe(self, pipe: _pipe.CatchPipe) -> Any:
raise NotImplementedError()

@abstractmethod
def visitLogPipe(self, pipe: _pipe.LogPipe) -> Any:
raise NotImplementedError()
Loading

0 comments on commit d0110df

Please sign in to comment.