|
3 | 3 | from time import sleep |
4 | 4 | from typing import Any, Dict, List, Optional, Union |
5 | 5 |
|
| 6 | +from printo import describe_call, not_none |
| 7 | + |
6 | 8 | from cantok.errors import CancellationError |
7 | 9 | from cantok.tokens.abstract.cancel_cause import CancelCause |
8 | 10 | from cantok.tokens.abstract.report import CancellationReport |
@@ -64,32 +66,27 @@ def __init__(self, *tokens: 'AbstractToken', cancelled: bool = False, doc: Optio |
64 | 66 | self._lock: RLock = RLock() |
65 | 67 |
|
66 | 68 | def __repr__(self) -> str: |
67 | | - chunks = [] |
68 | 69 | superpower = self._text_representation_of_superpower() |
69 | | - if superpower: |
70 | | - chunks.append(superpower) |
71 | | - other_tokens = ', '.join([repr(x) for x in self._tokens]) |
72 | | - if other_tokens: |
73 | | - chunks.append(other_tokens) |
74 | 70 | report = self._get_report(direct=False) |
75 | | - extra_kwargs: Dict[str, Any] |
76 | | - if report.cause == CancelCause.NOT_CANCELLED: |
77 | | - extra_kwargs = {} |
78 | | - elif report.from_token is self and report.cause == CancelCause.CANCELLED: |
79 | | - extra_kwargs = {'cancelled': True} |
80 | | - else: |
81 | | - extra_kwargs = {} |
82 | | - extra_kwargs.update(**(self._get_extra_kwargs())) |
83 | | - if self.doc is not None: |
84 | | - extra_kwargs['doc'] = self.doc |
85 | | - text_representation_of_extra_kwargs = self._text_representation_of_kwargs( |
86 | | - **extra_kwargs, |
| 71 | + cancelled = report.from_token is self and report.cause == CancelCause.CANCELLED |
| 72 | + |
| 73 | + return describe_call( |
| 74 | + type(self).__name__, |
| 75 | + [superpower, *self._tokens], |
| 76 | + { |
| 77 | + 'cancelled': cancelled, |
| 78 | + **self._get_extra_kwargs(), |
| 79 | + 'doc': self.doc, |
| 80 | + }, |
| 81 | + filters={ |
| 82 | + 0: lambda argument: bool(argument), |
| 83 | + 'cancelled': lambda argument: bool(argument), |
| 84 | + 'doc': not_none, |
| 85 | + }, |
| 86 | + placeholders={ |
| 87 | + 0: superpower, |
| 88 | + }, |
87 | 89 | ) |
88 | | - if text_representation_of_extra_kwargs: |
89 | | - chunks.append(text_representation_of_extra_kwargs) |
90 | | - |
91 | | - glued_chunks = ', '.join(chunks) |
92 | | - return f'{type(self).__name__}({glued_chunks})' |
93 | 90 |
|
94 | 91 | def __str__(self) -> str: |
95 | 92 | cancelled_flag = ( |
|
0 commit comments