BioSTEAMDevelopmentGroup / thermosteam

BioSTEAM's Premier Thermodynamic Engine
Other
57 stars 12 forks source link

Chemical cache decorator #84

Closed yoelcortes closed 1 year ago

yoelcortes commented 1 year ago

This is a simple enhancement, so I am requesting newer members for their review :)

The pull request adds a new chemical_cache decorator that acts like functools.cache but with the added feature that it avoids recreating Chemical objects.

This pull request also fixes a bug so that when the default Chemical.cache is True, chemicals are cached (unless Chemical(..., cache=False)).

Here is the example which was added to the tests:

@tmo.utils.chemical_cache
def create_ethanol_chemicals(other_chemicals):
    chemicals = tmo.Chemicals(['Water', 'Ethanol', *other_chemicals])
    return chemicals

chemicals_a = create_ethanol_chemicals(('CO2', 'O2'))
chemicals_b = create_ethanol_chemicals(('CO2', 'O2'))
assert chemicals_a is chemicals_b

chemicals_c = create_ethanol_chemicals(('O2',))
assert chemicals_a is not chemicals_c 
assert chemicals_a.Water is chemicals_c.Water
assert chemicals_b.O2 is chemicals_c.O2