Skip to content

Commit 36cea21

Browse files
committed
Update
1 parent 9442ab0 commit 36cea21

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

highpymath/__init__.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,12 @@ def trapezoid(a: any, b: any, c: any = None, d: any = None, h: any = None, retur
765765
raise MathTypeError("a must be a number")
766766
if not isinstance(b, (int, float)):
767767
raise MathTypeError("b must be a number")
768+
if return_area or return_both and not isinstance(h, (int, float)):
769+
raise MathTypeError("h must be a number")
770+
if return_circumference or return_both and not isinstance(c, (int, float)):
771+
raise MathTypeError("c must be a number")
772+
if return_circumference or return_both and not isinstance(d, (int, float)):
773+
raise MathTypeError("d must be a number")
768774
if return_area or return_both and isinstance(h, int):
769775
h = float(h)
770776
if return_circumference or return_both and isinstance(c, int):
@@ -801,6 +807,68 @@ def trapezoid(a: any, b: any, c: any = None, d: any = None, h: any = None, retur
801807
elif return_circumference:
802808
return _circumference
803809

810+
@staticmethod
811+
def parallelogram(a: any, b: any = None, h: any = None, return_area: bool = False, return_circumference: bool = False, return_both: bool = True, return_int: bool = False, return_string: bool = False):
812+
"""
813+
Calculate the Area or the Circumference of a Parallelogram.
814+
"""
815+
from .highpymath import parallelogram_area as _pa
816+
from .highpymath import parallelogram_circumference as _pc
817+
if not return_area and not return_circumference and not return_both:
818+
raise MathValueError("You need to specify one of the 3 arguments")
819+
if return_area and return_circumference:
820+
raise MathValueError("You need to specify one of the 3 arguments")
821+
if return_area and return_both:
822+
return_both = False
823+
if return_circumference and return_both:
824+
return_both = False
825+
return_float = True
826+
if return_int:
827+
return_float = False
828+
if return_area and h is None:
829+
raise MathValueError("You need to specify h")
830+
if return_circumference or return_both and b is None:
831+
raise MathValueError("You need to specify b")
832+
if not isinstance(a, (int, float)):
833+
raise MathTypeError("a must be a number")
834+
if return_area or return_both and not isinstance(h, (int, float)):
835+
raise MathTypeError("h must be a number")
836+
if return_circumference or return_both and not isinstance(b, (int, float)):
837+
raise MathTypeError("b must be a number")
838+
if isinstance(a, int):
839+
a = float(a)
840+
if isinstance(b, int):
841+
b = float(b)
842+
if return_area and isinstance(h, int):
843+
h = float(h)
844+
if return_circumference and isinstance(b, int):
845+
b = float(b)
846+
if return_area or return_both:
847+
_area = _pa(a=a, h=h)
848+
if return_circumference or return_both:
849+
_circumference = _pc(a=a, b=b)
850+
if return_int:
851+
if _area:
852+
_area = int(_area)
853+
if _circumference:
854+
_circumference = int(_circumference)
855+
elif return_float:
856+
if _area:
857+
_area = float(_area)
858+
if _circumference:
859+
_circumference = float(_circumference)
860+
if return_string:
861+
if _area:
862+
_area = str(_area)
863+
if _circumference:
864+
_circumference = str(_circumference)
865+
if return_both:
866+
return _area, _circumference
867+
elif return_area:
868+
return _area
869+
elif return_circumference:
870+
return _circumference
871+
804872
GeometricProperties2D = GeometricProperties2D()
805873

806874
__all__.append('GeometricProperties2D')

src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,16 @@ fn trapezoid_circumference(a: PyFloat, b: PyFloat, c: PyFloat, d: PyFloat) -> Py
290290
Ok(a + b + c + d)
291291
}
292292

293+
#[pyfunction]
294+
fn parallelogram_area(a: PyFloat, h: PyFloat) -> PyResult<PyFloat> {
295+
Ok(a * h)
296+
}
297+
298+
#[pyfunction]
299+
fn parallelogram_circumference(a: PyFloat, b: PyFloat) -> PyResult<PyFloat> {
300+
Ok(2.0 * (a + b))
301+
}
302+
293303
/// A Python module implemented in Rust.
294304
#[pymodule]
295305
fn highpymath(m: &PyModule) -> PyResult<()> {
@@ -325,6 +335,8 @@ fn highpymath(m: &PyModule) -> PyResult<()> {
325335
m.add_function(wrap_pyfunction!(circle_circumference, m)?)?;
326336
m.add_function(wrap_pyfunction!(trapezoid_area, m)?)?;
327337
m.add_function(wrap_pyfunction!(trapezoid_circumference, m)?)?;
338+
m.add_function(wrap_pyfunction!(parallelogram_area, m)?)?;
339+
m.add_function(wrap_pyfunction!(parallelogram_circumference, m)?)?;
328340
m.add("MathTypeError", m.py().get_type::<MathTypeError>())?;
329341
m.add("MathValueError", m.py().get_type::<MathValueError>())?;
330342
m.add("GeometryError", m.py().get_type::<GeometryError>())?;

0 commit comments

Comments
 (0)