Skip to content

Commit

Permalink
Fix clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Oct 29, 2023
1 parent 15ac137 commit 75e7dac
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 42 deletions.
1 change: 1 addition & 0 deletions nautilus_core/core/src/python/casing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use heck::ToSnakeCase;
use pyo3::prelude::*;

#[must_use]
#[pyfunction(name = "convert_to_snake_case")]
pub fn py_convert_to_snake_case(s: String) -> String {
s.to_snake_case()
Expand Down
7 changes: 7 additions & 0 deletions nautilus_core/core/src/python/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,43 @@ use crate::datetime::{
secs_to_millis, secs_to_nanos, unix_nanos_to_iso8601,
};

#[must_use]
#[pyfunction(name = "secs_to_nanos")]
pub fn py_secs_to_nanos(secs: f64) -> u64 {
secs_to_nanos(secs)
}

#[must_use]
#[pyfunction(name = "secs_to_millis")]
pub fn py_secs_to_millis(secs: f64) -> u64 {
secs_to_millis(secs)
}

#[must_use]
#[pyfunction(name = "millis_to_nanos")]
pub fn py_millis_to_nanos(millis: f64) -> u64 {
millis_to_nanos(millis)
}

#[must_use]
#[pyfunction(name = "micros_to_nanos")]
pub fn py_micros_to_nanos(micros: f64) -> u64 {
micros_to_nanos(micros)
}

#[must_use]
#[pyfunction(name = "nanos_to_secs")]
pub fn py_nanos_to_secs(nanos: u64) -> f64 {
nanos_to_secs(nanos)
}

#[must_use]
#[pyfunction(name = "nanos_to_millis")]
pub fn py_nanos_to_millis(nanos: u64) -> u64 {
nanos_to_millis(nanos)
}

#[must_use]
#[pyfunction(name = "nanos_to_micros")]
pub fn py_nanos_to_micros(nanos: u64) -> u64 {
nanos_to_micros(nanos)
Expand Down
1 change: 1 addition & 0 deletions nautilus_core/indicators/src/average/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub enum MovingAverageType {
pub struct MovingAverageFactory;

impl MovingAverageFactory {
#[must_use]
pub fn create(
moving_average_type: MovingAverageType,
period: usize,
Expand Down
16 changes: 8 additions & 8 deletions nautilus_core/indicators/src/average/wma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ impl WeightedMovingAverage {
fn weighted_average(&self) -> f64 {
let mut sum = 0.0;
let mut weight_sum = 0.0;
let reverse_weights: Vec<f64> = self.weights.iter().cloned().rev().collect();
let reverse_weights: Vec<f64> = self.weights.iter().copied().rev().collect();
for (index, input) in self.inputs.iter().rev().enumerate() {
let weight = reverse_weights.get(index).unwrap();
sum += input * weight;
weight_sum += weight
weight_sum += weight;
}
sum / weight_sum
}
Expand Down Expand Up @@ -152,7 +152,7 @@ mod tests {

#[rstest]
fn test_wma_initialized(indicator_wma_10: WeightedMovingAverage) {
let display_str = format!("{}", indicator_wma_10);
let display_str = format!("{indicator_wma_10}");
assert_eq!(
display_str,
"WeightedMovingAverage(10,[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])"
Expand Down Expand Up @@ -196,7 +196,7 @@ mod tests {
fn test_value_with_two_inputs(mut indicator_wma_10: WeightedMovingAverage) {
indicator_wma_10.update_raw(1.0);
indicator_wma_10.update_raw(2.0);
let result = (2.0 * 1.0 + 1.0 * 0.9) / 1.9;
let result = 2.0f64.mul_add(1.0, 1.0 * 0.9) / 1.9;
assert_eq!(indicator_wma_10.value, result);
}

Expand All @@ -205,24 +205,24 @@ mod tests {
indicator_wma_10.update_raw(1.0);
indicator_wma_10.update_raw(2.0);
indicator_wma_10.update_raw(3.0);
let result = (3.0 * 1.0 + 2.0 * 0.9 + 1.0 * 0.8) / (1.0 + 0.9 + 0.8);
let result = 1.0f64.mul_add(0.8, 3.0f64.mul_add(1.0, 2.0 * 0.9)) / (1.0 + 0.9 + 0.8);
assert_eq!(indicator_wma_10.value, result);
}

#[rstest]
fn test_value_expected_with_exact_period(mut indicator_wma_10: WeightedMovingAverage) {
for i in 1..11 {
indicator_wma_10.update_raw(i as f64);
indicator_wma_10.update_raw(f64::from(i));
}
assert_eq!(indicator_wma_10.value, 7.0);
}

#[rstest]
fn test_value_expected_with_more_inputs(mut indicator_wma_10: WeightedMovingAverage) {
for i in 1..=11 {
indicator_wma_10.update_raw(i as f64);
indicator_wma_10.update_raw(f64::from(i));
}
assert_eq!(indicator_wma_10.value(), 8.0000000000000018);
assert_eq!(indicator_wma_10.value(), 8.000_000_000_000_002);
}

#[rstest]
Expand Down
34 changes: 17 additions & 17 deletions nautilus_core/indicators/src/momentum/rsi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl RelativeStrengthIndex {
pub fn update_raw(&mut self, value: f64) {
if !self._has_inputs {
self._last_value = value;
self._has_inputs = true
self._has_inputs = true;
}
let gain = value - self._last_value;
if gain > 0.0 {
Expand Down Expand Up @@ -153,40 +153,40 @@ mod tests {

#[rstest]
fn test_rsi_initialized(rsi_10: RelativeStrengthIndex) {
let display_str = format!("{}", rsi_10);
let display_str = format!("{rsi_10}");
assert_eq!(display_str, "RelativeStrengthIndex(10,EXPONENTIAL)");
assert_eq!(rsi_10.period, 10);
assert_eq!(rsi_10.is_initialized, false)
assert!(!rsi_10.is_initialized);
}

#[rstest]
fn test_initialized_with_required_inputs_returns_true(mut rsi_10: RelativeStrengthIndex) {
for i in 0..12 {
rsi_10.update_raw(i as f64);
rsi_10.update_raw(f64::from(i));
}
assert_eq!(rsi_10.is_initialized, true)
assert!(rsi_10.is_initialized);
}

#[rstest]
fn test_value_with_one_input_returns_expected_value(mut rsi_10: RelativeStrengthIndex) {
rsi_10.update_raw(1.0);
assert_eq!(rsi_10.value, 1.0)
assert_eq!(rsi_10.value, 1.0);
}

#[rstest]
fn test_value_all_higher_inputs_returns_expected_value(mut rsi_10: RelativeStrengthIndex) {
for i in 1..4 {
rsi_10.update_raw(i as f64);
rsi_10.update_raw(f64::from(i));
}
assert_eq!(rsi_10.value, 1.0)
assert_eq!(rsi_10.value, 1.0);
}

#[rstest]
fn test_value_with_all_lower_inputs_returns_expected_value(mut rsi_10: RelativeStrengthIndex) {
for i in (1..4).rev() {
rsi_10.update_raw(i as f64);
rsi_10.update_raw(f64::from(i));
}
assert_eq!(rsi_10.value, 0.0)
assert_eq!(rsi_10.value, 0.0);
}

#[rstest]
Expand All @@ -198,7 +198,7 @@ mod tests {
rsi_10.update_raw(7.0);
rsi_10.update_raw(6.0);

assert_eq!(rsi_10.value, 0.6837363325825265)
assert_eq!(rsi_10.value, 0.683_736_332_582_526_5);
}

#[rstest]
Expand All @@ -212,36 +212,36 @@ mod tests {
rsi_10.update_raw(6.0);
rsi_10.update_raw(7.0);

assert_eq!(rsi_10.value, 0.7615344667662725);
assert_eq!(rsi_10.value, 0.761_534_466_766_272_5);
}

#[rstest]
fn test_reset(mut rsi_10: RelativeStrengthIndex) {
rsi_10.update_raw(1.0);
rsi_10.update_raw(2.0);
rsi_10.reset();
assert_eq!(rsi_10.is_initialized(), false);
assert_eq!(rsi_10.count, 0)
assert!(!rsi_10.is_initialized());
assert_eq!(rsi_10.count, 0);
}

#[rstest]
fn test_handle_quote_tick(mut rsi_10: RelativeStrengthIndex, quote_tick: QuoteTick) {
rsi_10.handle_quote_tick(&quote_tick);
assert_eq!(rsi_10.count, 1);
assert_eq!(rsi_10.value, 1.0)
assert_eq!(rsi_10.value, 1.0);
}

#[rstest]
fn test_handle_trade_tick(mut rsi_10: RelativeStrengthIndex, trade_tick: TradeTick) {
rsi_10.handle_trade_tick(&trade_tick);
assert_eq!(rsi_10.count, 1);
assert_eq!(rsi_10.value, 1.0)
assert_eq!(rsi_10.value, 1.0);
}

#[rstest]
fn test_handle_bar(mut rsi_10: RelativeStrengthIndex, bar_ethusdt_binance_minute_bid: Bar) {
rsi_10.handle_bar(&bar_ethusdt_binance_minute_bid);
assert_eq!(rsi_10.count, 1);
assert_eq!(rsi_10.value, 1.0)
assert_eq!(rsi_10.value, 1.0);
}
}
4 changes: 2 additions & 2 deletions nautilus_core/network/src/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ impl WebSocketClientInner {
let mut request = url.into_client_request()?;
let req_headers = request.headers_mut();

headers.into_iter().for_each(|(key, val)| {
for (key, val) in headers {
let header_value = HeaderValue::from_str(&val).unwrap();
let header_name = HeaderName::from_str(&key).unwrap();
req_headers.insert(header_name, header_value);
});
}

connect_async(request).await.map(|resp| resp.0.split())
}
Expand Down
19 changes: 9 additions & 10 deletions nautilus_core/persistence/src/backend/kmerge_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<T> EagerStream<T> {
.await;
});

EagerStream { rx, task, runtime }
Self { rx, task, runtime }
}
}

Expand Down Expand Up @@ -85,7 +85,7 @@ where
match iter.next() {
Some(mut batch) => match batch.next() {
Some(item) => {
break Some(ElementBatchIter { item, batch, iter });
break Some(Self { item, batch, iter });
}
None => continue,
},
Expand Down Expand Up @@ -276,7 +276,7 @@ mod tests {
let mut vec: Vec<u64> = Arbitrary::arbitrary(g);

// Sort the vector
vec.sort();
vec.sort_unstable();

// Recreate nested Vec structure by splitting the flattened_sorted_vec into sorted chunks
let mut nested_sorted_vec = Vec::new();
Expand All @@ -292,7 +292,7 @@ mod tests {
}

// Wrap the sorted nested vector in the SortedNestedVecU64 struct
SortedNestedVec(nested_sorted_vec)
Self(nested_sorted_vec)
}

// Optionally, implement the `shrink` method if you want to shrink the generated data on test failures
Expand All @@ -306,18 +306,17 @@ mod tests {
let mut kmerge: KMerge<_, u64, _> = KMerge::new(OrdComparator);

let copy_data = all_data.clone();
copy_data.into_iter().for_each(|stream| {
let input = stream.0.into_iter().map(|batch| batch.into_iter());
for stream in copy_data.into_iter() {
let input = stream.0.into_iter().map(std::iter::IntoIterator::into_iter);
kmerge.push_iter(input);
});
}
let merged_data: Vec<u64> = kmerge.collect();

let mut sorted_data: Vec<u64> = all_data
.into_iter()
.map(|stream| stream.0.into_iter().flatten())
.flatten()
.flat_map(|stream| stream.0.into_iter().flatten())
.collect();
sorted_data.sort();
sorted_data.sort_unstable();

merged_data.len() == sorted_data.len() && merged_data.eq(&sorted_data)
}
Expand Down
10 changes: 5 additions & 5 deletions nautilus_core/persistence/src/backend/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ impl DataBackendSession {
/// Query a file for its records. the caller must specify `T` to indicate
/// the kind of data expected from this query.
///
/// table_name: Logical table_name assigned to this file. Queries to this file should address the
/// `table_name`: Logical `table_name` assigned to this file. Queries to this file should address the
/// file by its table name.
/// file_path: Path to file
/// sql_query: A custom sql query to retrieve records from file. If no query is provided a default
/// query "SELECT * FROM <table_name>" is run.
/// `file_path`: Path to file
/// `sql_query`: A custom sql query to retrieve records from file. If no query is provided a default
/// query "SELECT * FROM <`table_name`>" is run.
///
/// # Safety
/// The file data must be ordered by the ts_init in ascending order for this
/// The file data must be ordered by the `ts_init` in ascending order for this
/// to work correctly.
pub fn add_file<T>(
&mut self,
Expand Down

0 comments on commit 75e7dac

Please sign in to comment.