Skip to content

MethodSCRIPT

This page documents the MethodSCRIPT (MS) method.

pypalmsens.MethodScript

Create a MethodSCRIPT method.

With the MethodSCRIPT scripting language, you can write code that runs on-device. This enables custom measurements and makes it possible to combine different measurements and other tasks.

This class uses the PyPalmSens machinery to send your MethodSCRIPT script directly to the potentiostat.

See the MethodSCRIPT documentation for more information and script examples.

Examples:

Return hello world:

>>> import pypalmsens as ps
>>> from textwrap import dedent
>>> method = ps.MethodScript(
...     script=dedent('''\
...        wait 100m
...        if 1 < 2
...            send_string "Hello world"
...        endif
...     ''')
... )

Chronoamperometry measurement loop example:

>>> method = ps.MethodScript(
...     script=dedent('''\
...         var potential
...         var current
...         set_pgstat_chan 0
...         set_pgstat_mode 2
...         cell_on
...         meas_loop_ca potential current 100m 200m 1000m
...             pck_start
...             pck_add potential
...             pck_add current
...             pck_end
...         endloop
...     ''')
... )

Methods:

  • to_dict –

    Return the technique instance as a new key/value dictionary mapping.

  • from_dict –

    Structure technique instance from dict.

  • from_method_id –

    Create new instance of appropriate technique from method ID.

  • from_file –

    Load methodscript from file.

  • to_file –

    Save script to file.

  • from_url –

    Download script over http.

Attributes:

  • id (Literal['ms']) –

    Unique method identifier.

  • version (Literal['1.10'] | None) –

    MethodSCRIPT language version.

  • script (str) –

    Script to run.

  • custom_units (dict[Literal['as', 'at', 'au'], CustomUnits]) –

    Define custom quantities and units for 'as', 'at', 'au' variable types.

id class-attribute instance-attribute

id: Literal['ms'] = 'ms'

Unique method identifier.

version class-attribute instance-attribute

version: Literal['1.10'] | None = '1.10'

MethodSCRIPT language version.

Determines the version of the MethodSCRIPT parser to use for validating the syntax of script. Set to None to skip script validation.

script class-attribute instance-attribute

script: str = 'wait 100m\nif 1 < 2\n    send_string "Hello world"\nendif\n'

Script to run.

For more info on MethodSCRIPT, see: https://www.palmsens.com/methodscript/ for more information.

custom_units class-attribute instance-attribute

custom_units: dict[Literal['as', 'at', 'au'], CustomUnits] = Field(default_factory=dict)

Define custom quantities and units for 'as', 'at', 'au' variable types.

Use this to override the quantities for analog inputs (0, 1, and 2). These default to Voltage / V. Defining custum units is purely cosmetic. The values are used for labeling curves and axes.

to_dict

to_dict() -> dict[str, Any]

Return the technique instance as a new key/value dictionary mapping.

Source code in src/pypalmsens/_methods/base.py
47
48
49
def to_dict(self) -> dict[str, Any]:
    """Return the technique instance as a new key/value dictionary mapping."""
    return self.model_dump()

from_dict classmethod

from_dict(obj: dict[str, Any]) -> MethodType

Structure technique instance from dict.

Opposite of .to_dict()

Source code in src/pypalmsens/_methods/base.py
51
52
53
54
55
56
@classmethod
def from_dict(cls, obj: dict[str, Any]) -> MethodType:
    """Structure technique instance from dict.

    Opposite of `.to_dict()`"""
    return cls.model_validate(obj)  # type:ignore

from_method_id classmethod

from_method_id(id: str) -> MethodType

Create new instance of appropriate technique from method ID.

Source code in src/pypalmsens/_methods/base.py
58
59
60
61
62
@classmethod
def from_method_id(cls, id: str) -> MethodType:
    """Create new instance of appropriate technique from method ID."""
    new = cls._registry[id]
    return new()

from_file classmethod

from_file(path: str | Path = 'methodscript.mscr') -> MethodScript

Load methodscript from file.

Parameters:

  • path

    (str | Path, default: 'methodscript.mscr' ) –

    Path to methodscript file.

Returns:

Source code in src/pypalmsens/_methods/techniques.py
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
@classmethod
def from_file(cls, path: str | Path = 'methodscript.mscr') -> MethodScript:
    """Load methodscript from file.

    Parameters
    ----------
    path: str | Path
        Path to methodscript file.

    Returns
    -------
    method: MethodScript
    """
    with Path(path).open('r') as f:
        script = f.read()

    return cls(script=script)

to_file

to_file(path: str | Path = 'methodscript.mscr') -> None

Save script to file.

Parameters:

  • path

    (str | Path, default: 'methodscript.mscr' ) –

    Path to methodscript file.

Source code in src/pypalmsens/_methods/techniques.py
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
def to_file(self, path: str | Path = 'methodscript.mscr') -> None:
    """Save script to file.

    Parameters
    ----------
    path: str | Path
        Path to methodscript file.
    """
    with Path(path).open('w') as f:
        _ = f.write(self.script)

from_url classmethod

from_url(url: str) -> MethodScript

Download script over http.

Parameters:

  • url

    (url) –

    URL to download methodscript file from.

Returns:

Source code in src/pypalmsens/_methods/techniques.py
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
@classmethod
def from_url(cls, url: str) -> MethodScript:
    """Download script over http.

    Parameters
    ----------
    url : url
        URL to download methodscript file from.

    Returns
    -------
    method: MethodScript
    """
    with urllib.request.urlopen(url) as response:
        script = response.read().decode()

    return cls(script=script)