Skip to content

Commit

Permalink
Added __str__
Browse files Browse the repository at this point in the history
  • Loading branch information
Armd04 committed Sep 8, 2024
1 parent 497e4ef commit 0bae200
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
109 changes: 108 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use pyo3::prelude::*;
use pyo3::types::{PyList, PySet, PyAny};
use pyo3::types::PyAny;

#[pyclass(module = "pqtree", get_all)]
struct P{
Expand Down Expand Up @@ -37,6 +37,60 @@ impl P{
self.children.reverse();
}

fn __repr__(&self, py: Python) -> PyResult<String> {
let mut result = String::from("P{");
let mut first = true;

for child in &self.children {
if !first {
result.push_str(", ");
}
first = false;

// Recursively format if the child is an instance of P or Q

if let Ok(py_obj) = child.extract::<PyObject>(py) {
if let Ok(p_obj) = py_obj.downcast_bound::<P>(py) {
result.push_str(&p_obj.borrow().__repr__(py)?);
} else if let Ok(q_obj) = py_obj.downcast_bound::<Q>(py) {
result.push_str(&q_obj.borrow().__repr__(py)?);
} else if let Ok(obj) = py_obj.downcast_bound::<PyAny>(py){
result.push_str(obj.str()?.to_str()?);
}
}
}

result.push('}');
Ok(result)
}


fn __str__(&self, py: Python) -> PyResult<String> {
let mut result = String::from("P{");
let mut first = true;

for child in &self.children {
if !first {
result.push_str(", ");
}
first = false;

// Recursively format if the child is an instance of P or Q

if let Ok(py_obj) = child.extract::<PyObject>(py) {
if let Ok(p_obj) = py_obj.downcast_bound::<P>(py) {
result.push_str(&p_obj.borrow().__str__(py)?);
} else if let Ok(q_obj) = py_obj.downcast_bound::<Q>(py) {
result.push_str(&q_obj.borrow().__str__(py)?);
} else if let Ok(obj) = py_obj.downcast_bound::<PyAny>(py){
result.push_str(obj.str()?.to_str()?);
}
}
}

result.push('}');
Ok(result)
}


}
Expand All @@ -63,6 +117,59 @@ impl Q{
self.children.reverse();
}

fn __repr__(&self, py: Python) -> PyResult<String> {
let mut result = String::from("Q{");
let mut first = true;

for child in &self.children {
if !first {
result.push_str(", ");
}
first = false;

// Recursively format if the child is an instance of P or Q

if let Ok(py_obj) = child.extract::<PyObject>(py) {
if let Ok(p_obj) = py_obj.downcast_bound::<P>(py) {
result.push_str(&p_obj.borrow().__repr__(py)?);
} else if let Ok(q_obj) = py_obj.downcast_bound::<Q>(py) {
result.push_str(&q_obj.borrow().__repr__(py)?);
} else if let Ok(obj) = py_obj.downcast_bound::<PyAny>(py){
result.push_str(obj.str()?.to_str()?);
}
}
}

result.push('}');
Ok(result)
}

fn __str__(&self, py: Python) -> PyResult<String> {
let mut result = String::from("Q{");
let mut first = true;

for child in &self.children {
if !first {
result.push_str(", ");
}
first = false;

// Recursively format if the child is an instance of P or Q

if let Ok(py_obj) = child.extract::<PyObject>(py) {
if let Ok(p_obj) = py_obj.downcast_bound::<P>(py) {
result.push_str(&p_obj.borrow().__str__(py)?);
} else if let Ok(q_obj) = py_obj.downcast_bound::<Q>(py) {
result.push_str(&q_obj.borrow().__str__(py)?);
} else if let Ok(obj) = py_obj.downcast_bound::<PyAny>(py){
result.push_str(obj.str()?.to_str()?);
}
}
}

result.push('}');
Ok(result)
}
}


Expand Down
5 changes: 5 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
def test():
p = P([1, Q([4, 5]), 3])

print("Before reverse:\n",p)

p.reverse()

print("After:\n",p)


if __name__ == '__main__':
Expand Down

0 comments on commit 0bae200

Please sign in to comment.