ChemInformant.models module#

Pydantic models for representing chemical compound data retrieved from PubChem, and custom exceptions.

exception NotFoundError(identifier)[source]#

Bases: Exception

Custom exception for when a compound identifier cannot be found.

Parameters:

identifier (str | int)

exception AmbiguousIdentifierError(identifier, cids)[source]#

Bases: Exception

Custom exception for when a name maps to multiple CIDs.

Parameters:
class CompoundData(**data)[source]#

Bases: BaseModel

Represents structured information for a chemical compound.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Parameters:
  • cid (int)

  • input_identifier (str | int)

  • common_name (str | None)

  • cas (str | None)

  • unii (str | None)

  • MolecularFormula (str | None)

  • MolecularWeight (float | None)

  • CanonicalSMILES (str | None)

  • IUPACName (str | None)

  • description (str | None)

  • synonyms (List[str])

cid: int#
input_identifier: Union[str, int]#
common_name: Optional[str]#
cas: Optional[str]#
unii: Optional[str]#
molecular_formula: Optional[str]#
molecular_weight: Optional[float]#
canonical_smiles: Optional[str]#
iupac_name: Optional[str]#
description: Optional[str]#
synonyms: List[str]#
model_config: ClassVar[ConfigDict] = {'computed_fields': ['pubchem_url'], 'extra': 'ignore', 'frozen': False, 'populate_by_name': True, 'validate_by_alias': True, 'validate_by_name': True}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

classmethod weight_to_float(v)[source]#

Attempt to convert molecular weight string to float.

Return type:

Optional[float]

Parameters:

v (Any)

property pubchem_url: HttpUrl | None#

Direct URL to the PubChem compound page, computed from CID.

model_copy(*, update=None, deep=False)[source]#
!!! abstract “Usage Documentation”

[model_copy](../concepts/serialization.md#model_copy)

Returns a copy of the model.

!!! note

The underlying instance’s [__dict__][object.__dict__] attribute is copied. This might have unexpected side effects if you store anything in it, on top of the model fields (e.g. the value of [cached properties][functools.cached_property]).

Parameters:
  • update (Optional[Dict[str, Any]]) – Values to change/add in the new model. Note: the data is not validated before creating the new model. You should trust this data.

  • deep (bool) – Set to True to make a deep copy of the model.

  • self (CompoundData)

Return type:

CompoundData

Returns:

New model instance.