Params: add new method to get all keys (#25779)

* allKeys

* cleanup and test

Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
pull/25799/head
Dean Lee 3 years ago committed by GitHub
parent d5410dfac5
commit 85b433760a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      common/params.cc
  2. 2
      common/params.h
  3. 5
      common/params_pyx.pyx
  4. 8
      common/tests/test_params.py

@ -3,6 +3,7 @@
#include <dirent.h> #include <dirent.h>
#include <sys/file.h> #include <sys/file.h>
#include <algorithm>
#include <csignal> #include <csignal>
#include <unordered_map> #include <unordered_map>
@ -203,6 +204,14 @@ Params::Params(const std::string &path) {
params_path = path.empty() ? default_param_path : ensure_params_path(prefix, path); params_path = path.empty() ? default_param_path : ensure_params_path(prefix, path);
} }
std::vector<std::string> Params::allKeys() const {
std::vector<std::string> ret;
for (auto &p : keys) {
ret.push_back(p.first);
}
return ret;
}
bool Params::checkKey(const std::string &key) { bool Params::checkKey(const std::string &key) {
return keys.find(key) != keys.end(); return keys.find(key) != keys.end();
} }

@ -2,6 +2,7 @@
#include <map> #include <map>
#include <string> #include <string>
#include <vector>
enum ParamKeyType { enum ParamKeyType {
PERSISTENT = 0x02, PERSISTENT = 0x02,
@ -15,6 +16,7 @@ enum ParamKeyType {
class Params { class Params {
public: public:
Params(const std::string &path = {}); Params(const std::string &path = {});
std::vector<std::string> allKeys() const;
bool checkKey(const std::string &key); bool checkKey(const std::string &key);
ParamKeyType getKeyType(const std::string &key); ParamKeyType getKeyType(const std::string &key);
inline std::string getParamPath(const std::string &key = {}) { inline std::string getParamPath(const std::string &key = {}) {

@ -2,6 +2,7 @@
# cython: language_level = 3 # cython: language_level = 3
from libcpp cimport bool from libcpp cimport bool
from libcpp.string cimport string from libcpp.string cimport string
from libcpp.vector cimport vector
import threading import threading
cdef extern from "common/params.h": cdef extern from "common/params.h":
@ -22,6 +23,7 @@ cdef extern from "common/params.h":
bool checkKey(string) nogil bool checkKey(string) nogil
string getParamPath(string) nogil string getParamPath(string) nogil
void clearAll(ParamKeyType) void clearAll(ParamKeyType)
vector[string] allKeys()
def ensure_bytes(v): def ensure_bytes(v):
@ -99,6 +101,9 @@ cdef class Params:
cdef string key_bytes = ensure_bytes(key) cdef string key_bytes = ensure_bytes(key)
return self.p.getParamPath(key_bytes).decode("utf-8") return self.p.getParamPath(key_bytes).decode("utf-8")
def all_keys(self):
return self.p.allKeys()
def put_nonblocking(key, val, d=""): def put_nonblocking(key, val, d=""):
threading.Thread(target=lambda: Params(d).put(key, val)).start() threading.Thread(target=lambda: Params(d).put(key, val)).start()

@ -98,6 +98,14 @@ class TestParams(unittest.TestCase):
assert q.get("CarParams") is None assert q.get("CarParams") is None
assert q.get("CarParams", True) == b"1" assert q.get("CarParams", True) == b"1"
def test_params_all_keys(self):
keys = Params().all_keys()
# sanity checks
assert len(keys) > 20
assert len(keys) == len(set(keys))
assert b"CarParams" in keys
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

Loading…
Cancel
Save