MCPcopy
hub / github.com/google/python-fire / _PrintResult

Function _PrintResult

fire/core.py:239–275  ·  view source on GitHub ↗

Prints the result of the Fire call to stdout in a human readable way.

(component_trace, verbose=False, serialize=None)

Source from the content-addressed store, hash-verified

237
238
239def _PrintResult(component_trace, verbose=False, serialize=None):
240 """Prints the result of the Fire call to stdout in a human readable way."""
241 # TODO(dbieber): Design human readable deserializable serialization method
242 # and move serialization to its own module.
243 result = component_trace.GetResult()
244
245 # Allow users to modify the return value of the component and provide
246 # custom formatting.
247 if serialize:
248 if not callable(serialize):
249 raise FireError(
250 'The argument `serialize` must be empty or callable:', serialize)
251 result = serialize(result)
252
253 if value_types.HasCustomStr(result):
254 # If the object has a custom __str__ method, rather than one inherited from
255 # object, then we use that to serialize the object.
256 print(str(result))
257 return
258
259 if isinstance(result, (list, set, frozenset, types.GeneratorType)):
260 for i in result:
261 print(_OneLineResult(i))
262 elif inspect.isgeneratorfunction(result):
263 raise NotImplementedError
264 elif isinstance(result, dict) and value_types.IsSimpleGroup(result):
265 print(_DictAsString(result, verbose))
266 elif isinstance(result, tuple):
267 print(_OneLineResult(result))
268 elif isinstance(result, value_types.VALUE_TYPES):
269 if result is not None:
270 print(result)
271 else:
272 help_text = helptext.HelpText(
273 result, trace=component_trace, verbose=verbose)
274 output = [help_text]
275 Display(output, out=sys.stdout)
276
277
278def _DisplayError(component_trace):

Callers 1

FireFunction · 0.85

Calls 5

FireErrorClass · 0.85
_OneLineResultFunction · 0.85
_DictAsStringFunction · 0.85
DisplayFunction · 0.85
GetResultMethod · 0.80

Tested by

no test coverage detected