Demo

This example shows the output from attr_utils.annotations and attr_utils.autoattrs.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
"""
This example is based on real code.
"""

# stdlib
from typing import Any, Dict, List, Optional, Sequence, Tuple, Union, overload

# 3rd party
import attr
from domdf_python_tools.utils import strtobool

# this package
from attr_utils.annotations import attrib
from attr_utils.pprinter import pretty_repr
from attr_utils.serialise import serde


@pretty_repr
@serde
@attr.s(slots=True)
class Device:
    """
    Represents a device in an :class:`~.AcqMethod`.
    """

    #: The ID of the device
    device_id: str = attr.ib(converter=str)

    #: The display name for the device.
    display_name: str = attr.ib(converter=str)

    rc_device: bool = attr.ib(converter=strtobool)
    """
    Flag to indicate the device is an RC Device.
    If :py:obj:`False` the device is an SCIC.
    """

    #: List of key: value mappings for configuration options.
    configuration: List[Dict[str, Any]] = attr.ib(converter=list, factory=list)

    #: Alternative form of ``configuration``.
    configuration2: Tuple[Dict[str, Any]] = attr.ib(
            converter=tuple,
            default=attr.Factory(tuple),
            )

    #: Alternative form of ``configuration``.
    configuration3: List[Dict[str, Any]] = attr.ib(
            converter=list,
            default=attr.Factory(list),
            metadata={"annotation": Sequence[Dict[str, Any]]},
            )

    #: Alternative form of ``configuration``.
    configuration4: List[Dict[str, Any]] = attrib(
            converter=list,
            factory=list,
            annotation=Sequence[Dict[str, Any]],
            )

    @overload
    def __getitem__(self, item: int) -> str: ...

    @overload
    def __getitem__(self, item: slice) -> List[str]: ...

    def __getitem__(self, item: Union[int, slice]) -> Union[str, List[str]]:
        """
        Return the item with the given index.

        :param item:

        :rtype:

        .. versionadded:: 1.2.3
        """


@attr.s(init=False)
class Connector:
    """
    Represents an electrical connector.

    :param name: The name of the connector.
    :param n_pins: The number if pins. For common connectors this is inferred from the name.
    :param right_angle: Whether this is a right angle connector.
    """

    #: The name of the connector
    name: str = attr.ib(converter=str)

    #: The number of pins
    n_pins: int = attr.ib(converter=int)

    def __init__(self, name: str, n_pins: Optional[int] = None, right_angle: bool = False):
        if name == "DA-15":
            n_pins = 15
        elif name == "DB-25":
            n_pins = 25
        elif name == "DE-15":
            n_pins = 15

        self.__attrs_init__(name, n_pins)

.. autoattrs:: demo.Device
    :autosummary:
class Device(device_id, display_name, rc_device, configuration=[], configuration2=(), configuration3=[], configuration4=[])[source]

Bases: object

Represents a device in an AcqMethod.

Parameters
  • device_id (str) – The ID of the device

  • display_name (str) – The display name for the device.

  • rc_device (Union[str, int]) – Flag to indicate the device is an RC Device. If False the device is an SCIC.

  • configuration (List[Dict[str, Any]]) – List of key: value mappings for configuration options. Default [].

  • configuration2 (Tuple[Dict[str, Any]]) – Alternative form of configuration. Default ().

  • configuration3 (Sequence[Dict[str, Any]]) – Alternative form of configuration. Default [].

  • configuration4 (Sequence[Dict[str, Any]]) – Alternative form of configuration. Default [].

Methods:

__eq__(​other)

Return self == other.

__ge__(​other)

Return self >= other.

__getitem__(​item)

Return the item with the given index.

__getstate__(​)

Used for pickling.

__gt__(​other)

Return self > other.

__le__(​other)

Return self <= other.

__lt__(​other)

Return self < other.

__ne__(​other)

Return self != other.

__repr__(​)

Return a string representation of the Device.

__setstate__(​state)

Used for pickling.

from_dict(​d)

Construct an instance of Device from a dictionary.

to_dict(​[convert_values])

Returns a dictionary containing the contents of the Device object.

Attributes:

configuration

List of key: value mappings for configuration options.

configuration2

Alternative form of configuration.

configuration3

Alternative form of configuration.

configuration4

Alternative form of configuration.

device_id

The ID of the device

display_name

The display name for the device.

rc_device

Flag to indicate the device is an RC Device.

__eq__(other)

Return self == other.

Return type

bool

__ge__(other)

Return self >= other.

Return type

bool

__getitem__(item)[source]

Return the item with the given index.

Parameters

item (Union[int, slice])

Return type

Union[str, List[str]]

Overloads

New in version 1.2.3.

__getstate__()

Used for pickling.

Automatically created by attrs.

__gt__(other)

Return self > other.

Return type

bool

__le__(other)

Return self <= other.

Return type

bool

__lt__(other)

Return self < other.

Return type

bool

__ne__(other)

Return self != other.

Return type

bool

__repr__()

Return a string representation of the Device.

Return type

str

__setstate__(state)

Used for pickling.

Automatically created by attrs.

configuration

Type:    List[Dict[str, Any]]

List of key: value mappings for configuration options.

configuration2

Type:    Tuple[Dict[str, Any]]

Alternative form of configuration.

configuration3

Type:    List[Dict[str, Any]]

Alternative form of configuration.

configuration4

Type:    List[Dict[str, Any]]

Alternative form of configuration.

device_id

Type:    str

The ID of the device

display_name

Type:    str

The display name for the device.

classmethod from_dict(d)

Construct an instance of Device from a dictionary.

Parameters

d (Mapping[str, Any]) – The dictionary.

rc_device

Type:    bool

Flag to indicate the device is an RC Device. If False the device is an SCIC.

to_dict(convert_values=False)

Returns a dictionary containing the contents of the Device object.

Parameters

convert_values (bool) – Recursively convert values into dictionaries, lists etc. as appropriate. Default False.

Return type

MutableMapping[str, Any]