Compile a Python entity into equivalent TensorFlow. Args: o: A Python entity. program_ctx: A ProgramContext object. Returns: A tuple (ast, new_name, namespace): * ast: An AST representing an entity with interface equivalent to `o`, but which when executed it cre
(o, program_ctx)
| 444 | |
| 445 | # TODO(mdan): Rename to convert_*_node to avoid confusion with convert. |
| 446 | def convert_entity_to_ast(o, program_ctx): |
| 447 | """Compile a Python entity into equivalent TensorFlow. |
| 448 | |
| 449 | Args: |
| 450 | o: A Python entity. |
| 451 | program_ctx: A ProgramContext object. |
| 452 | |
| 453 | Returns: |
| 454 | A tuple (ast, new_name, namespace): |
| 455 | * ast: An AST representing an entity with interface equivalent to `o`, |
| 456 | but which when executed it creates TF a graph. |
| 457 | * new_name: The symbol name under which the new entity can be found. |
| 458 | * namespace: A dict mapping all symbols visible to the converted entity, |
| 459 | keyed by their symbol name. |
| 460 | |
| 461 | Raises: |
| 462 | ValueError: if the entity type is not supported. |
| 463 | """ |
| 464 | logging.log(1, 'Converting %s', o) |
| 465 | |
| 466 | if tf_inspect.isclass(o): |
| 467 | nodes, name, entity_info = convert_class_to_ast(o, program_ctx) |
| 468 | elif tf_inspect.isfunction(o): |
| 469 | nodes, name, entity_info = convert_func_to_ast(o, program_ctx) |
| 470 | elif tf_inspect.ismethod(o): |
| 471 | nodes, name, entity_info = convert_func_to_ast(o, program_ctx) |
| 472 | elif hasattr(o, '__class__'): |
| 473 | # Note: this should only be raised when attempting to convert the object |
| 474 | # directly. converted_call should still support it. |
| 475 | raise NotImplementedError( |
| 476 | 'cannot convert entity "{}": object conversion is not yet' |
| 477 | ' supported.'.format(o)) |
| 478 | else: |
| 479 | raise ValueError( |
| 480 | 'Entity "%s" has unsupported type "%s". Only functions and classes are ' |
| 481 | 'supported for now.' % (o, type(o))) |
| 482 | |
| 483 | if logging.has_verbosity(2): |
| 484 | logging.log(2, 'Compiled output of %s:\n\n%s\n', o, |
| 485 | compiler.ast_to_source(nodes)) |
| 486 | if logging.has_verbosity(4): |
| 487 | for n in nodes: |
| 488 | logging.log(4, 'Compiled AST of %s:\n\n%s\n\n', o, |
| 489 | pretty_printer.fmt(n, color=False)) |
| 490 | |
| 491 | return nodes, name, entity_info |
| 492 | |
| 493 | |
| 494 | def convert_class_to_ast(c, program_ctx): |
no test coverage detected