Skip to content
This repository has been archived by the owner on Jun 14, 2022. It is now read-only.

Commit

Permalink
Read a list of segment records directly from a file (#22)
Browse files Browse the repository at this point in the history
* Read a list of segment records directly from a file

* add text_signature
  • Loading branch information
prehner authored Jan 21, 2022
1 parent c179716 commit b5f5643
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/parameter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ where

// Read segment records
let segment_records: Vec<SegmentRecord<Self::Pure, Self::IdealGas>> =
serde_json::from_reader(BufReader::new(File::open(file_segments)?))?;
SegmentRecord::from_json(file_segments)?;

// Read binary records
let binary_records = file_binary
Expand Down
14 changes: 14 additions & 0 deletions src/parameter/segment.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use super::ParameterError;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::hash::{Hash, Hasher};
use std::io::BufReader;
use std::path::Path;

/// Parameters describing an individual segment of a molecule.
#[derive(Serialize, Deserialize, Debug, Clone)]
Expand All @@ -25,6 +30,15 @@ impl<M, I> SegmentRecord<M, I> {
ideal_gas_record,
}
}

/// Read a list of `SegmentRecord`s from a JSON file.
pub fn from_json<P: AsRef<Path>>(file: P) -> Result<Vec<Self>, ParameterError>
where
I: DeserializeOwned,
M: DeserializeOwned,
{
Ok(serde_json::from_reader(BufReader::new(File::open(file)?))?)
}
}

impl<M, I> Hash for SegmentRecord<M, I> {
Expand Down
19 changes: 19 additions & 0 deletions src/python/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,25 @@ macro_rules! impl_segment_record {
)))
}

/// Read a list of `SegmentRecord`s from a JSON file.
///
/// Parameters
/// ----------
/// path : str
/// Path to file containing the segment records.
///
/// Returns
/// -------
/// SegmentRecord
#[staticmethod]
#[pyo3(text_signature = "(path)")]
fn from_json(path: &str) -> Result<Vec<Self>, ParameterError> {
Ok(SegmentRecord::from_json(path)?
.into_iter()
.map(|s| Self(s))
.collect())
}

#[getter]
fn get_identifier(&self) -> String {
self.0.identifier.clone()
Expand Down

0 comments on commit b5f5643

Please sign in to comment.