| 513 | GenPythonOp::~GenPythonOp() {} |
| 514 | |
| 515 | string GenPythonOp::Code() { |
| 516 | // This has all the input args followed by those attrs that don't have |
| 517 | // defaults. |
| 518 | std::vector<ParamNames> params_no_default; |
| 519 | // The parameters with defaults (these have to be listed after those without). |
| 520 | // No input args are included, just attrs. |
| 521 | std::vector<ParamNames> params_with_default; |
| 522 | |
| 523 | for (int i = 0; i < api_def_.arg_order_size(); ++i) { |
| 524 | const auto& arg = *FindInputArg(api_def_.arg_order(i), op_def_); |
| 525 | const auto& api_def_arg = *FindInputArg(api_def_.arg_order(i), api_def_); |
| 526 | params_no_default.emplace_back(api_def_arg.name(), api_def_arg.rename_to()); |
| 527 | if (!arg.type_attr().empty()) { |
| 528 | gtl::InsertIfNotPresent(&inferred_attrs_, arg.type_attr(), arg.name()); |
| 529 | } else if (!arg.type_list_attr().empty()) { |
| 530 | gtl::InsertIfNotPresent(&inferred_attrs_, arg.type_list_attr(), |
| 531 | arg.name()); |
| 532 | } |
| 533 | if (!arg.number_attr().empty()) { |
| 534 | gtl::InsertIfNotPresent(&inferred_attrs_, arg.number_attr(), arg.name()); |
| 535 | } |
| 536 | } |
| 537 | for (int i = 0; i < api_def_.attr_size(); ++i) { |
| 538 | const auto& attr(api_def_.attr(i)); |
| 539 | // Do not add inferred attrs to the Python function signature. |
| 540 | if (inferred_attrs_.find(attr.name()) == inferred_attrs_.end()) { |
| 541 | if (attr.has_default_value()) { |
| 542 | params_with_default.emplace_back(attr.name(), attr.rename_to()); |
| 543 | } else { |
| 544 | params_no_default.emplace_back(attr.name(), attr.rename_to()); |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | // Save the list of attr parameters (attrs that won't be inferred), |
| 550 | // those with defaults go at the end. |
| 551 | // Get the attrs in the order we want by taking the attrs without defaults |
| 552 | // from the end of args_no_default, and adding args_no_default. |
| 553 | attrs_.reserve(params_no_default.size() - op_def_.input_arg_size() + |
| 554 | params_with_default.size()); |
| 555 | for (int i = op_def_.input_arg_size(); i < params_no_default.size(); ++i) { |
| 556 | attrs_.push_back(params_no_default[i].GetName()); |
| 557 | } |
| 558 | for (int i = 0; i < params_with_default.size(); ++i) { |
| 559 | attrs_.push_back(params_with_default[i].GetName()); |
| 560 | } |
| 561 | |
| 562 | param_names_.reserve(params_no_default.size() + params_with_default.size()); |
| 563 | param_names_.insert(param_names_.begin(), params_no_default.begin(), |
| 564 | params_no_default.end()); |
| 565 | for (const auto& param : params_with_default) { |
| 566 | param_names_.push_back(param); |
| 567 | } |
| 568 | |
| 569 | string parameters; |
| 570 | for (const auto& param : params_no_default) { |
| 571 | AddDelimiter(¶meters, ", "); |
| 572 | strings::StrAppend(¶meters, param.GetRenameTo()); |
nothing calls this directly
no test coverage detected