Skip to main content

Calculates the Molecular Weight from a string of an arbitrary chemical formula.

Project description

PyPI version Actions Status Downloads License

The molecular weight (MW) can be algebraically calculated from any chemical formula that adheres to chemical conventions, which is exemplified in subsequent examples.

The ChemMW object of ChemW parses a chemical formula string – which consists of any combination of elements and decimal stoichiometry – and precisely calculates the MW of the chemical formula, based upon the current physical accuracy that is embedded in the periodic_table dictionary of the chemicals module.

The PHREEQdb object of ChemW parses a PHREEQ database via the ChemMW object and exports a JSON of mineral masses for all of the described minerals in the database. This unique application of the ChemMW object has been applied as the pivotal means of predicting the mass of mineral scaling in the ROSSpy module for reverse osmosis research.

The ChemW module is offered with the MIT License. Examples of the module are available in the examples directory of the ChemW GitHub repository. Please submit errors or inaccuracies as GitHub issues so that they may be resolved.


The following command installs ChemW in a command prompt/terminal environment:

pip install chemw



The data environment, in a Python IDE, is defined:

import chemw
chem_mw = chemw.ChemMW(verbose = False, printing = True)
  • verbose & printing bool: specifies whether troubleshooting information or MW results will be printed, respectively.


The parameterized data is fitted to the Hill equation, with the following arguments and their default values:

  • formula str: parameterizes the chemical formula for which the MW is desired. The acceptable formats for the formula are quite broad, which are exemplified in the following formulae:

Example chemical

Format option


Underscores can arbitrarily separate content, since these are ignored by ChemMW.


An arbitrary number of groups can be distinguished in the chemical formula,

with () denoting the boundaries of the specified group.


Chemical groups can be nested, with differing stoichiometric values.


Water molecules can be complexed,

with a leading stoichiometric quantity of the complexation.


Stoichiometry can be any decimal for any atom in a molecule,

and even omit a leading zero.


Non-water entities can be complexed.


Multiple complexations can be applied with repeated : separators.


The complexity, while remaining within the aforementioned format, is arbitrary.

Accessible content

The ChemMW object retains numerous components that are accessible to the user:

  • mw float: The MW of the parameterized chemical formula.

  • proportions dict: The ratio of elements in the chemical formula. This loses accuracy with the grouped elements, and is being improved.

  • formula str: The original chemical formula as a string.

  • groups int: A numerical counter for the quantity of chemical groups that are

  • group_masses dict: A dictionary for the masses of each nesting level in a molecule.



The data environment, in a Python IDE, is defined:

import chemw
phreeq_db = chemw.PHREEQdb(output_path = None, verbose = False, printing = False)
  • output_path str: optionally specifies an path to where the processed PHREEQ database file will be exported, where None selects the current working directory.

  • verbose & printing bool: optionally specifies whether progress or results of the calculations, respectively, are printed. The former is valuable for troubleshooting while the latter is beneficial for reviewing a readout summary of the calculations.


A PHREEQ database file is processed into a JSON file of the elements and minerals, with their respective formula and MW:

  • db_path str: The path to where the .dat PHREEQ database file that will be processed.

Accessible content

The PHREEQdb object retains numerous components that are accessible to the user:

  • db_name str: The name of the database that is parsed in the process() function.

  • db, minerals, & elements Pandas.DataFrame: The entire PHREEQ database and the minerals and elements of the PHREEQ database, respectively, expressed in a Pandas Database object, and organized with labeled columns of the content.

  • chem_mw ChemMW: An instance of the ChemMW object is loaded, which allows the user to access the ChemMW module through the PHREEQdb module.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ChemW-0.1.1.tar.gz (523.3 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page