Skip to content

Commit 23e071e

Browse files
committed
docs/feat: improve shear stress logic with foundational physical principles
- Refactored interface to use None for unknown variables (more idiomatic) - Enhanced documentation with core mechanical context - Added robust error handling for physical constraints and division by zero
1 parent ca5b8c1 commit 23e071e

File tree

1 file changed

+62
-45
lines changed

1 file changed

+62
-45
lines changed

physics/shear_stress.py

Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,76 @@
11
from __future__ import annotations
22

33
"""
4-
Shear stress is a component of stress that is coplanar to the material cross-section.
5-
It arises due to a shear force, the component of the force vector parallel to the
6-
material cross-section.
4+
In foundational mechanics, shear stress is defined as a stress component that acts
5+
parallel to the material's cross-section. It arises from a shear force, which is the
6+
component of the force vector acting coplanar with the cross-section.
77
8-
https://en.wikipedia.org/wiki/Shear_stress
8+
The relationship is governed by the core formula: tau = F / A
9+
where:
10+
tau (τ) is the shear stress
11+
F is the tangential force (shear force)
12+
A is the cross-sectional area
13+
14+
Reference: https://en.wikipedia.org/wiki/Shear_stress
915
"""
1016

1117

12-
def shear_stress(
13-
stress: float,
14-
tangential_force: float,
15-
area: float,
16-
) -> tuple[str, float]:
18+
def calculate_shear_stress(
19+
shear_stress: float | None = None,
20+
tangential_force: float | None = None,
21+
area: float | None = None,
22+
) -> float:
1723
"""
18-
This function can calculate any one of the three -
19-
1. Shear Stress
20-
2. Tangential Force
21-
3. Cross-sectional Area
22-
This is calculated from the other two provided values
23-
Examples -
24-
>>> shear_stress(stress=25, tangential_force=100, area=0)
25-
('area', 4.0)
26-
>>> shear_stress(stress=0, tangential_force=1600, area=200)
27-
('stress', 8.0)
28-
>>> shear_stress(stress=1000, tangential_force=0, area=1200)
29-
('tangential_force', 1200000)
24+
Calculates the missing variable in the shear stress formula (tau = F / A).
25+
Exactly two of the parameters must be provided.
26+
27+
Args:
28+
shear_stress: The stress parallel to the cross-section (Pascal).
29+
tangential_force: The force acting parallel to the cross-section (Newton).
30+
area: The cross-sectional area (Square meters).
31+
32+
Returns:
33+
The calculated missing value (shear_stress, tangential_force, or area).
34+
35+
Raises:
36+
ValueError: If fewer or more than two parameters are provided,
37+
if a parameter is negative, or if a division by zero occurs.
38+
39+
Examples:
40+
>>> calculate_shear_stress(tangential_force=100.0, area=4.0)
41+
25.0
42+
>>> calculate_shear_stress(shear_stress=8.0, area=200.0)
43+
1600.0
44+
>>> calculate_shear_stress(shear_stress=1000.0, tangential_force=1200000.0)
45+
1200.0
3046
"""
31-
if (stress, tangential_force, area).count(0) != 1:
32-
raise ValueError("You cannot supply more or less than 2 values")
33-
elif stress < 0:
34-
raise ValueError("Stress cannot be negative")
35-
elif tangential_force < 0:
36-
raise ValueError("Tangential Force cannot be negative")
37-
elif area < 0:
38-
raise ValueError("Area cannot be negative")
39-
elif stress == 0:
40-
return (
41-
"stress",
42-
tangential_force / area,
43-
)
44-
elif tangential_force == 0:
45-
return (
46-
"tangential_force",
47-
stress * area,
48-
)
49-
else:
50-
return (
51-
"area",
52-
tangential_force / stress,
53-
)
47+
params = (shear_stress, tangential_force, area)
48+
none_count = params.count(None)
49+
50+
if none_count != 1:
51+
raise ValueError("Exactly two values must be provided to calculate the third.")
52+
53+
# Validation: Physically, these values should not be negative in this context
54+
if any(p is not None and p < 0 for p in params):
55+
raise ValueError("Parameters cannot be negative.")
56+
57+
if shear_stress is None:
58+
if area == 0:
59+
raise ValueError("Area cannot be zero when calculating stress.")
60+
return tangential_force / area
61+
62+
if tangential_force is None:
63+
return shear_stress * area
64+
65+
if area is None:
66+
if shear_stress == 0:
67+
raise ValueError("Shear stress cannot be zero when calculating area.")
68+
return tangential_force / shear_stress
69+
70+
return 0.0
5471

5572

5673
if __name__ == "__main__":
5774
import doctest
5875

59-
doctest.testmod()
76+
doctest.testmod()

0 commit comments

Comments
 (0)