-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Quality] fix c++ binaries formatting (#859)
- Loading branch information
Showing
2 changed files
with
80 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// | ||
// This source code is licensed under the MIT license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#include "utils.h" | ||
|
||
namespace py = pybind11; | ||
|
||
py::tuple _unravel_key_to_tuple(const py::object &key) { | ||
bool is_tuple = py::isinstance<py::tuple>(key); | ||
bool is_str = py::isinstance<py::str>(key); | ||
|
||
if (is_tuple) { | ||
py::list newkey; | ||
for (const auto &subkey : key) { | ||
if (py::isinstance<py::str>(subkey)) { | ||
newkey.append(subkey); | ||
} else { | ||
auto _key = _unravel_key_to_tuple(subkey.cast<py::object>()); | ||
if (_key.size() == 0) { | ||
return py::make_tuple(); | ||
} | ||
newkey += _key; | ||
} | ||
} | ||
return py::tuple(newkey); | ||
} | ||
if (is_str) { | ||
return py::make_tuple(key); | ||
} else { | ||
return py::make_tuple(); | ||
} | ||
} | ||
|
||
py::object unravel_key(const py::object &key) { | ||
bool is_tuple = py::isinstance<py::tuple>(key); | ||
bool is_str = py::isinstance<py::str>(key); | ||
|
||
if (is_tuple) { | ||
py::list newkey; | ||
int count = 0; | ||
for (const auto &subkey : key) { | ||
if (py::isinstance<py::str>(subkey)) { | ||
newkey.append(subkey); | ||
count++; | ||
} else { | ||
auto _key = _unravel_key_to_tuple(subkey.cast<py::object>()); | ||
count += _key.size(); | ||
newkey += _key; | ||
} | ||
} | ||
if (count == 1) { | ||
return newkey[0]; | ||
} | ||
return py::tuple(newkey); | ||
} | ||
if (is_str) { | ||
return key; | ||
} else { | ||
throw std::runtime_error("key should be a Sequence<NestedKey>"); | ||
} | ||
} | ||
|
||
py::list unravel_key_list(const py::list &keys) { | ||
py::list newkeys; | ||
for (const auto &key : keys) { | ||
auto _key = unravel_key(key.cast<py::object>()); | ||
newkeys.append(_key); | ||
} | ||
return newkeys; | ||
} | ||
|
||
py::list unravel_key_list(const py::tuple &keys) { | ||
return unravel_key_list(py::list(keys)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters