From 5c53942b62970f644b1ae00b789e7e30c54df89f Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Wed, 6 Nov 2019 17:42:41 -0500 Subject: [PATCH] Add a class and windchill example. --- README.md | 4 ++++ src/lib.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/README.md b/README.md index 69f53b1..93ff293 100644 --- a/README.md +++ b/README.md @@ -25,3 +25,7 @@ Insure the following dependencies are in place: ## Documentation * [PyO3 Python - Rust bindings](https://pyo3.rs/master/get_started.html) +* Windchill calculations: + * [PDF from US Gov](https://www.weather.gov/media/epz/wxcalc/windChill.pdf) + * [Calculation in Celsius and kph](https://www.calcunation.com/calculator/wind-chill-celsius.php) + * [Windchill Calculator](https://www.weather.gov/epz/wxcalc_windchill) diff --git a/src/lib.rs b/src/lib.rs index e9c6f08..b86119b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,32 @@ extern crate pyo3; use pyo3::prelude::*; +#[pyclass(module = "unit_converter")] +struct Temperature { + celsius: f32, +} + +#[pymethods] +impl Temperature { + #[new] + fn new(obj: &PyRawObject, temperature: f32) { + obj.init(Temperature { + celsius: temperature, + }); + } + + fn to_fahrenheit(&self) -> f32 { + self.celsius * 1.8 + 32.0 + } + + fn windchill(&self, wind_speed_kph: f32) -> f32 { + 13.12 + (0.6215 * self.celsius) - (11.37 * wind_speed_kph.powf(0.16)) + + (0.3965 * self.celsius * wind_speed_kph.powf(0.16)) + } +} + +// TODO: Add a test that -20 C and 32 kph feels like -32.9 + #[pyfunction] fn convert_to_fahrenheit(celsius: f32) -> f32 { celsius * 1.8 + 32.0 @@ -22,6 +48,7 @@ fn batch_convert_to_fahrenheit(celsius: Vec) -> Vec { fn unit_converter(_py: Python, m: &PyModule) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(convert_to_fahrenheit))?; m.add_wrapped(wrap_pyfunction!(batch_convert_to_fahrenheit))?; + m.add_class::()?; Ok(()) }