File: slots.py

package info (click to toggle)
sphinx-automodapi 0.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 944 kB
  • sloc: python: 1,696; makefile: 197
file content (116 lines) | stat: -rw-r--r-- 3,417 bytes parent folder | download
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
104
105
106
107
108
109
110
111
112
113
114
115
116
"""Test classes containing __slots__

Instance attributes named in ``__slots__`` can be introspected and are listed
in the Attributes section of the class documentation. Class attributes are
listed in the same section of the generated docs so docstrings should be used
to distinguish class attributes vs instance attributes. Regular instance
attributes are dynamically inserted into ``__dict__`` and cannot be reliably
introspected so they're not included in the documentation.
"""
from __future__ import annotations

__all__ = ['SlotDict', 'DerivedParam', 'DerivedSlotParam',]


class SlotDict(object):
    """
    A class that uses __slots__ and __dict__ for its attribute namespace.
    """
    __slots__ = {
        "instance_attr": "instance attribute docstring can be added here",
        "__dict__": None,   # Allows additional instance attributes to be added
    }

    class_attr = "class attribute value"
    """(class attr) this is a class attribute."""

    def __init__(self, param: str, other_param: str):
        """
        Initializes a SlotDict object.

        Parameters
        ----------
        param : str
            A parameter
        other_param : str
            Another parameter
        """

        self.instance_attr = param
        """Instance attributes declared in slots can also define their docstring
        here
        """

        if other_param is not None:
            self.other_attr = other_param
            """This instance attribute is dynamic (not declared in a slot) so
            it's not included in the docs
            """

    def my_method(self):
        """
        Prints the SlotDict parameters.
        """
        print(f"instance_attr: {self.instance_attr}")
        print(f"other_attr: {self.other_attr}")


class DerivedParam(SlotDict):
    """
    Extends SlotDict by adding an extra parameter
    """
    def __init__(self, param: str, other_param: str, extra_param: str):
        """
        Initializes a DerivedParam object.

        Parameters
        ----------
        param : str
            A parameter
        other_param : str
            Another parameter
        extra_param : str
            An extra parameter
        """
        super(DerivedParam, self).__init__(param, other_param)
        self.extra_attr = extra_param

    def derived_from_slot_class_method(self):
        """
        Prints the DerivedParam parameters.
        """
        print(f"instance_attr: {self.instance_attr}")
        print(f"other_attr: {self.other_attr}")
        print(f"extra_attr: {self.extra_attr}")


class DerivedSlotParam(SlotDict):
    """
    Extends SlotDict by adding a slot parameter
    """

    __slots__ = ('extra_attr',)

    def __init__(self, param: str, other_param: str, extra_param: str):
        """
        Initializes a DerivedSlotParam object.

        Parameters
        ----------
        param : str
            A parameter
        other_param : str
            Another parameter
        extra_param : str
            An extra parameter
        """
        super(DerivedSlotParam, self).__init__(param, other_param)
        self.extra_attr = extra_param

    def derived_from_slot_class_method(self):
        """
        Prints the DerivedSlotParam parameters.
        """
        print(f"instance_attr: {self.instance_attr}")
        print(f"other_attr: {self.other_attr}")
        print(f"extra_attr: {self.extra_attr}")