ElementSorter
The goal is to sort a list of elements based on many methods.
[1]:
from bobleesj.utils.sorters.element_sorter import ElementSorter
Method 1. By alphabetical order
[2]:
# Fe=55, Si=78
element_sorter = ElementSorter()
element_sorter.sort(["Fe", "Si"])
[2]:
('Si', 'Fe')
Method 2. By custom label
[3]:
label_mapping = {
2: {"A": ["Fe", "Co", "Ni"], "B": ["Si", "Ga", "Ge"]},
3: {
"R": ["Sc", "Y", "La"],
"M": ["Fe", "Co", "Ni"],
"X": ["Si", "Ga", "Ge"],
},
4: {
"A": ["Sc", "Y", "La"],
"B": ["Fe", "Co", "Ni"],
"C": ["Si", "Ga", "Ge"],
"D": ["Gd", "Tb", "Dy"],
},
}
[4]:
element_sorter = ElementSorter(label_mapping=label_mapping)
# You can optionally pass an Excel path instead of label mapping
# element_sorter = ElementSorter(excel_path="path/to/excel.xlsx")
# Use the template Excel file provided in https://github.com/bobleesj/bobleesj.utils/blob/main/tests/data/sort/test-custom-labels.xlsx
[5]:
assert element_sorter.label_mapping == label_mapping
[6]:
test_cases = [
(["Fe", "Si"], ("Fe", "Si")),
(["Si", "Fe"], ("Fe", "Si")),
(["Ge", "Ni"], ("Ni", "Ge")),
(["Ga", "Co"], ("Co", "Ga")),
]
for input_elements, expected in test_cases:
assert element_sorter.sort(input_elements, method="custom") == expected
Method 3. By Mendeleev number
The list of Mendeleev numbers is sourced from bobleesj.utils
.
[7]:
# Fe=55, Si=78
element_sorter.sort(["Fe", "Si"], method="mendeleev")
[7]:
('Si', 'Fe')
[8]:
element_sorter.sort(["Fe", "Si"], method="mendeleev", descending=False)
[8]:
('Fe', 'Si')