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

Commit b5f5643

Browse files
authored
Read a list of segment records directly from a file (#22)
* Read a list of segment records directly from a file * add text_signature
1 parent c179716 commit b5f5643

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/parameter/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ where
287287

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

292292
// Read binary records
293293
let binary_records = file_binary

src/parameter/segment.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
use super::ParameterError;
2+
use serde::de::DeserializeOwned;
13
use serde::{Deserialize, Serialize};
4+
use std::fs::File;
25
use std::hash::{Hash, Hasher};
6+
use std::io::BufReader;
7+
use std::path::Path;
38

49
/// Parameters describing an individual segment of a molecule.
510
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -25,6 +30,15 @@ impl<M, I> SegmentRecord<M, I> {
2530
ideal_gas_record,
2631
}
2732
}
33+
34+
/// Read a list of `SegmentRecord`s from a JSON file.
35+
pub fn from_json<P: AsRef<Path>>(file: P) -> Result<Vec<Self>, ParameterError>
36+
where
37+
I: DeserializeOwned,
38+
M: DeserializeOwned,
39+
{
40+
Ok(serde_json::from_reader(BufReader::new(File::open(file)?))?)
41+
}
2842
}
2943

3044
impl<M, I> Hash for SegmentRecord<M, I> {

src/python/parameter.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,25 @@ macro_rules! impl_segment_record {
482482
)))
483483
}
484484

485+
/// Read a list of `SegmentRecord`s from a JSON file.
486+
///
487+
/// Parameters
488+
/// ----------
489+
/// path : str
490+
/// Path to file containing the segment records.
491+
///
492+
/// Returns
493+
/// -------
494+
/// SegmentRecord
495+
#[staticmethod]
496+
#[pyo3(text_signature = "(path)")]
497+
fn from_json(path: &str) -> Result<Vec<Self>, ParameterError> {
498+
Ok(SegmentRecord::from_json(path)?
499+
.into_iter()
500+
.map(|s| Self(s))
501+
.collect())
502+
}
503+
485504
#[getter]
486505
fn get_identifier(&self) -> String {
487506
self.0.identifier.clone()

0 commit comments

Comments
 (0)