Skip to content

base_classes

EasyDynamicsBase

Bases: NameMixin, NewBase

Base class for all EasyDynamics classes.

Source code in src/easydynamics/base_classes/easydynamics_base.py
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
class EasyDynamicsBase(NameMixin, NewBase):
    """Base class for all EasyDynamics classes."""

    def __init__(
        self,
        *args: object,
        name: str = 'MyEasyDynamicsModel',
        display_name: str | None = None,
        unique_name: str | None = None,
        **kwargs: object,
    ) -> None:
        """
        Initialize the EasyDynamicsBase.

        Parameters
        ----------
        *args : object
            Positional arguments to pass to the parent class.
        name : str, default='MyEasyDynamicsModel'
            Name of the model.
        display_name : str | None, default=None
            Display name of the model. If None, the name will be used.
        unique_name : str | None, default=None
            Unique name of the model. If None, a unique name will be generated.
        **kwargs : object
             Additional keyword arguments to pass to the parent class.

        Raises
        ------
        TypeError
            If name is not a string.
        """

        if not isinstance(name, str):
            raise TypeError(f'Name must be a string, got {type(name)}')

        if display_name is None:
            display_name = name

        super().__init__(
            *args,
            name=name,
            display_name=display_name,
            unique_name=unique_name,
            **kwargs,
        )

__init__(*args, name='MyEasyDynamicsModel', display_name=None, unique_name=None, **kwargs)

Initialize the EasyDynamicsBase.

Parameters:

Name Type Description Default
*args object

Positional arguments to pass to the parent class.

()
name str

Name of the model.

'MyEasyDynamicsModel'
display_name str | None

Display name of the model. If None, the name will be used.

None
unique_name str | None

Unique name of the model. If None, a unique name will be generated.

None
**kwargs object

Additional keyword arguments to pass to the parent class.

{}

Raises:

Type Description
TypeError

If name is not a string.

Source code in src/easydynamics/base_classes/easydynamics_base.py
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
def __init__(
    self,
    *args: object,
    name: str = 'MyEasyDynamicsModel',
    display_name: str | None = None,
    unique_name: str | None = None,
    **kwargs: object,
) -> None:
    """
    Initialize the EasyDynamicsBase.

    Parameters
    ----------
    *args : object
        Positional arguments to pass to the parent class.
    name : str, default='MyEasyDynamicsModel'
        Name of the model.
    display_name : str | None, default=None
        Display name of the model. If None, the name will be used.
    unique_name : str | None, default=None
        Unique name of the model. If None, a unique name will be generated.
    **kwargs : object
         Additional keyword arguments to pass to the parent class.

    Raises
    ------
    TypeError
        If name is not a string.
    """

    if not isinstance(name, str):
        raise TypeError(f'Name must be a string, got {type(name)}')

    if display_name is None:
        display_name = name

    super().__init__(
        *args,
        name=name,
        display_name=display_name,
        unique_name=unique_name,
        **kwargs,
    )

EasyDynamicsModelBase

Bases: NameMixin, ModelBase

Base class for all EasyDynamics models.

Source code in src/easydynamics/base_classes/easydynamics_modelbase.py
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
class EasyDynamicsModelBase(NameMixin, ModelBase):
    """Base class for all EasyDynamics models."""

    def __init__(
        self,
        *args: object,
        unit: str | sc.Unit = 'meV',
        name: str = 'MyEasyDynamicsModel',
        display_name: str | None = None,
        unique_name: str | None = None,
        **kwargs: object,
    ) -> None:
        """
        Initialize the EasyDynamicsModelBase.

        Parameters
        ----------
        *args : object
            Positional arguments to pass to the parent class.
        unit : str | sc.Unit, default='meV'
            Unit of the model.
        name : str, default='MyEasyDynamicsModel'
            Name of the model.
        display_name : str | None, default=None
            Display name of the model. If None, the name will be used.
        unique_name : str | None, default=None
            Unique name of the model. If None, a unique name will be generated.
        **kwargs : object
             Additional keyword arguments to pass to the parent class.

        Raises
        ------
        TypeError
            If name is not a string.
        """

        if not isinstance(name, str):
            raise TypeError(f'Name must be a string, got {type(name)}')

        if display_name is None:
            display_name = name

        super().__init__(
            *args,
            name=name,
            display_name=display_name,
            unique_name=unique_name,
            **kwargs,
        )

        self._unit = _validate_unit(unit)

    @property
    def unit(self) -> str | sc.Unit | None:
        """
        Get the unit of the model.

        Returns
        -------
        str | sc.Unit | None
             The unit of the model.
        """

        return self._unit

    @unit.setter
    def unit(self, _unit_str: str) -> None:
        """
        Unit is read-only and cannot be set directly.

        Parameters
        ----------
        _unit_str : str
            The new unit to set (ignored).

        Raises
        ------
        AttributeError
            Always raised to indicate that the unit is read-only.
        """
        raise AttributeError(
            f'Unit is read-only. Use convert_unit to change the unit between allowed types '
            f'or create a new {self.__class__.__name__} with the desired unit.'
        )

unit property writable

Get the unit of the model.

Returns:

Type Description
str | sc.Unit | None

The unit of the model.

__init__(*args, unit='meV', name='MyEasyDynamicsModel', display_name=None, unique_name=None, **kwargs)

Initialize the EasyDynamicsModelBase.

Parameters:

Name Type Description Default
*args object

Positional arguments to pass to the parent class.

()
unit str | sc.Unit

Unit of the model.

'meV'
name str

Name of the model.

'MyEasyDynamicsModel'
display_name str | None

Display name of the model. If None, the name will be used.

None
unique_name str | None

Unique name of the model. If None, a unique name will be generated.

None
**kwargs object

Additional keyword arguments to pass to the parent class.

{}

Raises:

Type Description
TypeError

If name is not a string.

Source code in src/easydynamics/base_classes/easydynamics_modelbase.py
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
def __init__(
    self,
    *args: object,
    unit: str | sc.Unit = 'meV',
    name: str = 'MyEasyDynamicsModel',
    display_name: str | None = None,
    unique_name: str | None = None,
    **kwargs: object,
) -> None:
    """
    Initialize the EasyDynamicsModelBase.

    Parameters
    ----------
    *args : object
        Positional arguments to pass to the parent class.
    unit : str | sc.Unit, default='meV'
        Unit of the model.
    name : str, default='MyEasyDynamicsModel'
        Name of the model.
    display_name : str | None, default=None
        Display name of the model. If None, the name will be used.
    unique_name : str | None, default=None
        Unique name of the model. If None, a unique name will be generated.
    **kwargs : object
         Additional keyword arguments to pass to the parent class.

    Raises
    ------
    TypeError
        If name is not a string.
    """

    if not isinstance(name, str):
        raise TypeError(f'Name must be a string, got {type(name)}')

    if display_name is None:
        display_name = name

    super().__init__(
        *args,
        name=name,
        display_name=display_name,
        unique_name=unique_name,
        **kwargs,
    )

    self._unit = _validate_unit(unit)

easydynamics_base

EasyDynamicsBase

Bases: NameMixin, NewBase

Base class for all EasyDynamics classes.

Source code in src/easydynamics/base_classes/easydynamics_base.py
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
class EasyDynamicsBase(NameMixin, NewBase):
    """Base class for all EasyDynamics classes."""

    def __init__(
        self,
        *args: object,
        name: str = 'MyEasyDynamicsModel',
        display_name: str | None = None,
        unique_name: str | None = None,
        **kwargs: object,
    ) -> None:
        """
        Initialize the EasyDynamicsBase.

        Parameters
        ----------
        *args : object
            Positional arguments to pass to the parent class.
        name : str, default='MyEasyDynamicsModel'
            Name of the model.
        display_name : str | None, default=None
            Display name of the model. If None, the name will be used.
        unique_name : str | None, default=None
            Unique name of the model. If None, a unique name will be generated.
        **kwargs : object
             Additional keyword arguments to pass to the parent class.

        Raises
        ------
        TypeError
            If name is not a string.
        """

        if not isinstance(name, str):
            raise TypeError(f'Name must be a string, got {type(name)}')

        if display_name is None:
            display_name = name

        super().__init__(
            *args,
            name=name,
            display_name=display_name,
            unique_name=unique_name,
            **kwargs,
        )

__init__(*args, name='MyEasyDynamicsModel', display_name=None, unique_name=None, **kwargs)

Initialize the EasyDynamicsBase.

Parameters:

Name Type Description Default
*args object

Positional arguments to pass to the parent class.

()
name str

Name of the model.

'MyEasyDynamicsModel'
display_name str | None

Display name of the model. If None, the name will be used.

None
unique_name str | None

Unique name of the model. If None, a unique name will be generated.

None
**kwargs object

Additional keyword arguments to pass to the parent class.

{}

Raises:

Type Description
TypeError

If name is not a string.

Source code in src/easydynamics/base_classes/easydynamics_base.py
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
def __init__(
    self,
    *args: object,
    name: str = 'MyEasyDynamicsModel',
    display_name: str | None = None,
    unique_name: str | None = None,
    **kwargs: object,
) -> None:
    """
    Initialize the EasyDynamicsBase.

    Parameters
    ----------
    *args : object
        Positional arguments to pass to the parent class.
    name : str, default='MyEasyDynamicsModel'
        Name of the model.
    display_name : str | None, default=None
        Display name of the model. If None, the name will be used.
    unique_name : str | None, default=None
        Unique name of the model. If None, a unique name will be generated.
    **kwargs : object
         Additional keyword arguments to pass to the parent class.

    Raises
    ------
    TypeError
        If name is not a string.
    """

    if not isinstance(name, str):
        raise TypeError(f'Name must be a string, got {type(name)}')

    if display_name is None:
        display_name = name

    super().__init__(
        *args,
        name=name,
        display_name=display_name,
        unique_name=unique_name,
        **kwargs,
    )

easydynamics_list

EasyDynamicsList

Bases: EasyList[ProtectedType_]

Base class for all EasyDynamics lists.

Source code in src/easydynamics/base_classes/easydynamics_list.py
 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
class EasyDynamicsList(EasyList[ProtectedType_]):
    """Base class for all EasyDynamics lists."""

    def __init__(
        self,
        *args: ProtectedType_ | list[ProtectedType_],
        protected_types: (list[type[ProtectedType_]] | type[ProtectedType_] | None) = None,
        display_name: str | None = None,
        unique_name: str | None = None,
        **kwargs: object,
    ) -> None:
        """
        Initialize the EasyDynamicsList.

        Parameters
        ----------
        *args : ProtectedType_ | list[ProtectedType_]
            Initial items to add to the list. Can be a single item or a list of items. Each item
            must be an instance of one of the protected types.
        protected_types : list[type[ProtectedType_]] | type[ProtectedType_] | None, default=None
            Types that are allowed in the list. Can be a single EasyDynamicsBase or
            EasyDynamicsModelBase subclass or a list of them. If None, defaults to
            [EasyDynamicsBase].
        display_name : str | None, default=None
            Display name of the list. If None, the name will be used.
        unique_name : str | None, default=None
            Unique name of the list. If None, a unique name will be generated.
        **kwargs : object
            Additional keyword arguments to pass to the EasyList constructor.
        """

        if display_name is None:
            display_name = unique_name

        super().__init__(
            *args,
            protected_types=protected_types,
            display_name=display_name,
            unique_name=unique_name,
            **kwargs,
        )

    # ------------------------------------------------------------------
    # List methods
    # ------------------------------------------------------------------

    def insert(self, index: int, value: ProtectedType_) -> None:
        """
        Insert an item into the list at a specific index.

        Parameters
        ----------
        index : int
            The index at which to insert the item.
        value : ProtectedType_
            The item to insert. Must be an instance of one of the protected types.
        """

        # Overwritten to update warning
        self._validate_type(value)
        if value in self:
            warnings.warn(
                (
                    f'Item with name "{self._get_key(value)}" already '
                    f'in EasyDynamicsList, it will be ignored'
                ),
                UserWarning,
                stacklevel=2,
            )
            return

        super().insert(index, value)

    def append(self, value: ProtectedType_) -> None:
        """
        Append an item to the end of the list.
        Parameters
        ----------
        value : ProtectedType_
            The item to append. Must be an instance of one of the protected types.
        """
        self._validate_type(value)

        # append calls insert which checks for duplicates
        super().append(value)

    def pop(self, index: int | str = -1) -> ProtectedType_:
        """
        Remove and return an item at a specific index or name.

        Parameters
        ----------
        index : int | str, default=-1
            The index or name at which to pop the item.

        Returns
        -------
        ProtectedType_
            The item that was popped.

        Raises
        ------
        TypeError
            If index is not an int or str.
        KeyError
            If index is a str and no item with that name is found.
        """

        # Overwritten to update warning
        if isinstance(index, int):
            return self._data.pop(index)
        if isinstance(index, str):
            for i, item in enumerate(self._data):
                if self._get_key(item) == index:
                    return self._data.pop(i)
            raise KeyError(f'No item with name "{index}" found')
        raise TypeError('Index must be an int or str')

    # ------------------------------------------------------------------
    # Other methods
    # ------------------------------------------------------------------

    def get_names(self) -> list[str]:
        """
        Get a list of the names of all items in the list.

        Returns
        -------
        list[str]
            A list of the names of all items in the list.
        """
        return [item.name for item in self._data]

    def get_duplicate_names(self) -> list[str]:
        """
        Get a list of duplicate names in the list.

        Returns
        -------
        list[str]
            A list of duplicate names in the list.
        """
        seen = set()
        duplicates = set()
        for item in self._data:
            name = item.name
            if name in seen:
                duplicates.add(name)
            else:
                seen.add(name)
        return list(duplicates)

    # ------------------------------------------------------------------
    # Private methods
    # ------------------------------------------------------------------

    def _get_key(self, obj: EasyDynamicsBase | EasyDynamicsModelBase) -> str:
        """
        Get the name of an object.

        Parameters
        ----------
        obj : EasyDynamicsBase | EasyDynamicsModelBase
            Object to get the key for.

        Returns
        -------
        str
            The name of the object.
        """
        return obj.name

    def _validate_type(self, value: object) -> None:
        """
        Validate that a value is an instance of one of the protected types.

        Parameters
        ----------
        value : object
            The value to validate.

        Raises
        ------
        TypeError
             If the value is not an instance of one of the protected types.
        """

        if not isinstance(value, tuple(self._protected_types)):
            allowed = ', '.join(t.__name__ for t in self._protected_types)
            raise TypeError(
                f'Value must be an instance of type: {allowed}. Got {type(value).__name__} instead.'  # noqa: E501
            )

    # ------------------------------------------------------------------
    # dunder methods
    # ------------------------------------------------------------------
    def __getitem__(
        self, idx: int | slice | str
    ) -> ProtectedType_ | EasyDynamicsList[ProtectedType_]:
        """
        Get an item by index, slice, or unique_name.

        Parameters
        ----------
        idx : int | slice | str
                Index, slice, or name of the item to get.

        Returns
        -------
        ProtectedType_ | EasyDynamicsList[ProtectedType_]
             The item at the specified index or name, or a new EasyDynamicsList if a slice is
             provided.

        Raises
        ------
        TypeError
            If idx is not an int, slice, or str.
        KeyError
            If idx is a str and no item with that name is found.
        AmbiguousNameError
            If idx is a str and multiple items with that name are found.
        """
        if isinstance(idx, int):
            return self._data[idx]
        if isinstance(idx, slice):
            return self.__class__(self._data[idx], protected_types=self._protected_types)
        if isinstance(idx, str):
            matches = [r for r in self._data if self._get_key(r) == idx]
            if len(matches) == 1:
                return matches[0]
            if len(matches) > 1:
                raise AmbiguousNameError(idx, matches)

            raise KeyError(f'No item with name "{idx}" found')
        raise TypeError('Index must be an int, slice, or str')

__init__(*args, protected_types=None, display_name=None, unique_name=None, **kwargs)

Initialize the EasyDynamicsList.

Parameters:

Name Type Description Default
*args ProtectedType_ | list[ProtectedType_]

Initial items to add to the list. Can be a single item or a list of items. Each item must be an instance of one of the protected types.

()
protected_types list[type[ProtectedType_]] | type[ProtectedType_] | None

Types that are allowed in the list. Can be a single EasyDynamicsBase or EasyDynamicsModelBase subclass or a list of them. If None, defaults to [EasyDynamicsBase].

None
display_name str | None

Display name of the list. If None, the name will be used.

None
unique_name str | None

Unique name of the list. If None, a unique name will be generated.

None
**kwargs object

Additional keyword arguments to pass to the EasyList constructor.

{}
Source code in src/easydynamics/base_classes/easydynamics_list.py
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
def __init__(
    self,
    *args: ProtectedType_ | list[ProtectedType_],
    protected_types: (list[type[ProtectedType_]] | type[ProtectedType_] | None) = None,
    display_name: str | None = None,
    unique_name: str | None = None,
    **kwargs: object,
) -> None:
    """
    Initialize the EasyDynamicsList.

    Parameters
    ----------
    *args : ProtectedType_ | list[ProtectedType_]
        Initial items to add to the list. Can be a single item or a list of items. Each item
        must be an instance of one of the protected types.
    protected_types : list[type[ProtectedType_]] | type[ProtectedType_] | None, default=None
        Types that are allowed in the list. Can be a single EasyDynamicsBase or
        EasyDynamicsModelBase subclass or a list of them. If None, defaults to
        [EasyDynamicsBase].
    display_name : str | None, default=None
        Display name of the list. If None, the name will be used.
    unique_name : str | None, default=None
        Unique name of the list. If None, a unique name will be generated.
    **kwargs : object
        Additional keyword arguments to pass to the EasyList constructor.
    """

    if display_name is None:
        display_name = unique_name

    super().__init__(
        *args,
        protected_types=protected_types,
        display_name=display_name,
        unique_name=unique_name,
        **kwargs,
    )

insert(index, value)

Insert an item into the list at a specific index.

Parameters:

Name Type Description Default
index int

The index at which to insert the item.

required
value ProtectedType_

The item to insert. Must be an instance of one of the protected types.

required
Source code in src/easydynamics/base_classes/easydynamics_list.py
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
def insert(self, index: int, value: ProtectedType_) -> None:
    """
    Insert an item into the list at a specific index.

    Parameters
    ----------
    index : int
        The index at which to insert the item.
    value : ProtectedType_
        The item to insert. Must be an instance of one of the protected types.
    """

    # Overwritten to update warning
    self._validate_type(value)
    if value in self:
        warnings.warn(
            (
                f'Item with name "{self._get_key(value)}" already '
                f'in EasyDynamicsList, it will be ignored'
            ),
            UserWarning,
            stacklevel=2,
        )
        return

    super().insert(index, value)

append(value)

Append an item to the end of the list.

Parameters:

Name Type Description Default
value ProtectedType_

The item to append. Must be an instance of one of the protected types.

required
Source code in src/easydynamics/base_classes/easydynamics_list.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def append(self, value: ProtectedType_) -> None:
    """
    Append an item to the end of the list.
    Parameters
    ----------
    value : ProtectedType_
        The item to append. Must be an instance of one of the protected types.
    """
    self._validate_type(value)

    # append calls insert which checks for duplicates
    super().append(value)

pop(index=-1)

Remove and return an item at a specific index or name.

Parameters:

Name Type Description Default
index int | str

The index or name at which to pop the item.

-1

Returns:

Type Description
ProtectedType_

The item that was popped.

Raises:

Type Description
TypeError

If index is not an int or str.

KeyError

If index is a str and no item with that name is found.

Source code in src/easydynamics/base_classes/easydynamics_list.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def pop(self, index: int | str = -1) -> ProtectedType_:
    """
    Remove and return an item at a specific index or name.

    Parameters
    ----------
    index : int | str, default=-1
        The index or name at which to pop the item.

    Returns
    -------
    ProtectedType_
        The item that was popped.

    Raises
    ------
    TypeError
        If index is not an int or str.
    KeyError
        If index is a str and no item with that name is found.
    """

    # Overwritten to update warning
    if isinstance(index, int):
        return self._data.pop(index)
    if isinstance(index, str):
        for i, item in enumerate(self._data):
            if self._get_key(item) == index:
                return self._data.pop(i)
        raise KeyError(f'No item with name "{index}" found')
    raise TypeError('Index must be an int or str')

get_names()

Get a list of the names of all items in the list.

Returns:

Type Description
list[str]

A list of the names of all items in the list.

Source code in src/easydynamics/base_classes/easydynamics_list.py
140
141
142
143
144
145
146
147
148
149
def get_names(self) -> list[str]:
    """
    Get a list of the names of all items in the list.

    Returns
    -------
    list[str]
        A list of the names of all items in the list.
    """
    return [item.name for item in self._data]

get_duplicate_names()

Get a list of duplicate names in the list.

Returns:

Type Description
list[str]

A list of duplicate names in the list.

Source code in src/easydynamics/base_classes/easydynamics_list.py
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
def get_duplicate_names(self) -> list[str]:
    """
    Get a list of duplicate names in the list.

    Returns
    -------
    list[str]
        A list of duplicate names in the list.
    """
    seen = set()
    duplicates = set()
    for item in self._data:
        name = item.name
        if name in seen:
            duplicates.add(name)
        else:
            seen.add(name)
    return list(duplicates)

__getitem__(idx)

Get an item by index, slice, or unique_name.

Parameters:

Name Type Description Default
idx int | slice | str
Index, slice, or name of the item to get.
required

Returns:

Type Description
ProtectedType_ | EasyDynamicsList[ProtectedType_]

The item at the specified index or name, or a new EasyDynamicsList if a slice is provided.

Raises:

Type Description
TypeError

If idx is not an int, slice, or str.

KeyError

If idx is a str and no item with that name is found.

AmbiguousNameError

If idx is a str and multiple items with that name are found.

Source code in src/easydynamics/base_classes/easydynamics_list.py
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
def __getitem__(
    self, idx: int | slice | str
) -> ProtectedType_ | EasyDynamicsList[ProtectedType_]:
    """
    Get an item by index, slice, or unique_name.

    Parameters
    ----------
    idx : int | slice | str
            Index, slice, or name of the item to get.

    Returns
    -------
    ProtectedType_ | EasyDynamicsList[ProtectedType_]
         The item at the specified index or name, or a new EasyDynamicsList if a slice is
         provided.

    Raises
    ------
    TypeError
        If idx is not an int, slice, or str.
    KeyError
        If idx is a str and no item with that name is found.
    AmbiguousNameError
        If idx is a str and multiple items with that name are found.
    """
    if isinstance(idx, int):
        return self._data[idx]
    if isinstance(idx, slice):
        return self.__class__(self._data[idx], protected_types=self._protected_types)
    if isinstance(idx, str):
        matches = [r for r in self._data if self._get_key(r) == idx]
        if len(matches) == 1:
            return matches[0]
        if len(matches) > 1:
            raise AmbiguousNameError(idx, matches)

        raise KeyError(f'No item with name "{idx}" found')
    raise TypeError('Index must be an int, slice, or str')

easydynamics_modelbase

EasyDynamicsModelBase

Bases: NameMixin, ModelBase

Base class for all EasyDynamics models.

Source code in src/easydynamics/base_classes/easydynamics_modelbase.py
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
class EasyDynamicsModelBase(NameMixin, ModelBase):
    """Base class for all EasyDynamics models."""

    def __init__(
        self,
        *args: object,
        unit: str | sc.Unit = 'meV',
        name: str = 'MyEasyDynamicsModel',
        display_name: str | None = None,
        unique_name: str | None = None,
        **kwargs: object,
    ) -> None:
        """
        Initialize the EasyDynamicsModelBase.

        Parameters
        ----------
        *args : object
            Positional arguments to pass to the parent class.
        unit : str | sc.Unit, default='meV'
            Unit of the model.
        name : str, default='MyEasyDynamicsModel'
            Name of the model.
        display_name : str | None, default=None
            Display name of the model. If None, the name will be used.
        unique_name : str | None, default=None
            Unique name of the model. If None, a unique name will be generated.
        **kwargs : object
             Additional keyword arguments to pass to the parent class.

        Raises
        ------
        TypeError
            If name is not a string.
        """

        if not isinstance(name, str):
            raise TypeError(f'Name must be a string, got {type(name)}')

        if display_name is None:
            display_name = name

        super().__init__(
            *args,
            name=name,
            display_name=display_name,
            unique_name=unique_name,
            **kwargs,
        )

        self._unit = _validate_unit(unit)

    @property
    def unit(self) -> str | sc.Unit | None:
        """
        Get the unit of the model.

        Returns
        -------
        str | sc.Unit | None
             The unit of the model.
        """

        return self._unit

    @unit.setter
    def unit(self, _unit_str: str) -> None:
        """
        Unit is read-only and cannot be set directly.

        Parameters
        ----------
        _unit_str : str
            The new unit to set (ignored).

        Raises
        ------
        AttributeError
            Always raised to indicate that the unit is read-only.
        """
        raise AttributeError(
            f'Unit is read-only. Use convert_unit to change the unit between allowed types '
            f'or create a new {self.__class__.__name__} with the desired unit.'
        )

unit property writable

Get the unit of the model.

Returns:

Type Description
str | sc.Unit | None

The unit of the model.

__init__(*args, unit='meV', name='MyEasyDynamicsModel', display_name=None, unique_name=None, **kwargs)

Initialize the EasyDynamicsModelBase.

Parameters:

Name Type Description Default
*args object

Positional arguments to pass to the parent class.

()
unit str | sc.Unit

Unit of the model.

'meV'
name str

Name of the model.

'MyEasyDynamicsModel'
display_name str | None

Display name of the model. If None, the name will be used.

None
unique_name str | None

Unique name of the model. If None, a unique name will be generated.

None
**kwargs object

Additional keyword arguments to pass to the parent class.

{}

Raises:

Type Description
TypeError

If name is not a string.

Source code in src/easydynamics/base_classes/easydynamics_modelbase.py
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
def __init__(
    self,
    *args: object,
    unit: str | sc.Unit = 'meV',
    name: str = 'MyEasyDynamicsModel',
    display_name: str | None = None,
    unique_name: str | None = None,
    **kwargs: object,
) -> None:
    """
    Initialize the EasyDynamicsModelBase.

    Parameters
    ----------
    *args : object
        Positional arguments to pass to the parent class.
    unit : str | sc.Unit, default='meV'
        Unit of the model.
    name : str, default='MyEasyDynamicsModel'
        Name of the model.
    display_name : str | None, default=None
        Display name of the model. If None, the name will be used.
    unique_name : str | None, default=None
        Unique name of the model. If None, a unique name will be generated.
    **kwargs : object
         Additional keyword arguments to pass to the parent class.

    Raises
    ------
    TypeError
        If name is not a string.
    """

    if not isinstance(name, str):
        raise TypeError(f'Name must be a string, got {type(name)}')

    if display_name is None:
        display_name = name

    super().__init__(
        *args,
        name=name,
        display_name=display_name,
        unique_name=unique_name,
        **kwargs,
    )

    self._unit = _validate_unit(unit)

name_mixin

NameMixin

Mixin class to add name functionality to EasyDynamics classes.

Source code in src/easydynamics/base_classes/name_mixin.py
 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
class NameMixin:
    """Mixin class to add name functionality to EasyDynamics classes."""

    def __init__(
        self,
        *args: object,
        name: str = 'MyEasyDynamicsModel',
        **kwargs: object,
    ) -> None:
        """
        Initialize the NameMixin.

        Parameters
        ----------
        *args : object
            Positional arguments to pass to the parent class.
        name : str, default='MyEasyDynamicsModel'
            Name of the model.
        **kwargs : object
            Keyword arguments to pass to the parent class.

        Raises
        ------
        TypeError
            If name is not a string.
        """

        super().__init__(*args, **kwargs)
        if not isinstance(name, str):
            raise TypeError('Name must be a string.')
        self._name = name

    @property
    def name(self) -> str:
        """
        Get the name of the model.

        Returns
        -------
        str
            The name of the model.
        """
        return self._name

    @name.setter
    def name(self, name_str: str) -> None:
        """
        Set the name of the model.

        Parameters
        ----------
        name_str : str
            The new name to set.

        Raises
        ------
        TypeError
            If name_str is not a string.
        """

        if not isinstance(name_str, str):
            raise TypeError('Name must be a string.')
        self._name = name_str

name property writable

Get the name of the model.

Returns:

Type Description
str

The name of the model.

__init__(*args, name='MyEasyDynamicsModel', **kwargs)

Initialize the NameMixin.

Parameters:

Name Type Description Default
*args object

Positional arguments to pass to the parent class.

()
name str

Name of the model.

'MyEasyDynamicsModel'
**kwargs object

Keyword arguments to pass to the parent class.

{}

Raises:

Type Description
TypeError

If name is not a string.

Source code in src/easydynamics/base_classes/name_mixin.py
 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
def __init__(
    self,
    *args: object,
    name: str = 'MyEasyDynamicsModel',
    **kwargs: object,
) -> None:
    """
    Initialize the NameMixin.

    Parameters
    ----------
    *args : object
        Positional arguments to pass to the parent class.
    name : str, default='MyEasyDynamicsModel'
        Name of the model.
    **kwargs : object
        Keyword arguments to pass to the parent class.

    Raises
    ------
    TypeError
        If name is not a string.
    """

    super().__init__(*args, **kwargs)
    if not isinstance(name, str):
        raise TypeError('Name must be a string.')
    self._name = name