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

stabilized-value in pyrsutils; replace 9 cpp tests with 1 py #11214

Merged
merged 4 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions third-party/rsutils/py/pyrsutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <rsutils/easylogging/easyloggingpp.h>
#include <rsutils/string/split.h>
#include <rsutils/version.h>
#include <rsutils/number/stabilized-value.h>
#include <rsutils/string/from.h>

#define NAME pyrsutils
#define SNAME "pyrsutils"
Expand Down Expand Up @@ -37,8 +39,7 @@ PYBIND11_MODULE(NAME, m) {
py::arg( "build" ) = 0 )
.def_static( "from_number", []( version::number_type n ) { return version( n ); } )
.def( "is_valid", &version::is_valid )
.def( "__nonzero__", &version::is_valid ) // Called to implement truth value testing in Python 2
.def( "__bool__", &version::is_valid ) // Called to implement truth value testing in Python 3
.def( "__bool__", &version::is_valid )
.def( "major", &version::get_major )
.def( "minor", &version::get_minor )
.def( "patch", &version::get_patch )
Expand All @@ -63,4 +64,23 @@ PYBIND11_MODULE(NAME, m) {
.def( py::self >= py::self )
.def( py::self > py::self )
.def( "is_between", &version::is_between );

using stabilized_value = rsutils::number::stabilized_value< double >;
auto not_empty = []( stabilized_value const & self ) {
return ! self.empty();
};
auto to_string = []( stabilized_value const & self ) -> std::string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those 2 functions are global will generic names but have a specific implementation.
What will happen the next time you want to add them to a different utility?
Can you use some namespace or rename to a more specific var names?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Global? No, they're just in this code.
If needed, we'll rename or move inline.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant that it's not binds to the stabilized value utility, it is known in all of pyrsutils

if( self.empty() )
return "EMPTY";
return rsutils::string::from( self.get() );
};
py::class_< stabilized_value >( m, "stabilized_value" )
.def( py::init< size_t >() )
.def( "empty", &stabilized_value::empty )
.def( "__bool__", not_empty )
.def( "add", &stabilized_value::add )
.def( "get", &stabilized_value::get, py::arg( "stabilization-percent" ) = 0.75 )
.def( "clear", &stabilized_value::clear )
.def( "to_string", to_string )
.def( "__str__", to_string );
}
2 changes: 2 additions & 0 deletions unit-tests/py/rspy/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ def check_throws( _lambda, expected_type, expected_msg = None, abort_if_failed =
"""
We expect the lambda, when called, to raise an exception!
"""
if not callable( _lambda ):
raise RuntimeError( "expecting a function, not " + _lambda )
try:
_lambda()
except Exception as e:
Expand Down
30 changes: 0 additions & 30 deletions unit-tests/rsutils/number/stabilized/test-60-percent.cpp

This file was deleted.

32 changes: 0 additions & 32 deletions unit-tests/rsutils/number/stabilized/test-change-percent.cpp

This file was deleted.

22 changes: 0 additions & 22 deletions unit-tests/rsutils/number/stabilized/test-empty.cpp

This file was deleted.

25 changes: 0 additions & 25 deletions unit-tests/rsutils/number/stabilized/test-flickering-value.cpp

This file was deleted.

21 changes: 0 additions & 21 deletions unit-tests/rsutils/number/stabilized/test-illegal-input.cpp

This file was deleted.

34 changes: 0 additions & 34 deletions unit-tests/rsutils/number/stabilized/test-not-full.cpp

This file was deleted.

28 changes: 0 additions & 28 deletions unit-tests/rsutils/number/stabilized/test-same-value.cpp

This file was deleted.

Loading