Skip to content

Commit

Permalink
Add python thermograph module
Browse files Browse the repository at this point in the history
  • Loading branch information
t4ccer committed Nov 4, 2023
1 parent 3ebe265 commit 9d3781f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 67 deletions.
21 changes: 4 additions & 17 deletions cgt-py/src/canonical_form.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
use crate::rational::PyRational;
use crate::{rational::PyRational, thermograph::PyThermograph};
use cgt::short::partizan::canonical_form::CanonicalForm;
use pyo3::{prelude::*, pyclass::CompareOp};
use std::{
ops::{Add, Neg, Sub},
str::FromStr,
};

#[pyclass(name = "CanonicalForm")]
#[derive(Clone)]
pub struct PyCanonicalForm {
inner: CanonicalForm,
}

impl From<CanonicalForm> for PyCanonicalForm {
fn from(canonical_form: CanonicalForm) -> Self {
Self {
inner: canonical_form,
}
}
}
crate::wrap_struct!(CanonicalForm, PyCanonicalForm, "CanonicalForm", Clone);

#[pymethods]
impl PyCanonicalForm {
Expand Down Expand Up @@ -70,8 +58,7 @@ impl PyCanonicalForm {
self.inner.temperature().into()
}

fn thermograph(&self) -> String {
// format!("{:?}", self.inner.thermograph())
self.inner.thermograph().to_svg()
fn thermograph(&self) -> PyThermograph {
PyThermograph::from(self.inner.thermograph())
}
}
30 changes: 6 additions & 24 deletions cgt-py/src/domineering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,12 @@ use cgt::short::partizan::{
};
use pyo3::prelude::*;

#[pyclass(name = "Domineering")]
#[derive(Clone)]
pub struct PyDomineering {
inner: Domineering,
}

impl From<Domineering> for PyDomineering {
fn from(domineering: Domineering) -> Self {
Self { inner: domineering }
}
}

#[pyclass(name = "DomineeringTranspositionTable")]
pub struct PyDomineeringTranspositionTable {
inner: TranspositionTable<Domineering>,
}

impl From<TranspositionTable<Domineering>> for PyDomineeringTranspositionTable {
fn from(transposition_table: TranspositionTable<Domineering>) -> Self {
Self {
inner: transposition_table,
}
}
}
crate::wrap_struct!(Domineering, PyDomineering, "Domineering", Clone);
crate::wrap_struct!(
TranspositionTable<Domineering>,
PyDomineeringTranspositionTable,
"DomineeringTranspositionTable"
);

#[pymethods]
impl PyDomineering {
Expand Down
26 changes: 22 additions & 4 deletions cgt-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,31 @@ mod canonical_form;
mod domineering;
mod nimber;
mod rational;
mod thermograph;

use crate::canonical_form::*;
use crate::domineering::*;
use crate::nimber::*;
use crate::rational::*;
use crate::{canonical_form::*, domineering::*, nimber::*, rational::*, thermograph::*};

// TODO: Pretty printers
// TODO: SVG rendering & html()

#[macro_export]
macro_rules! wrap_struct {
($struct:path, $py_struct:ident, $py_class:expr $(, $trait:tt)*) => {
#[derive($($trait),*)]
#[pyclass(name = $py_class)]
#[repr(transparent)]
pub struct $py_struct {
inner: $struct,
}

impl From<$struct> for $py_struct {
fn from(inner: $struct) -> Self {
$py_struct { inner }
}
}
};
}

#[pymodule]
fn cgt_py(_py: Python, m: &PyModule) -> PyResult<()> {
macro_rules! add_class {
Expand All @@ -37,5 +53,7 @@ fn cgt_py(_py: Python, m: &PyModule) -> PyResult<()> {

add_class!(PyRational);

add_class!(PyThermograph);

Ok(())
}
12 changes: 1 addition & 11 deletions cgt-py/src/nimber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@ use cgt::numeric::nimber::Nimber;
use pyo3::{prelude::*, pyclass::CompareOp};
use std::ops::{Add, Neg, Sub};

#[pyclass(name = "Nimber")]
#[derive(Clone)]
pub struct PyNimber {
inner: Nimber,
}

impl From<Nimber> for PyNimber {
fn from(nimber: Nimber) -> Self {
Self { inner: nimber }
}
}
crate::wrap_struct!(Nimber, PyNimber, "Nimber", Clone);

#[pymethods]
impl PyNimber {
Expand Down
12 changes: 1 addition & 11 deletions cgt-py/src/rational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@ use std::{
str::FromStr,
};

#[pyclass(name = "Rational")]
#[derive(Clone)]
pub struct PyRational {
inner: Rational,
}

impl From<Rational> for PyRational {
fn from(rational: Rational) -> Self {
Self { inner: rational }
}
}
crate::wrap_struct!(Rational, PyRational, "Rational", Clone);

#[pymethods]
impl PyRational {
Expand Down
15 changes: 15 additions & 0 deletions cgt-py/src/thermograph.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use cgt::short::partizan::thermograph::Thermograph;
use pyo3::prelude::*;

crate::wrap_struct!(Thermograph, PyThermograph, "Thermograph", Clone);

#[pymethods]
impl PyThermograph {
fn __repr__(&self) -> String {
format!("Thermograph({})", self.inner)
}

fn to_svg(&self) -> String {
self.inner.to_svg()
}
}

0 comments on commit 9d3781f

Please sign in to comment.