Skip to content

Commit

Permalink
improved printing small numbers for register_value
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-urban committed Oct 1, 2024
1 parent 5fe0abb commit 406d94f
Show file tree
Hide file tree
Showing 21 changed files with 143 additions and 90 deletions.
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ project(
'cpp',
license: 'MPL-2.0',

version: '0.26.1',
version: '0.27.0',
default_options: ['warning_level=2', 'buildtype=release', 'cpp_std=c++20'],
meson_version: '>=1.3.2' #first version with clang-cl openmp support,
)
Expand Down
5 changes: 3 additions & 2 deletions src/pymodule/classhelper/c_objectprinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ void init_c_objectprinter(pybind11::module& m)

py::class_<ObjectPrinter>(
m, "ObjectPrinter", DOC(themachinethatgoesping, tools, classhelper, ObjectPrinter))
.def(py::init<const std::string&, unsigned int>(),
.def(py::init<const std::string&, unsigned int, bool>(),
DOC(themachinethatgoesping, tools, classhelper, ObjectPrinter, ObjectPrinter),
py::arg("name"),
py::arg("float_precission"))
py::arg("float_precission"),
py::arg("superscript_exponents"))
.def("class_name",
&ObjectPrinter::class_name,
DOC(themachinethatgoesping, tools, classhelper, ObjectPrinter, class_name))
Expand Down
5 changes: 2 additions & 3 deletions src/pymodule/m_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
//
// SPDX-License-Identifier: MPL-2.0




#include "m_helper.hpp"
// automatically gernerated using python -m pybind11_mkdoc -o docstrings.h <headerfiles>

Expand Down Expand Up @@ -42,4 +39,6 @@ void init_m_helper(py::module& m)
&string_as_int<int64_t>,
"Interprete a 8 byte string to an integer",
py::arg("value"));
m_helper.def(
"superscript", &superscript, "convert integer number to superscript", py::arg("exponent"));
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//sourcehash: 9e52974b63da8f3ce7627008f66e807c37ee88c968b30ff423515d475050c669
//sourcehash: d04c6e0a7a517edf52d0daaba3cabb32505694b61d90588da1589496c6290d99

/*
This file contains docstrings for use in the Python bindings.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//sourcehash: 1035d74930b449d87a2f6efd0753966618188fd10c4f929ffbaad9a8fa55d240
//sourcehash: 3dd835ee71b018c07763279786b4352637a796cf98af473f56d2ced2b22431c7

/*
This file contains docstrings for use in the Python bindings.
Expand Down Expand Up @@ -126,6 +126,9 @@ R"doc(\ return an info string using the class __printer__ object \
Parameter ``float_precision``:
number of digits for floating point values \
Parameter ``superscript_exponents``:
print exponents in superscript \
Returns:
std::string \)doc";

Expand All @@ -138,7 +141,10 @@ Parameter ``os``:
output stream, e.g. file stream or std::out or std::cerr \
Parameter ``float_precision``:
number of digits for floating point values \)doc";
number of digits for floating point values \
Parameter ``superscript_exponents``:
print exponents in superscript \)doc";

static const char *__doc_themachinethatgoesping_tools_classhelper_ObjectPrinter_printer = R"doc()doc";

Expand Down Expand Up @@ -250,10 +256,7 @@ static const char *__doc_themachinethatgoesping_tools_classhelper_ObjectPrinter_

static const char *__doc_themachinethatgoesping_tools_classhelper_ObjectPrinter_section_underliner = R"doc(< additional info (printed in []))doc";

static const char *__doc_themachinethatgoesping_tools_classhelper_ObjectPrinter_slow_hash =
R"doc(compute a 64 bit hash of the object using xxhash and the \ to_binary
function. This function is called binary because the to_binary
function creates \ a copy)doc";
static const char *__doc_themachinethatgoesping_tools_classhelper_ObjectPrinter_superscript_exponents = R"doc()doc";

static const char *__doc_themachinethatgoesping_tools_classhelper_ObjectPrinter_t_field =
R"doc(internal, describe the value type for implementing different printing
Expand Down
56 changes: 45 additions & 11 deletions src/themachinethatgoesping/tools/classhelper/objectprinter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <magic_enum.hpp>
#include <numeric>

#include "../helper/printing.hpp"
#include "stream.hpp"

// // source https://gitlab.com/tesch1/cppduals/blob/master/duals/dual#L1379-1452
Expand Down Expand Up @@ -86,8 +87,8 @@ struct fmt::formatter<std::complex<T>, Char> : public fmt::formatter<T, Char>
}

template<typename FormatContext>
FMT_CONSTEXPR auto format(const std::complex<T>& x, FormatContext& ctx) const
-> decltype(ctx.out())
FMT_CONSTEXPR auto format(const std::complex<T>& x,
FormatContext& ctx) const -> decltype(ctx.out())
{
format_to(ctx.out(), "(");
if (style_ == style::pair)
Expand Down Expand Up @@ -122,11 +123,13 @@ struct fmt::formatter<std::complex<T>, Char> : public fmt::formatter<T, Char>
/** \
* @brief return an info string using the class __printer__ object \
* @param float_precision number of digits for floating point values \
* @param superscript_exponents print exponents in superscript \
* @return std::string \
*/ \
std::string info_string(unsigned int float_precision = 2) const \
std::string info_string(unsigned int float_precision = 2, bool superscript_exponents = true) \
const \
{ \
return this->__printer__(float_precision).create_str(); \
return this->__printer__(float_precision, superscript_exponents).create_str(); \
}

#define __CLASSHELPER_PRINTER_PRINT__ \
Expand All @@ -135,10 +138,13 @@ struct fmt::formatter<std::complex<T>, Char> : public fmt::formatter<T, Char>
* \
* @param os output stream, e.g. file stream or std::out or std::cerr \
* @param float_precision number of digits for floating point values \
* @param superscript_exponents print exponents in superscript \
*/ \
void print(std::ostream& os, unsigned int float_precision = 2) const \
void print(std::ostream& os, \
unsigned int float_precision = 2, \
bool superscript_exponents = true) const \
{ \
os << this->__printer__(float_precision).create_str() << std::endl; \
os << this->__printer__(float_precision, superscript_exponents).create_str() << std::endl; \
}

#define __CLASSHELPER_DEFAULT_PRINTING_FUNCTIONS__ \
Expand Down Expand Up @@ -184,7 +190,8 @@ class ObjectPrinter
std::vector<std::string> _value_infos; ///< additional info (printed in [])
std::vector<char> _section_underliner; ///< additional info (printed in [])

unsigned int _float_precision = 2;
unsigned int _float_precision = 2;
bool _superscript_exponents = true;

/**
* @brief Construct a new Object Printer object
Expand Down Expand Up @@ -212,6 +219,9 @@ class ObjectPrinter
is.read(reinterpret_cast<char*>(&printer._float_precision),
sizeof(printer._float_precision));

is.read(reinterpret_cast<char*>(&printer._superscript_exponents),
sizeof(printer._superscript_exponents));

return printer;
}

Expand All @@ -227,11 +237,13 @@ class ObjectPrinter
container_to_stream(os, _section_underliner);

os.write(reinterpret_cast<const char*>(&_float_precision), sizeof(_float_precision));
os.write(reinterpret_cast<const char*>(&_superscript_exponents),
sizeof(_superscript_exponents));
}

ObjectPrinter __printer__(unsigned int float_precision) const
ObjectPrinter __printer__(unsigned int float_precision, bool superscript_exponents) const
{
ObjectPrinter printer("ObjectPrinter", float_precision);
ObjectPrinter printer("ObjectPrinter", float_precision, superscript_exponents);

printer.register_value("name", _name);
printer.register_container("fields", _fields);
Expand All @@ -255,6 +267,7 @@ class ObjectPrinter
printer.register_container("value_infos", _value_infos);
printer.register_container("section_underliner", _section_underliner);
printer.register_value("float_precision", _float_precision);
printer.register_value("superscript_exponents", _superscript_exponents);

return printer;
}
Expand All @@ -268,9 +281,10 @@ class ObjectPrinter
*
* @param name name of the class that is to be printed
*/
ObjectPrinter(std::string_view name, unsigned int float_precision)
ObjectPrinter(std::string_view name, unsigned int float_precision, bool superscript_exponents)
: _name(std::string(name))
, _float_precision(float_precision)
, _superscript_exponents(superscript_exponents)
{
}

Expand Down Expand Up @@ -394,7 +408,27 @@ class ObjectPrinter

// convert value to string
if constexpr (std::is_floating_point<t_value>())
str = fmt::format("{:.{}f}", value, _float_precision);
{
// make sure small values are displayed in a more readable format
int exponent = 0;
while (std::fabs(value) < t_value(0.1))
{
exponent -= 3;
value *= 1000;
}
if (exponent != 0)
{
if (_superscript_exponents)
str = fmt::format("{:.{}f}e{}",
value,
_float_precision,
tools::helper::superscript(exponent));
else
str = fmt::format("{:.{}f}e{}", value, _float_precision, exponent);
}
else
str = fmt::format("{:.{}f}", value, _float_precision);
}
else
str = fmt::format("{}", value);

Expand Down
1 change: 1 addition & 0 deletions src/themachinethatgoesping/tools/helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "helper/defaultmap.hpp"
#include "helper/defaultsharedpointermap.hpp"
#include "helper/printing.hpp"

namespace themachinethatgoesping {
namespace tools {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//sourcehash: 3c0d4feadbff0131d234be02026952f1d91aae273da61f904d59d0c6e19c3337
//sourcehash: 3f65e51e87a20fd462bd6686b56e98f32e2f2ae64d13445747ff2b33889aeb49

/*
This file contains docstrings for use in the Python bindings.
Expand Down Expand Up @@ -132,6 +132,9 @@ R"doc(\ return an info string using the class __printer__ object \
Parameter ``float_precision``:
number of digits for floating point values \
Parameter ``superscript_exponents``:
print exponents in superscript \
Returns:
std::string \)doc";

Expand All @@ -144,14 +147,12 @@ Parameter ``os``:
output stream, e.g. file stream or std::out or std::cerr \
Parameter ``float_precision``:
number of digits for floating point values \)doc";
number of digits for floating point values \
static const char *__doc_themachinethatgoesping_tools_pyhelper_PyIndexer_Slice_printer = R"doc()doc";
Parameter ``superscript_exponents``:
print exponents in superscript \)doc";

static const char *__doc_themachinethatgoesping_tools_pyhelper_PyIndexer_Slice_slow_hash =
R"doc(compute a 64 bit hash of the object using xxhash and the \ to_binary
function. This function is called binary because the to_binary
function creates \ a copy)doc";
static const char *__doc_themachinethatgoesping_tools_pyhelper_PyIndexer_Slice_printer = R"doc()doc";

static const char *__doc_themachinethatgoesping_tools_pyhelper_PyIndexer_Slice_start = R"doc(< the start index of the slice (None if not sliced))doc";

Expand Down Expand Up @@ -226,6 +227,9 @@ R"doc(\ return an info string using the class __printer__ object \
Parameter ``float_precision``:
number of digits for floating point values \
Parameter ``superscript_exponents``:
print exponents in superscript \
Returns:
std::string \)doc";

Expand All @@ -249,7 +253,10 @@ Parameter ``os``:
output stream, e.g. file stream or std::out or std::cerr \
Parameter ``float_precision``:
number of digits for floating point values \)doc";
number of digits for floating point values \
Parameter ``superscript_exponents``:
print exponents in superscript \)doc";

static const char *__doc_themachinethatgoesping_tools_pyhelper_PyIndexer_printer =
R"doc(Print function, needs __CLASSHELPER_DEFAULT_PRINTING_FUNCTIONS__ macro
Expand Down Expand Up @@ -319,11 +326,6 @@ static const char *__doc_themachinethatgoesping_tools_pyhelper_PyIndexer_size =

static const char *__doc_themachinethatgoesping_tools_pyhelper_PyIndexer_slice_size = R"doc(< the size of the slice (_vector_size if not sliced))doc";

static const char *__doc_themachinethatgoesping_tools_pyhelper_PyIndexer_slow_hash =
R"doc(compute a 64 bit hash of the object using xxhash and the \ to_binary
function. This function is called binary because the to_binary
function creates \ a copy)doc";

static const char *__doc_themachinethatgoesping_tools_pyhelper_PyIndexer_to_binary =
R"doc(convert object to vector of bytes \ \
Expand Down
8 changes: 4 additions & 4 deletions src/themachinethatgoesping/tools/pyhelper/pyindexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ class PyIndexer
// operators
bool operator==(const Slice& other) const = default;

classhelper::ObjectPrinter __printer__(unsigned int float_precision) const
classhelper::ObjectPrinter __printer__(unsigned int float_precision, bool superscript_exponents) const
{
std::string start_ = start == PyIndexer::None ? "" : std::to_string(start);
std::string stop_ = stop == PyIndexer::None ? "" : std::to_string(stop);
std::string step_ = step == PyIndexer::None ? "" : std::to_string(step);

classhelper::ObjectPrinter printer(
fmt::format("PyIndexer::Slice({}:{}:{})", start_, stop_, step_), float_precision);
fmt::format("PyIndexer::Slice({}:{}:{})", start_, stop_, step_), float_precision, superscript_exponents);

return printer;
}
Expand Down Expand Up @@ -497,9 +497,9 @@ class PyIndexer
* @param float_precision Precision of floating point numbers
* @return classhelper::ObjectPrinter
*/
classhelper::ObjectPrinter __printer__(unsigned int float_precision) const
classhelper::ObjectPrinter __printer__(unsigned int float_precision, bool superscript_exponents) const
{
classhelper::ObjectPrinter printer("PyIndexer", float_precision);
classhelper::ObjectPrinter printer("PyIndexer", float_precision, superscript_exponents);

printer.register_value("_vector_size", _vector_size);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//sourcehash: 12b75430c31f5f8201205f6df7a536126122af73b78661006d38d967ee650dd7
//sourcehash: ed064a674b2936cc83bd14ff3ef722ec0d7671d201bb532268476ded39e7ef9d

/*
This file contains docstrings for use in the Python bindings.
Expand Down Expand Up @@ -117,6 +117,9 @@ R"doc(\ return an info string using the class __printer__ object \
Parameter ``float_precision``:
number of digits for floating point values \
Parameter ``superscript_exponents``:
print exponents in superscript \
Returns:
std::string \)doc";

Expand Down Expand Up @@ -161,7 +164,10 @@ Parameter ``os``:
output stream, e.g. file stream or std::out or std::cerr \
Parameter ``float_precision``:
number of digits for floating point values \)doc";
number of digits for floating point values \
Parameter ``superscript_exponents``:
print exponents in superscript \)doc";

static const char *__doc_themachinethatgoesping_tools_vectorinterpolators_AkimaInterpolator_printer = R"doc()doc";

Expand All @@ -174,11 +180,6 @@ Parameter ``X:``:
Parameter ``Y:``:
y vector (must be same size))doc";

static const char *__doc_themachinethatgoesping_tools_vectorinterpolators_AkimaInterpolator_slow_hash =
R"doc(compute a 64 bit hash of the object using xxhash and the \ to_binary
function. This function is called binary because the to_binary
function creates \ a copy)doc";

static const char *__doc_themachinethatgoesping_tools_vectorinterpolators_AkimaInterpolator_to_binary =
R"doc(convert object to vector of bytes \ \
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//sourcehash: 390c910b626333c212305666bc1bab58a2c0e92fa06902d8b143c93f5a69d895
//sourcehash: 5ddaae068f5b36105f921597593f0f8bc2e9ce00931d815570cf341fe2594edc

/*
This file contains docstrings for use in the Python bindings.
Expand Down Expand Up @@ -115,6 +115,9 @@ R"doc(\ return an info string using the class __printer__ object \
Parameter ``float_precision``:
number of digits for floating point values \
Parameter ``superscript_exponents``:
print exponents in superscript \
Returns:
std::string \)doc";

Expand Down Expand Up @@ -160,7 +163,10 @@ Parameter ``os``:
output stream, e.g. file stream or std::out or std::cerr \
Parameter ``float_precision``:
number of digits for floating point values \)doc";
number of digits for floating point values \
Parameter ``superscript_exponents``:
print exponents in superscript \)doc";

static const char *__doc_themachinethatgoesping_tools_vectorinterpolators_I_Interpolator_printer =
R"doc(return a printer object
Expand Down
Loading

0 comments on commit 406d94f

Please sign in to comment.