Skip to content

Commit

Permalink
Implement __str__ and __repr__ methods for PyQuery (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZhihao-723 authored Sep 23, 2023
1 parent 95bb6a2 commit 1b8d10e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions clp_ffi_py/ir/native.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down
5 changes: 2 additions & 3 deletions clp_ffi_py/ir/query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions src/clp_ffi_py/ir/native/PyQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -568,6 +586,8 @@ PyType_Slot PyQuery_slots[]{
{Py_tp_dealloc, reinterpret_cast<void*>(PyQuery_dealloc)},
{Py_tp_new, reinterpret_cast<void*>(PyType_GenericNew)},
{Py_tp_init, reinterpret_cast<void*>(PyQuery_init)},
{Py_tp_str, reinterpret_cast<void*>(PyQuery_str)},
{Py_tp_repr, reinterpret_cast<void*>(PyQuery_repr)},
{Py_tp_methods, static_cast<void*>(PyQuery_method_table)},
{Py_tp_doc, const_cast<void*>(static_cast<void const*>(cPyQueryDoc))},
{0, nullptr}
Expand Down

0 comments on commit 1b8d10e

Please sign in to comment.