Skip to content

Commit 18ba635

Browse files
dbiebercopybara-github
authored andcommitted
Allow custom types to define their own serialization with __str__.
PiperOrigin-RevId: 260078541 Change-Id: I2999e0caaaf962708ab1de4272b1df58327b2ada
1 parent 9629074 commit 18ba635

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

docs/guide.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ if __name__ == '__main__':
413413
Now we can draw stuff :).
414414

415415
```bash
416-
$ python example.py move 3 3 on move 3 6 on move 6 3 on move 6 6 on move 7 4 on move 7 5 on __str__
416+
$ python example.py move 3 3 on move 3 6 on move 6 3 on move 6 6 on move 7 4 on move 7 5 on
417417
0 0 0 0 0 0 0 0 0 0
418418
0 0 0 0 0 0 0 0 0 0
419419
0 0 0 0 0 0 0 0 0 0
@@ -428,6 +428,16 @@ $ python example.py move 3 3 on move 3 6 on move 6 3 on move 6 6 on move 7 4 on
428428

429429
It's supposed to be a smiley face.
430430

431+
### Custom Serialization
432+
433+
You'll notice in the BinaryCanvas example, the canvas with the smiley face was
434+
printed to the screen. You can determine how a component will be serialized by
435+
defining its `__str__` method.
436+
437+
If a custom `__str__` method is present on the final component, the object is
438+
serialized and printed. If there's no custom `__str__` method, then the help
439+
screen for the object is shown instead.
440+
431441
### Can we make an even simpler example than Hello World?
432442

433443
Yes, this program is even simpler than our original Hello World example.

docs/using-cli.md

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ the arguments of the class's \_\_init\_\_ function. Arguments must be specified
9191
by name, using the flags syntax. See the section on
9292
[calling a function](#calling-a-function) for more details.
9393

94+
Similarly, when passing arguments to a callable object (an object with a custom
95+
`__call__` function), those arguments must be passed using flags syntax.
9496

9597
## Using Flags with Fire CLIs <a name="using-flags"></a>
9698

fire/core.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,15 @@ def _PrintResult(component_trace, verbose=False):
244244
# and move serialization to its own module.
245245
result = component_trace.GetResult()
246246

247+
if hasattr(result, '__str__'):
248+
# If the object has a custom __str__ method, rather than one inherited from
249+
# object, then we use that to serialize the object.
250+
class_attrs = completion.GetClassAttrsDict(type(result))
251+
str_attr = class_attrs.get('__str__')
252+
if str_attr and str_attr.defining_class is not object:
253+
print(str(result))
254+
return
255+
247256
if isinstance(result, (list, set, frozenset, types.GeneratorType)):
248257
for i in result:
249258
print(_OneLineResult(i))
@@ -253,9 +262,6 @@ def _PrintResult(component_trace, verbose=False):
253262
print(_DictAsString(result, verbose))
254263
elif isinstance(result, tuple):
255264
print(_OneLineResult(result))
256-
elif isinstance(result, complex):
257-
# Print "3+4j" instead of "(3+4j)".
258-
print(str(result).strip('()'))
259265
elif isinstance(result, value_types.VALUE_TYPES):
260266
if result is not None:
261267
print(result)

fire/inspectutils.py

-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ def Py2GetArgSpec(fn):
9898

9999
def GetFullArgSpec(fn):
100100
"""Returns a FullArgSpec describing the given callable."""
101-
102101
original_fn = fn
103102
fn, skip_arg = _GetArgSpecInfo(fn)
104103

0 commit comments

Comments
 (0)