Skip to content

Commit

Permalink
CI: ruff on github actions (#309)
Browse files Browse the repository at this point in the history
Co-authored-by: JHM Darbyshire (MacBookAir) <[email protected]>
  • Loading branch information
attack68 and attack68 authored Aug 8, 2024
1 parent b20bfe1 commit e37319b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 23 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/ubuntu-latest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install .[dev] -v
- name: Lint with Ruff
run: |
ruff check
- name: Ruff Format
run: |
ruff format
- name: Test with pytest
run: |
coverage run -m pytest
coverage run -m --source=rateslib pytest
coverage report -m
3 changes: 2 additions & 1 deletion deprecations_2.0.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

- FixedRateBond docs calc modes in Notes section
- FixedRateBond docs calc modes in Notes section
- PPSpline
5 changes: 2 additions & 3 deletions src/curves/curve.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::curves::nodes::{Nodes, NodesTimestamp};
use crate::curves::interpolation::utils::index_left;
use crate::curves::nodes::{Nodes, NodesTimestamp};
use crate::dual::{ADOrder, DualsOrF64};
use chrono::NaiveDateTime;
use pyo3::{PyErr};
use pyo3::PyErr;

/// Default struct for storing discount factors (DFs).
pub struct Curve<T: CurveInterpolation> {
Expand Down Expand Up @@ -85,7 +85,6 @@ mod tests {
assert_eq!(result, DualsOrF64::F64(0.9950147597711371))
}


fn nodes_timestamp_fixture() -> NodesTimestamp {
let nodes = Nodes::F64(IndexMap::from_iter(vec![
(ndt(2000, 1, 1), 1.0_f64),
Expand Down
21 changes: 15 additions & 6 deletions src/curves/curve_py.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! Wrapper module to export Rust curve data types to Python using pyo3 bindings.
use crate::curves::nodes::{Nodes, NodesTimestamp};
use crate::curves::{Curve, LinearInterpolator, LogLinearInterpolator, LinearZeroRateInterpolator, CurveInterpolation};
use crate::curves::{
Curve, CurveInterpolation, LinearInterpolator, LinearZeroRateInterpolator,
LogLinearInterpolator,
};
use crate::dual::{get_variable_tags, set_order, ADOrder, Dual, Dual2, DualsOrF64};
use chrono::NaiveDateTime;
use indexmap::IndexMap;
Expand Down Expand Up @@ -30,8 +33,8 @@ impl CurveInterpolation for CurveInterpolator {
}

#[pyclass(name = "Curve", module = "rateslib.rs")]
pub(crate) struct PyCurve{
inner: Curve::<CurveInterpolator>
pub(crate) struct PyCurve {
inner: Curve<CurveInterpolator>,
}

#[pymethods]
Expand All @@ -57,9 +60,15 @@ impl PyCurve {
fn nodes(&self) -> IndexMap<NaiveDateTime, DualsOrF64> {
let nodes = Nodes::from(self.inner.nodes.clone());
match nodes {
Nodes::F64(i) => IndexMap::from_iter(i.into_iter().map(|(k,v)| (k, DualsOrF64::F64(v)))),
Nodes::Dual(i) => IndexMap::from_iter(i.into_iter().map(|(k,v)| (k, DualsOrF64::Dual(v)))),
Nodes::Dual2(i) => IndexMap::from_iter(i.into_iter().map(|(k,v)| (k, DualsOrF64::Dual2(v)))),
Nodes::F64(i) => {
IndexMap::from_iter(i.into_iter().map(|(k, v)| (k, DualsOrF64::F64(v))))
}
Nodes::Dual(i) => {
IndexMap::from_iter(i.into_iter().map(|(k, v)| (k, DualsOrF64::Dual(v))))
}
Nodes::Dual2(i) => {
IndexMap::from_iter(i.into_iter().map(|(k, v)| (k, DualsOrF64::Dual2(v))))
}
}
}

Expand Down
27 changes: 16 additions & 11 deletions src/curves/nodes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::dual::{Dual, Dual2};
use chrono::{NaiveDateTime, DateTime};
use chrono::{DateTime, NaiveDateTime};
use indexmap::IndexMap;

/// Datetime indexed values of a specific [ADOrder](`crate::dual::ADOrder`).
Expand Down Expand Up @@ -35,20 +35,25 @@ impl From<Nodes> for NodesTimestamp {
impl From<NodesTimestamp> for Nodes {
fn from(value: NodesTimestamp) -> Self {
match value {
NodesTimestamp::F64(m) => Nodes::F64(IndexMap::from_iter(
m.into_iter().map(|(k, v)| (DateTime::from_timestamp(k, 0).unwrap().naive_utc(), v)),
)),
NodesTimestamp::Dual(m) => Nodes::Dual(IndexMap::from_iter(
m.into_iter().map(|(k, v)| (DateTime::from_timestamp(k, 0).unwrap().naive_utc(), v)),
)),
NodesTimestamp::Dual2(m) => Nodes::Dual2(IndexMap::from_iter(
m.into_iter().map(|(k, v)| (DateTime::from_timestamp(k, 0).unwrap().naive_utc(), v)),
)),
NodesTimestamp::F64(m) => {
Nodes::F64(IndexMap::from_iter(m.into_iter().map(|(k, v)| {
(DateTime::from_timestamp(k, 0).unwrap().naive_utc(), v)
})))
}
NodesTimestamp::Dual(m) => {
Nodes::Dual(IndexMap::from_iter(m.into_iter().map(|(k, v)| {
(DateTime::from_timestamp(k, 0).unwrap().naive_utc(), v)
})))
}
NodesTimestamp::Dual2(m) => {
Nodes::Dual2(IndexMap::from_iter(m.into_iter().map(|(k, v)| {
(DateTime::from_timestamp(k, 0).unwrap().naive_utc(), v)
})))
}
}
}
}


impl NodesTimestamp {
// fn keys_as_f64(&self) -> Vec<f64> {
// match self {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use splines::spline_py::{
};

pub mod curves;
use curves::curve_py::PyCurve;
use curves::interpolation::interpolation_py::index_left_f64;
use curves::{LinearInterpolator, LinearZeroRateInterpolator, LogLinearInterpolator};
use curves::curve_py::PyCurve;

pub mod calendars;
use calendars::calendar_py::get_calendar_by_name_py;
Expand Down

0 comments on commit e37319b

Please sign in to comment.