Skip to content

Commit

Permalink
python: add __repr__ methods
Browse files Browse the repository at this point in the history
This makes handling the objects more visual than seeing the binary addresses

Signed-off-by: Baptiste Prevot <[email protected]>
  • Loading branch information
Castavo committed Oct 18, 2024
1 parent 7402ba2 commit 04d4cc9
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ node_modules
dist
data
.env
.venv
.vscode
pkg
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "liblrs"
version = "0.2.1"
version = "0.2.2"
edition = "2021"
description = "Library to manipulate linear referencing systems"
license = "MIT"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ To run locally:

```bash
cd wasm
npm install
npm install -g wasm-pack
npm run serve
```

Expand Down
2 changes: 1 addition & 1 deletion python/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "liblrs_python"
description = "Python bindings for liblrs: a library to work with linear referencing systems"
version = "0.2.1"
version = "0.2.2"
edition = "2021"
license = "MIT"
repository = "https://github.com/osrd-project/liblrs/"
Expand Down
26 changes: 23 additions & 3 deletions python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn liblrs_python(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
Ok(())
}

#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
/// A geographical [`Point`], it can be either a projected or spherical coordinates.
#[pyclass]
pub struct Point {
Expand All @@ -47,6 +47,10 @@ impl Point {
fn new(x: f64, y: f64) -> Self {
Self { x, y }
}

fn __repr__(&self) -> String {
format!("{:?}", self)
}
}

impl From<geo_types::Point> for Point {
Expand Down Expand Up @@ -83,7 +87,7 @@ impl From<Point> for geo_types::Coord {
}

#[pyclass]
#[derive(Clone)]
#[derive(Clone, Debug)]
/// Represent a position on an [`LrmScale`] relative as an `offset` to an [`Anchor`].
pub struct LrmScaleMeasure {
#[pyo3(get, set)]
Expand Down Expand Up @@ -122,6 +126,10 @@ impl LrmScaleMeasure {
scale_offset,
}
}

fn __repr__(&self) -> String {
format!("{:?}", self)
}
}

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -156,7 +164,7 @@ impl From<SegmentOfTraversal> for liblrs::builder::SegmentOfTraversal {
}
}

#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
#[pyclass]
/// The linear position of an anchor doesn’t always match the measured distance
/// For example if a road was transformed into a bypass, resulting in a longer road,
Expand All @@ -181,6 +189,10 @@ impl AnchorOnLrm {
distance_along_lrm,
}
}

fn __repr__(&self) -> String {
format!("{:?}", self)
}
}

impl From<AnchorOnLrm> for liblrs::builder::AnchorOnLrm {
Expand All @@ -192,6 +204,7 @@ impl From<AnchorOnLrm> for liblrs::builder::AnchorOnLrm {
}
}

#[derive(Debug)]
#[pyclass]
/// An `Anchor` is a reference point for a given [`Curve`].
/// It can be a milestone, a bridge…
Expand All @@ -210,6 +223,13 @@ pub struct Anchor {
pub scale_position: f64,
}

#[pymethods]
impl Anchor {
fn __repr__(&self) -> String {
format!("{:?}", self)
}
}

#[pyclass]
/// The result of a projection onto an [`LrmScale`].
pub struct LrmProjection {
Expand Down
2 changes: 1 addition & 1 deletion wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"pmtiles": "3.2.0"
},
"name": "liblrs",
"version": "0.2.1",
"version": "0.2.2",
"repository": {
"type": "git",
"url": "https://github.com/osrd-project/liblrs/"
Expand Down

0 comments on commit 04d4cc9

Please sign in to comment.