Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GetValidName() throughout Python bindings #3302

Merged
merged 3 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/mlpack/bindings/python/print_doc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ void PrintDoc(util::ParamData& d,
const size_t indent = *((size_t*) input);
std::ostringstream oss;
oss << " - ";
if (d.name == "lambda") // Don't print Python keywords.
oss << d.name << "_ (";
else
oss << d.name << " (";
oss << GetValidName(d.name);
oss << " (";
oss << GetPrintableType<typename std::remove_pointer<T>::type>(d) << "): "
<< d.desc;

Expand Down
24 changes: 7 additions & 17 deletions src/mlpack/bindings/python/print_doc_functions_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,8 @@ inline std::string ProgramCall(util::Params& params,
first = false;

// Print the input option.
if (it->second.name != "lambda") // Don't print Python keywords.
oss << it->second.name << "=";
else
oss << it->second.name << "_=";
const std::string correctName = GetValidName(it->second.name);
oss << correctName << "=";

std::string value;
params.functionMap[it->second.tname]["DefaultParam"](
Expand Down Expand Up @@ -375,11 +373,9 @@ inline std::string ParamString(const std::string& paramName)
{
// For a Python binding we don't need to know the type.

// Make sure that we don't print reserved keywords.
if (paramName == "lambda")
return "'" + paramName + "_'";
else
return "'" + paramName + "'";
// Make sure that we don't print reserved keywords or builtin functions.
const std::string correctName = GetValidName(paramName);
return "'" + correctName + "'";
}

/**
Expand All @@ -390,10 +386,7 @@ template<typename T>
inline std::string ParamString(const std::string& paramName, const T& value)
{
std::ostringstream oss;
if (paramName == "lambda") // Don't print reserved keywords.
oss << paramName << "_=" << value;
else
oss << paramName << "=" << value;
oss << GetValidName(paramName) << "=" << value;
return oss.str();
}

Expand Down Expand Up @@ -485,10 +478,7 @@ inline std::string CreateObject(const std::string& bindingName,
first = false;

// Print the input option.
if (it->second.name != "lambda") // Don't print Python keywords.
createObj += it->second.name + "=";
else
createObj += it->second.name + "_=";
createObj += GetValidName(it->second.name) + "=";

std::string value;
params.functionMap[it->second.tname]["DefaultParam"](
Expand Down
11 changes: 7 additions & 4 deletions src/mlpack/bindings/python/wrapper_functions_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ inline std::string GetClassName(const std::string& groupName)

inline std::string GetValidName(const std::string& paramName)
{
std::string correctParamName = paramName;
std::string correctParamName;

if(paramName == "lambda") correctParamName = "lambda_";
else if(paramName == "input") correctParamName = "input_";
else correctParamName = paramName;
if (paramName == "lambda")
correctParamName = "lambda_";
else if (paramName == "input")
correctParamName = "input_";
else
correctParamName = paramName;
conradsnicta marked this conversation as resolved.
Show resolved Hide resolved

return correctParamName;
}
Expand Down