Skip to content

Commit 273b2e2

Browse files
authored
Take user input (#2)
* Take user input Although this is meant to be a `module`, many people would prefer to run the program directly. So, I have added the functionality to ask the user for a valid chemical structure. I have also added a `help` command to get usage information. TODO: Maybe add more examples to the help() function? * Changed `comp_struct` to `compound_struct` Used strip() instead of .lstrip().rstrip() * Remove `help()` and use Namer.__doc__
1 parent e644264 commit 273b2e2

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

Carbonpy.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@
1010

1111

1212
class Namer(object): # IUPAC Names for now only
13+
14+
"""
15+
Bonds are represented as follows:
16+
- : Single bond
17+
= : Double bond
18+
~ : Triple bond
19+
20+
Examples:
21+
CH~CH
22+
CH~C-C~C-CH=C=C=CH2
23+
"""
24+
1325
symbol = '\u2261' # The triple bond symbol ≡
1426
subscripts = str.maketrans("0123456789", "₀₁₂₃₄₅₆₇₈₉") # Subscripts for molecular and structural formula
1527

@@ -169,18 +181,24 @@ class ValencyError(Exception):
169181

170182
multipl_suffixes = {2: "di", 3: "tri", 4: "tetra", 5: "penta", 6: "hexa", 7: "hepta", 8: "octa", 9: "nona"}
171183

172-
compound1 = Namer('CH3-C~C-CH3')
173-
compound2 = Namer('CH~CH')
174-
compound3 = Namer('CH~C-C~C-CH=C=C=CH2')
175-
compound4 = Namer('CH4')
176-
compound5 = Namer('CH2=CH-CH=CH-CH=CH2')
177-
compound6 = Namer('CH2=CH2')
178-
compound7 = Namer('CH~C-CH=CH2')
179-
180-
print(f"{compound1}\n{compound1.molecular_formula()}\n{compound1.analyser()}\n")
181-
print(f"{compound2}\n{compound2.molecular_formula()}\n{compound2.analyser()}\n")
182-
print(f"{compound3}\n{compound3.molecular_formula()}\n{compound3.analyser()}\n")
183-
print(f"{compound4}\n{compound4.molecular_formula()}\n{compound4.analyser()}\n")
184-
print(f"{compound5}\n{compound5.molecular_formula()}\n{compound5.analyser()}\n")
185-
print(f"{compound6}\n{compound6.molecular_formula()}\n{compound6.analyser()}\n")
186-
print(f"{compound7}\n{compound7.molecular_formula()}\n{compound7.analyser()}\n")
184+
def main():
185+
print("Type `/help` to get usage information.")
186+
187+
while True:
188+
try:
189+
compound_struct = input("Condensed structure > ")
190+
if compound_struct.strip() == "/help":
191+
print(Namer.__doc__)
192+
else:
193+
compound = Namer(compound_struct.strip())
194+
print(f"\n{compound}\n{compound.molecular_formula()}\n{compound.analyser()}\n")
195+
196+
except EOFError:
197+
print("\nExiting...")
198+
quit()
199+
200+
except Exception:
201+
print("Invalid input!")
202+
203+
if __name__ == "__main__":
204+
main()

0 commit comments

Comments
 (0)