Molecule Key Maker

class MoleculeKeyMaker(key_name, get_key)[source]

Bases: object

An abstract base class for making Molecule keys.

Keys are used in stk to determine if two molecules are duplicates of each other.

Notes

You might notice that the public methods of this abstract base class are implemented. This is purely for convenience when implementing subclasses. The implemented public methods are simply default implementations, which can be safely ignored or overridden, when implementing subclasses.

Examples

Subclass Implementation

The source code of any of the subclasses, listed in molecule_key_maker, can serve as good examples.

Creating a New Key Maker Directly

Apart from using the subclasses provided, a MoleculeKeyMaker can be used directly , if you don’t feel like writing a subclass

import stk

# Create a MoleculeKeyMaker instance with a custom get_key
# method.
get_num_atoms = stk.MoleculeKeyMaker(
    key_name='num_atoms',
    get_key=lambda molecule: molecule.get_num_atoms(),
)

# Use the MoleculeKeyMaker instance.
jsonizer = stk.MoleculeJsonizer(
    key_makers=(get_num_atoms, ),
)
# Get the JSON representation of a molecule.
json = jsonizer.to_json(stk.BuildingBlock('NCCN'))

Methods

get_key(molecule)

Get the key of molecule.

get_key_name()

Get the name of the key.

__init__(key_name, get_key)[source]

Initialize a MoleculeKeyMaker instance.

Parameters:
  • key_name (str) – The name of the key.

  • get_key (Callable[[Molecule], object]) – Takes a single parameter, molecule, and returns the key to use for that molecule. The value passed to the parameter must be a Molecule instance.

get_key(molecule)[source]

Get the key of molecule.

Parameters:

molecule (Molecule) – The molecule for which a key is needed.

Return type:

object

Returns:

The key of molecule.

get_key_name()[source]

Get the name of the key.

Return type:

str

Returns:

The name of the key.