aoptk.chemical

Classes

Chemical

Data structure representing a chemical.

Module Contents

class aoptk.chemical.Chemical(name: str)[source]

Data structure representing a chemical.

Instances are considered equal when their name values are equal. The class implements __hash__ so instances can be used as dict keys or set members.

_name[source]
_heading: str | None = None[source]
_synonyms: set[str][source]
property synonyms: set[str][source]

Synonyms of the chemical.

Returns:

All synonyms of the chemical.

Return type:

set[str]

property heading: str | None[source]

Heading or canonical name of the chemical.

Only available after normalization or if it is set manually after object instantiation.

Returns:

Canonical name of the chemical or None

Return type:

str | None

property name: str[source]

Return the chemical’s name.

__all_names() set[str][source]

Get all names for the chemical.

Returns:

Union of synonyms, name and heading.

Return type:

set[str]

similar(other: Chemical) bool[source]

Test if two chemicals are similar, so if any of their names match.

Chemicals that are equal are also always similar.

Parameters:

other (Chemical) – Chemical to compare to.

Returns:

True if any of the names match, False otherwise.

Return type:

bool

__str__() str[source]

Return a human-friendly string for the chemical (its name).

__eq__(other: object) bool[source]

Compare two Chemical instances for equality.

Returns NotImplemented when other is not a Chemical, allowing Python to try reflected comparisons or fall back to False.

__eq_chemical(other: Chemical) bool[source]

Private helper to compare two chemicals.

Parameters:

other (Chemical) – Chemical to compare.

Returns:

If headings are present, is true if heading matches. Otherwise use given name instead.

Return type:

bool

__eq_object(other: object) bool[source]

Private helper to compare a Chemical to a non-Chemical object.

This is called from __eq__() when other is not a Chemical. The method compares the string representation of other to this instance’s heading (when present) and name.

Parameters:

other (object) – Object to compare. Its str() value is used for the comparison.

Returns:

True if the string representation of other equals the chemical’s heading (if set) or name, False otherwise.

Return type:

bool

__hash__() int[source]

Return a hash based on the chemical name.

This ensures objects that compare equal also have the same hash, which is required for correct behaviour in sets and dict keys.