Skip to content

Commit

Permalink
add repr for node state
Browse files Browse the repository at this point in the history
  • Loading branch information
ljeub-pometry committed Jun 5, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 06fb545 commit baa57f1
Showing 2 changed files with 58 additions and 2 deletions.
10 changes: 10 additions & 0 deletions raphtory/src/python/types/macros/trait_impl/node_state.rs
Original file line number Diff line number Diff line change
@@ -19,6 +19,12 @@ use std::sync::Arc;

macro_rules! impl_node_state_ops {
($name:ident<$value:ty>, $inner_t:ty, $to_owned:expr) => {
impl $name {
pub fn iter(&self) -> impl Iterator<Item = $value> + '_ {
self.inner.values().map($to_owned)
}
}

#[pymethods]
impl $name {
fn __len__(&self) -> usize {
@@ -69,6 +75,10 @@ macro_rules! impl_node_state_ops {
fn values(&self) -> PyBorrowingIterator {
self.__iter__()
}

fn __repr__(&self) -> String {
self.inner.repr()
}
}
};
}
50 changes: 48 additions & 2 deletions raphtory/src/python/types/repr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::core::{storage::locked_view::LockedView, ArcStr};
use crate::{
core::{storage::locked_view::LockedView, ArcStr},
db::api::state::{LazyNodeState, NodeState},
prelude::{GraphViewOps, NodeStateOps, NodeViewOps},
};
use chrono::{DateTime, NaiveDateTime, TimeZone};
use itertools::Itertools;
use std::{collections::HashMap, ops::Deref};
@@ -37,18 +41,31 @@ impl StructReprBuilder {
}
}

pub fn add_field<V: Repr>(mut self, name: &str, value: V) -> Self {
fn add_field_sep(&mut self) {
if self.has_fields {
self.value.push_str(", ");
} else {
self.has_fields = true;
}
}

pub fn add_field<V: Repr>(mut self, name: &str, value: V) -> Self {
self.add_field_sep();
self.value.push_str(name);
self.value.push('=');
self.value.push_str(&value.repr());
self
}

pub fn add_fields_from_iter<I: Iterator<Item = (K, V)>, K: Repr, V: Repr>(
mut self,
iter: I,
) -> Self {
self.add_field_sep();
self.value.push_str(&iterator_dict_repr(iter));
self
}

pub fn finish(self) -> String {
let mut value = self.value;
value.push(')');
@@ -193,6 +210,35 @@ impl<'a, R: Repr> Repr for &'a R {
R::repr(self)
}
}

impl<
'graph,
G: GraphViewOps<'graph>,
GH: GraphViewOps<'graph>,
V: Repr + Clone + Send + Sync + 'graph,
> Repr for LazyNodeState<'graph, V, G, GH>
{
fn repr(&self) -> String {
StructReprBuilder::new("LazyNodeState")
.add_fields_from_iter(self.iter().map(|(n, v)| (n.name(), v)))
.finish()
}
}

impl<
'graph,
G: GraphViewOps<'graph>,
GH: GraphViewOps<'graph>,
V: Repr + Clone + Send + Sync + 'graph,
> Repr for NodeState<'graph, V, G, GH>
{
fn repr(&self) -> String {
StructReprBuilder::new("NodeState")
.add_fields_from_iter(self.iter().map(|(n, v)| (n.name(), v)))
.finish()
}
}

#[cfg(test)]
mod repr_tests {
use super::*;

0 comments on commit baa57f1

Please sign in to comment.