New Command: Difference between revisions

From PyMOLWiki
Jump to navigation Jump to search
m (PedroLacerda moved page Declare Command to New Command: The experimental feature changed its name.)
No edit summary
Line 1: Line 1:
==The problem==
IN ACTIVE DEVELOPMENT


Current PyMOL approach to new plugin commands is outdated.
[[new_command]] is an API-only feature which exposes a developer defined Python function as a command to other users.


==The proposal==
== In a brief ==


Introduce a new system based on modern Python with type checking and implicit type coercion.
If you need more examples, here a non exhaustive list of examples: [https://github.com/schrodinger/pymol-open-source/blob/9c923999732644743565deb99efcc642ecd15442/testing/tests/api/test_commanding.py]


==What works right now?==
<source lang="python">
from pymol import cmd
from pathlib import Path
from typing import List, Tuple, Union, Any


There's some support on PyMOL open-source, but not on Incentive. However it isn't working for all possible cases.
Point = Tuple[float, float, float]


 
@cmd.new_command
<source lang="python">
def nice_tool(
@cmd.declare_command
     my_var: Union[int | float],
def new_command(
    a_point: Point,
     my_var: int | float,
    title: str,
     dirname: Path = '.',
    other_point: Tuple[int, int, int] = (0, 0, 0),
    # Optional values aren't currently supported.
     dirname: Path = '.', # a bit ugly, but works as expected on command-line
     # Tuples are.
     this_list: Union[List[bool], None] = None,
    nullable_point: Optional[Tuple[int, int, int]] = None,
     extended_calculation: bool = True,
     extended_calculation: bool = True,
     old_style: Any = "Support anything currently not supported",
     old_style: Any = "Support anything as raw string",
    quiet: bool = 1,  # special 'quiet=False' on command-line
    _self=cmd  # special for multi-threaded applications
):
):
     """
     "A cool docstring."
    A cool docstring.
     print(this_list)
    """
     pass
</source>
</source>
== Details ==
It improves on [[extend]], the consolidated exposing mechanism, and works by parsing arguments given by users at command-line, enforcing correct types at runtime, freeing developers from it and ensuring typing strictness. It is also advantageous for developers consuming the exposed function/command directly by the API as types can also be enforced statically by MyPy.

Revision as of 11:47, 29 November 2025

IN ACTIVE DEVELOPMENT

new_command is an API-only feature which exposes a developer defined Python function as a command to other users.

In a brief

If you need more examples, here a non exhaustive list of examples: [1]

from pymol import cmd
from pathlib import Path
from typing import List, Tuple, Union, Any

Point = Tuple[float, float, float]

@cmd.new_command
def nice_tool(
    my_var: Union[int | float],
    a_point: Point,
    title: str,
    other_point: Tuple[int, int, int] = (0, 0, 0),
    dirname: Path = '.',  # a bit ugly, but works as expected on command-line
    this_list: Union[List[bool], None] = None,
    extended_calculation: bool = True,
    old_style: Any = "Support anything as raw string",
    quiet: bool = 1,  # special 'quiet=False' on command-line
    _self=cmd  # special for multi-threaded applications
):
    "A cool docstring."
    print(this_list)

Details

It improves on extend, the consolidated exposing mechanism, and works by parsing arguments given by users at command-line, enforcing correct types at runtime, freeing developers from it and ensuring typing strictness. It is also advantageous for developers consuming the exposed function/command directly by the API as types can also be enforced statically by MyPy.