-
Notifications
You must be signed in to change notification settings - Fork 0
/
price.py
58 lines (42 loc) · 1.63 KB
/
price.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""``metallurgy.price``
=============================
Module enabling calculation of approximate alloy price.
"""
from typing import Union, List
from collections.abc import Iterable
import metallurgy as mg
def price(
alloy: Union[mg.Alloy, str, dict]
) -> Union[float, None, List[Union[float, None]]]:
"""Returns the approximate price per kilogramme of an alloy.
:group: calculations.price
Parameters
----------
alloy : Alloy, str, dict
The alloy for which to calculate the linear mixture.
"""
# If a list of alloys is given, return a list of price data
if isinstance(alloy, Iterable) and not isinstance(alloy, (str, dict)):
return [price(a) for a in alloy]
# Convert input alloy to an Alloy instance if not already
elif not isinstance(alloy, mg.Alloy):
alloy = mg.Alloy(alloy)
# Get the total mass to enable conversion of atomic percent to weight percent
total_mass = mg.linear_mixture(alloy, "mass")
total_price = 0
for element in alloy.elements:
# Only attempt calculation if pricing data is available
if mg.periodic_table.elements[element]["price"] is not None:
# Convert atomic percent to weight percent
weight_percent = (
mg.periodic_table.elements[element]["mass"]
* alloy.composition[element]
/ total_mass
)
# Add weighted contribution to the price approximation
total_price += (
weight_percent * mg.periodic_table.elements[element]["price"]
)
else:
return None
return total_price