Skip to content

Commit cee83b2

Browse files
committed
Unify API and adopt readme
1 parent e2f8b84 commit cee83b2

File tree

3 files changed

+63
-54
lines changed

3 files changed

+63
-54
lines changed

README.rst

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -148,34 +148,33 @@ This code during execution will produce log records:
148148
Calling:
149149
'example_function1'(
150150
# POSITIONAL_ONLY:
151-
'arg0'=u'''arg0''', # type: <class 'str'>
151+
arg0='arg0', # type: str
152152
# POSITIONAL_OR_KEYWORD:
153-
'arg1'=u'''arg1''', # type: <class 'str'>
154-
'arg2'=u'''arg2''', # type: <class 'str'>
153+
arg1='arg1', # type: str
154+
arg2='arg2', # type: str
155155
# VAR_POSITIONAL:
156-
'args'=(),
156+
args=(),
157157
# KEYWORD_ONLY:
158-
'kwarg1'=u'''kwarg1''', # type: <class 'str'>
159-
'kwarg2'=u'''kwarg2''', # type: <class 'str'>
158+
kwarg1='kwarg1', # type: str
159+
kwarg2='kwarg2', # type: str
160160
# VAR_KEYWORD:
161-
'kwargs'=
162-
dict({
163-
'kwarg3': u'''kwarg3''',
164-
}),
161+
kwargs={
162+
'kwarg3': 'kwarg3',
163+
},
165164
)
166165
Done: 'example_function1' with result:
167166

168-
tuple((
169-
u'''arg0''',
170-
u'''arg1''',
171-
u'''arg2''',
167+
(
168+
'arg0',
169+
'arg1',
170+
'arg2',
172171
(),
173-
u'''kwarg1''',
174-
u'''kwarg2''',
175-
dict({
176-
'kwarg3': u'''kwarg3''',
177-
}),
178-
))
172+
'kwarg1',
173+
'kwarg2',
174+
{
175+
'kwarg3': 'kwarg3',
176+
},
177+
)
179178

180179
LogWrap
181180
-------
@@ -217,7 +216,6 @@ Signature is self-documenting:
217216
indent_step=4, # step between indents
218217
)
219218
220-
Limitation: Dict like objects is always marked inside `{}` for readability, even if it is `collections.OrderedDict` (standard repr as list of tuples).
221219
222220
pretty_str
223221
----------

logwrap/repr_utils.py

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -233,21 +233,21 @@ def _repr_simple(self, src: typing.Any, indent: int = 0, no_indent_start: bool =
233233
:return: simple repr() over object
234234
:rtype: str
235235
"""
236-
raise NotImplementedError()
237236

238237
@abc.abstractmethod
239-
def _repr_dict_items(self, src: typing.Dict[typing.Any, typing.Any], indent: int = 0) -> typing.Iterator[str]:
238+
def _repr_dict_items(self, src: typing.Dict[typing.Any, typing.Any], indent: int = 0) -> str:
240239
"""Repr dict items.
241240
242241
:param src: object to process
243242
:type src: typing.Dict
244243
:param indent: start indentation
245244
:type indent: int
246-
:rtype: typing.Iterator[str]
245+
:return: repr of key/value pairs from dict
246+
:rtype: str
247247
"""
248-
raise NotImplementedError()
249248

250249
@staticmethod
250+
@abc.abstractmethod
251251
def _repr_iterable_item(
252252
obj_type: str, prefix: str, indent: int, no_indent_start: bool, result: str, suffix: str
253253
) -> str:
@@ -265,24 +265,27 @@ def _repr_iterable_item(
265265
:type result: str
266266
:param suffix: suffix
267267
:type suffix: str
268-
:return: iterable as string
268+
:return: formatted repr of "result" with prefix and suffix to explain type.
269269
:rtype: str
270270
"""
271-
raise NotImplementedError()
272271

273-
def _repr_iterable_items(self, src: typing.Iterable[typing.Any], indent: int = 0) -> typing.Iterator[str]:
272+
def _repr_iterable_items(self, src: typing.Iterable[typing.Any], indent: int = 0) -> str:
274273
"""Repr iterable items (not designed for dicts).
275274
276275
:param src: object to process
277276
:type src: typing.Iterable
278277
:param indent: start indentation
279278
:type indent: int
280-
:return: repr of element in iterable item
281-
:rtype: typing.Iterator[str]
279+
:return: repr of elements in iterable item
280+
:rtype: str
282281
"""
283282
next_indent: int = self.next_indent(indent)
283+
buf: typing.List[str] = []
284284
for elem in src:
285-
yield "\n" + self.process_element(src=elem, indent=next_indent) + ","
285+
buf.append("\n")
286+
buf.append(self.process_element(src=elem, indent=next_indent))
287+
buf.append(",")
288+
return "".join(buf)
286289

287290
@property
288291
@abc.abstractmethod
@@ -291,7 +294,6 @@ def _magic_method_name(self) -> str:
291294
292295
:rtype: str
293296
"""
294-
raise NotImplementedError()
295297

296298
def process_element(self, src: typing.Any, indent: int = 0, no_indent_start: bool = False) -> str:
297299
"""Make human readable representation of object.
@@ -317,15 +319,15 @@ def process_element(self, src: typing.Any, indent: int = 0, no_indent_start: boo
317319

318320
if isinstance(src, dict):
319321
prefix, suffix = "{", "}"
320-
result = "".join(self._repr_dict_items(src=src, indent=indent))
322+
result = self._repr_dict_items(src=src, indent=indent)
321323
else:
322324
if isinstance(src, list):
323325
prefix, suffix = "[", "]"
324326
elif isinstance(src, tuple):
325327
prefix, suffix = "(", ")"
326328
else:
327329
prefix, suffix = "{", "}"
328-
result = "".join(self._repr_iterable_items(src=src, indent=indent))
330+
result = self._repr_iterable_items(src=src, indent=indent)
329331
if type(src) in (list, tuple, set, dict): # pylint: disable=unidiomatic-typecheck
330332
return f"{'':<{indent if not no_indent_start else 0}}{prefix}{result}\n{'':<{indent}}{suffix}"
331333
return self._repr_iterable_item(
@@ -383,23 +385,26 @@ def _repr_simple(self, src: typing.Any, indent: int = 0, no_indent_start: bool =
383385
"""
384386
return f"{'':<{0 if no_indent_start else indent}}{src!r}"
385387

386-
def _repr_dict_items(self, src: typing.Dict[typing.Any, typing.Any], indent: int = 0) -> typing.Iterator[str]:
388+
def _repr_dict_items(self, src: typing.Dict[typing.Any, typing.Any], indent: int = 0) -> str:
387389
"""Repr dict items.
388390
389391
:param src: object to process
390-
:type src: dict
392+
:type src: typing.Dict
391393
:param indent: start indentation
392394
:type indent: int
393-
:return: repr of key/value pair from dict
394-
:rtype: typing.Iterator[str]
395+
:return: repr of key/value pairs from dict
396+
:rtype: str
395397
"""
396398
max_len: int = max((len(repr(key)) for key in src)) if src else 0
397399
next_indent: int = self.next_indent(indent)
398400
prefix: str = "\n" + " " * next_indent
401+
buf: typing.List[str] = []
399402
for key, val in src.items():
400-
yield prefix + f"{key!r:{max_len}}: " + self.process_element(
401-
val, indent=next_indent, no_indent_start=True
402-
) + ","
403+
buf.append(prefix)
404+
buf.append(f"{key!r:{max_len}}: ")
405+
buf.append(self.process_element(val, indent=next_indent, no_indent_start=True))
406+
buf.append(",")
407+
return "".join(buf)
403408

404409
@staticmethod
405410
def _repr_iterable_item(
@@ -419,7 +424,7 @@ def _repr_iterable_item(
419424
:type result: str
420425
:param suffix: suffix
421426
:type suffix: str
422-
:return: iterable as string
427+
:return: formatted repr of "result" with prefix and suffix to explain type.
423428
:rtype: str
424429
"""
425430
return f"{'':<{indent if not no_indent_start else 0}}{obj_type}({prefix}{result}\n{'':<{indent}}{suffix})"
@@ -467,23 +472,26 @@ def _repr_simple(self, src: typing.Any, indent: int = 0, no_indent_start: bool =
467472
return self._strings_str(indent=indent, val=src)
468473
return f"{'':<{indent}}{src!s}"
469474

470-
def _repr_dict_items(self, src: typing.Dict[typing.Any, typing.Any], indent: int = 0) -> typing.Iterator[str]:
475+
def _repr_dict_items(self, src: typing.Dict[typing.Any, typing.Any], indent: int = 0) -> str:
471476
"""Repr dict items.
472477
473478
:param src: object to process
474-
:type src: dict
479+
:type src: typing.Dict
475480
:param indent: start indentation
476481
:type indent: int
477-
:return: repr of key/value pair from dict
478-
:rtype: typing.Iterator[str]
482+
:return: repr of key/value pairs from dict
483+
:rtype: str
479484
"""
480485
max_len = max((len(str(key)) for key in src)) if src else 0
481486
next_indent: int = self.next_indent(indent)
482487
prefix: str = "\n" + " " * next_indent
488+
buf: typing.List[str] = []
483489
for key, val in src.items():
484-
yield prefix + f"{key!s:{max_len}}: " + self.process_element(
485-
val, indent=next_indent, no_indent_start=True
486-
) + ","
490+
buf.append(prefix)
491+
buf.append(f"{key!s:{max_len}}: ")
492+
buf.append(self.process_element(val, indent=next_indent, no_indent_start=True))
493+
buf.append(",")
494+
return "".join(buf)
487495

488496
@staticmethod
489497
def _repr_iterable_item(
@@ -503,7 +511,7 @@ def _repr_iterable_item(
503511
:type result: str
504512
:param suffix: suffix
505513
:type suffix: str
506-
:return: iterable as string
514+
:return: formatted repr of "result" with prefix and suffix to explain type.
507515
:rtype: str
508516
"""
509517
return f"{'':<{indent if not no_indent_start else 0}}{prefix}{result}\n{'':<{indent}}{suffix}"

logwrap/repr_utils.pyx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ cdef class PrettyFormat:
225225
:type result: str
226226
:param suffix: suffix
227227
:type suffix: str
228-
:return: iterable as string
228+
:return: formatted repr of "result" with prefix and suffix to explain type.
229229
:rtype: str
230230
"""
231231
raise NotImplementedError()
@@ -241,6 +241,7 @@ cdef class PrettyFormat:
241241
:type src: typing.Dict
242242
:param indent: start indentation
243243
:type indent: int
244+
:return: repr of key/value pairs from dict
244245
:rtype: typing.Iterator[str]
245246
"""
246247
raise NotImplementedError()
@@ -256,7 +257,7 @@ cdef class PrettyFormat:
256257
:type src: typing.Iterable
257258
:param indent: start indentation
258259
:type indent: int
259-
:return: repr of element in iterable item
260+
:return: repr of elements in iterable item
260261
:rtype: str
261262
"""
262263
cdef:
@@ -400,7 +401,7 @@ cdef class PrettyRepr(PrettyFormat):
400401
:type result: str
401402
:param suffix: suffix
402403
:type suffix: str
403-
:return: iterable as string
404+
:return: formatted repr of "result" with prefix and suffix to explain type.
404405
:rtype: str
405406
"""
406407
return f"{'':<{indent if not no_indent_start else 0}}{obj_type}({prefix}{result}\n{'':<{indent}}{suffix})"
@@ -416,6 +417,7 @@ cdef class PrettyRepr(PrettyFormat):
416417
:type src: typing.Dict
417418
:param indent: start indentation
418419
:type indent: int
420+
:return: repr of key/value pairs from dict
419421
:rtype: str
420422
"""
421423
cdef:
@@ -500,7 +502,7 @@ cdef class PrettyStr(PrettyFormat):
500502
:type result: str
501503
:param suffix: suffix
502504
:type suffix: str
503-
:return: iterable as string
505+
:return: formatted repr of "result" with prefix and suffix to explain type.
504506
:rtype: str
505507
"""
506508
return f"{'':<{indent if not no_indent_start else 0}}{prefix}{result}\n{'':<{indent}}{suffix}"
@@ -516,6 +518,7 @@ cdef class PrettyStr(PrettyFormat):
516518
:type src: typing.Dict
517519
:param indent: start indentation
518520
:type indent: int
521+
:return: repr of key/value pairs from dict
519522
:rtype: str
520523
"""
521524
cdef:

0 commit comments

Comments
 (0)