-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelements.py
186 lines (139 loc) · 4.15 KB
/
elements.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 25 07:27:35 2022
@author: nicemicro
"""
from typing import Any
class Element:
"""
Abstract class that stores information about an element.
Members:
_symbol: str, the chemical symbol of an atom
_valence_el: int, number of valence electrons
_fullshell: int, the number of electrons in a filled valence shell
_hypervalent: bool, whether the atom can accomodate more electrons
then the full valence shell
"""
_symbol: str = ""
_valence_el: int = 0
_fullshell: int = 8
_hypervalent: bool = False
def get_symbol(self):
return self._symbol
def get_valence_el(self):
return self._valence_el
def get_fullshell(self):
return self._fullshell
def get_hypervalent(self):
return self._hypervalent
def set_attr (self, new):
raise AttributeError("Can't modify element properties")
def del_attr(self):
raise AttributeError("Can't delete element properties")
symbol = property(get_symbol, set_attr, del_attr)
valence_el = property(get_valence_el, set_attr, del_attr)
fullshell = property(get_fullshell, set_attr, del_attr)
hypervalent = property(get_hypervalent, set_attr, del_attr)
def to_dict(self) -> dict[str, str]:
desc: dict[str, str] = {}
desc["valence_el"] = str(self._valence_el)
desc["fullshell"] = str(self._fullshell)
desc["hypervalent"] = str(self.hypervalent)
return desc
class CustomElement(Element):
def get_fullshell(self) -> int:
if self._fullshell == 0:
return 9999
return self._fullshell
def get_valence_el(self) -> int:
if self._valence_el == 0:
return 9999
return self._valence_el
def __init__(
self,
symbol: str,
valence_el: int = 0,
fullshell: int = 0,
hypervalent: bool = True
):
self._symbol = symbol
self._valence_el = valence_el
self._fullshell = fullshell
self._hypervalent = hypervalent
super().__init__()
def set_attr (self, new):
super().set_attr(new)
def del_attr(self):
super().del_attr()
valence_el = property(get_valence_el, set_attr, del_attr)
fullshell = property(get_fullshell, set_attr, del_attr)
class Hydrogen(Element):
_symbol = "H"
_valence_el = 1
_fullshell = 2
class Boron(Element):
_symbol = "B"
_valence_el = 3
class Carbon(Element):
_symbol = "C"
_valence_el = 4
class Nitrogen(Element):
_symbol = "N"
_valence_el = 5
class Oxygen(Element):
_symbol = "O"
_valence_el = 6
class Fluorine(Element):
_symbol = "F"
_valence_el = 7
class Silicon(Element):
_symbol = "Si"
_valence_el = 4
_hypervalent = True
class Phosphorus(Element):
_symbol = "P"
_valence_el = 5
_hypervalent = True
class Sulphur(Element):
_symbol = "S"
_valence_el = 6
_hypervalent = True
class Chlorine(Element):
_symbol = "Cl"
_valence_el = 7
_hypervalent = True
class Iodine(Element):
_symbol = "I"
_valence_el = 7
_hypervalent = True
class Xenon(Element):
_symbol = "Xe"
_valence_el = 8
_hypervalent = True
def element_from_dict(symbol: str, params: dict[str, Any]) -> Element:
valence_el: int = 0
fullshell: int = 0
hypervalent: bool = False
for key, val in params.items():
value: str = str(val)
if key == "valence_el":
valence_el = int(value)
if key == "fullshell":
fullshell = int(value)
if key == "hypervalent":
hypervalent = (value == "True")
return CustomElement(symbol, valence_el, fullshell, hypervalent)
element_table: list[Element] = []
element_table.append(Hydrogen())
element_table.append(Boron())
element_table.append(Carbon())
element_table.append(Nitrogen())
element_table.append(Oxygen())
element_table.append(Fluorine())
element_table.append(Silicon())
element_table.append(Phosphorus())
element_table.append(Sulphur())
element_table.append(Chlorine())
element_table.append(Iodine())
element_table.append(Xenon())