From 1b8d10ed0c7e8f51b6603801c4768d50464dba43 Mon Sep 17 00:00:00 2001 From: Lin Zhihao <59785146+LinZhihao-723@users.noreply.github.com> Date: Fri, 22 Sep 2023 17:44:32 -0700 Subject: [PATCH] Implement __str__ and __repr__ methods for PyQuery (#26) --- clp_ffi_py/ir/native.pyi | 2 ++ clp_ffi_py/ir/query_builder.py | 5 ++--- src/clp_ffi_py/ir/native/PyQuery.cpp | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/clp_ffi_py/ir/native.pyi b/clp_ffi_py/ir/native.pyi index 25806f61..f953a5d0 100644 --- a/clp_ffi_py/ir/native.pyi +++ b/clp_ffi_py/ir/native.pyi @@ -48,6 +48,8 @@ class Query: wildcard_queries: Optional[List[WildcardQuery]] = None, search_time_termination_margin: int = default_search_time_termination_margin(), ): ... + def __str__(self) -> str: ... + def __repr__(self) -> str: ... def __getstate__(self) -> Dict[str, Any]: ... def __setstate__(self, state: Dict[str, Any]) -> None: ... def get_search_time_lower_bound(self) -> int: ... diff --git a/clp_ffi_py/ir/query_builder.py b/clp_ffi_py/ir/query_builder.py index c390f4b0..ecce0416 100644 --- a/clp_ffi_py/ir/query_builder.py +++ b/clp_ffi_py/ir/query_builder.py @@ -80,9 +80,8 @@ def set_search_time_termination_margin(self, ts: int) -> QueryBuilder: def add_wildcard_query(self, wildcard_query: str, case_sensitive: bool = False) -> QueryBuilder: """ - Constructs and adds a - :class:`~clp_ffi_py.wildcard_query.WildcardQuery` to the wildcard - query list. + Constructs and adds a :class:`~clp_ffi_py.wildcard_query.WildcardQuery` + to the wildcard query list. :param wildcard_query: The wildcard query string to add. :param case_sensitive: Whether to perform case-sensitive matching. diff --git a/src/clp_ffi_py/ir/native/PyQuery.cpp b/src/clp_ffi_py/ir/native/PyQuery.cpp index e725aae5..0d1cb304 100644 --- a/src/clp_ffi_py/ir/native/PyQuery.cpp +++ b/src/clp_ffi_py/ir/native/PyQuery.cpp @@ -469,6 +469,24 @@ PyDoc_STRVAR( auto PyQuery_default_search_time_termination_margin(PyObject* Py_UNUSED(self)) -> PyObject* { return PyLong_FromLongLong(Query::cDefaultSearchTimeTerminationMargin); } + +/** + * Callback of PyQuery `__str__` method. + * @param self + * @return Python string representation of the serialzed PyQuery object. + */ +auto PyQuery_str(PyQuery* self) -> PyObject* { + return PyObject_Str(PyQuery_getstate(self)); +} + +/** + * Callback of PyQuery `__repr__` method. + * @param self + * @return __repr__ of the serialzied PyQuery object. + */ +auto PyQuery_repr(PyQuery* self) -> PyObject* { + return PyObject_Repr(PyQuery_getstate(self)); +} } // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) @@ -568,6 +586,8 @@ PyType_Slot PyQuery_slots[]{ {Py_tp_dealloc, reinterpret_cast(PyQuery_dealloc)}, {Py_tp_new, reinterpret_cast(PyType_GenericNew)}, {Py_tp_init, reinterpret_cast(PyQuery_init)}, + {Py_tp_str, reinterpret_cast(PyQuery_str)}, + {Py_tp_repr, reinterpret_cast(PyQuery_repr)}, {Py_tp_methods, static_cast(PyQuery_method_table)}, {Py_tp_doc, const_cast(static_cast(cPyQueryDoc))}, {0, nullptr}