-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from shirte/main
Restrict output formats of ProblemListConverter
- Loading branch information
Showing
2 changed files
with
37 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,39 @@ | ||
from typing import Any, List, cast | ||
import logging | ||
from typing import Any, List, Union, cast | ||
|
||
from ..problem import Problem | ||
from .converter import Converter | ||
from .converter_config import ALL, ConverterConfig | ||
from .converter_config import ConverterConfig | ||
|
||
__all__ = ["ProblemListConverter"] | ||
__all__ = ["ProblemListIdentityConverter", "ProblemListConverter"] | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class ProblemListIdentityConverter(Converter): | ||
def _convert(self, input: Any, context: dict) -> Any: | ||
return input | ||
|
||
config = ConverterConfig( | ||
data_types="problem_list", | ||
output_formats=["pandas", "iterator", "record_list"], | ||
) | ||
|
||
|
||
class ProblemListConverter(Converter): | ||
def _convert(self, input: Any, context: dict) -> Any: | ||
if self.output_format in ["pandas", "iterator", "record_list"]: | ||
return input | ||
else: | ||
problem_list: List[Problem] = cast(List[Problem], input) | ||
return "; ".join([f"{problem.type}: {problem.message}" for problem in problem_list]) | ||
problem_list: List[Union[Problem, str]] = cast(List[Union[Problem, str]], input) | ||
|
||
def _represent(problem: Union[Problem, str]) -> str: | ||
if isinstance(problem, Problem): | ||
return f"{problem.type}: {problem.message}" | ||
else: | ||
logger.warning("Item is not an instance of Problem: %s", problem) | ||
return problem | ||
|
||
return "; ".join([_represent(problem) for problem in problem_list]) | ||
|
||
config = ConverterConfig( | ||
data_types="problem_list", | ||
output_formats=ALL, | ||
output_formats=["csv", "sdf"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters