What's it all about?
Beginning this project elem_info I wanted to create a useful application that had access to real world data and can be used in an academic/scientific environment. One of my roommates was struggling calculating mass percentages of long chemical compounds in his chemistry class, which is what sparked my idea.
The Idea
- Have access to elements with all their possibly known information on them
- Be able to sort these elements by period or group
- Be able to get some background information on the element
- Be able to build chemical compounds from a CLI and get information on said compound
The Pitfalls
I decided to go with Wikipedia for my scraping source, as I felt that this would stay up to date with the most recent findings as well as staying available to bots for a long period of time.
As this is good for the future, some data is non-existent or malformed and requires a lot of regex to scrape correctly.
Calculating Mass Percentage
Originally I wanted to use a pre-existing calculator to scrape this data and graphs from the compound name (as a string 'CO2') and output it and store this information for quick access. Although the site I wanted to use didn't allow robots (I could only wonder why).
Thus I went on to create my own class for building chemical compounds and then being able to calculate said details from our 'deeper' level of elemental information.
The Cool Stuff
When viewing a singular element via the CLI the first time you view said element it will scrape basic history and some other information from that elements wikipedia page and store it in that elements object. This is great because the next time you want to view that element the program before scraping will ask itself, "Does this element already have the extra information?", and if so it will just use the already stored information instead of re-scraping.
Sorting Elements
One of the most useful features, in my opinion, would be the sorting of the elements. For example you can call Element.get_by_group(2)
or Element.get_by_period(1)
to get elements by groups/period. Which also allows us to select elements that are only in a specific group and period!
group = Element.get_by_group(1)
period = Element.get_by_period(1)
group & period # => <ElemInfo:Element @name: Hydrogen ...>
Creating Compounds
To create a compound it as simple as the following:
# Assuming Oxygen & Carbon are Element objects
comp = Compound.new
comp.add(Oxygen)
comp.add(Carbon, 2)
# Now we can find our total mass!
comp.calculate # => total_mass
# Or we can see the entire chemical makeup as a hash
comp.makeup # => the hash makeup of the compound
# Iterates over the hash to calculate and display
comp.calculate_and_display # => the compounds detailed information
Conclusion
All in all I am happy with how this turned out and will most likely use this a lot in the near future for Environmental Chemistry and hope that this tool can be helpful to any who may use it or who need to experiment with creating new compounds.
Top comments (0)