MCPcopy Index your code
hub / github.com/google/python-fire / _DictAsString

Function _DictAsString

fire/core.py:305–337  ·  view source on GitHub ↗

Returns a dict as a string. Args: result: The dict to convert to a string verbose: Whether to include 'hidden' members, those keys starting with _. Returns: A string representing the dict

(result, verbose=False)

Source from the content-addressed store, hash-verified

303
304
305def _DictAsString(result, verbose=False):
306 """Returns a dict as a string.
307
308 Args:
309 result: The dict to convert to a string
310 verbose: Whether to include 'hidden' members, those keys starting with _.
311 Returns:
312 A string representing the dict
313 """
314
315 # We need to do 2 iterations over the items in the result dict
316 # 1) Getting visible items and the longest key for output formatting
317 # 2) Actually construct the output lines
318 class_attrs = inspectutils.GetClassAttrsDict(result)
319 result_visible = {
320 key: value for key, value in result.items()
321 if completion.MemberVisible(result, key, value,
322 class_attrs=class_attrs, verbose=verbose)
323 }
324
325 if not result_visible:
326 return '{}'
327
328 longest_key = max(len(str(key)) for key in result_visible.keys())
329 format_string = f'{{key:{longest_key + 1}s}} {{value}}'
330
331 lines = []
332 for key, value in result.items():
333 if completion.MemberVisible(result, key, value, class_attrs=class_attrs,
334 verbose=verbose):
335 line = format_string.format(key=f'{key}:', value=_OneLineResult(value))
336 lines.append(line)
337 return '\n'.join(lines)
338
339
340def _OneLineResult(result):

Callers 1

_PrintResultFunction · 0.85

Calls 1

_OneLineResultFunction · 0.85

Tested by

no test coverage detected