File: scalars.py

package info (click to toggle)
python-atom 0.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,616 kB
  • sloc: cpp: 9,040; python: 6,249; makefile: 123
file content (211 lines) | stat: -rw-r--r-- 6,594 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
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
# --------------------------------------------------------------------------------------
# Copyright (c) 2013-2025, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
# --------------------------------------------------------------------------------------
from .catom import DefaultValue, DelAttr, GetState, Member, SetAttr, Validate
from .typing_utils import extract_types


class Value(Member):
    """A member class which supports value initialization.

    A plain `Value` provides support for default values and factories,
    but does not perform any type checking or validation. It serves as
    a useful base class for scalar members and can be used for cases
    where type checking is not needed (like private attributes).

    """

    __slots__ = ()

    def __init__(self, default=None, *, factory=None):
        """Initialize a Value.

        Parameters
        ----------
        default : object, optional
            The default value for the member. If this is provided, it
            should be an immutable value. The value will will not be
            copied between owner instances.

        factory : callable, optional
            A callable object which is called with zero arguments and
            returns a default value for the member. This will override
            any value given by `default`.

        """
        if factory is not None:
            self.set_default_value_mode(DefaultValue.CallObject, factory)
        else:
            self.set_default_value_mode(DefaultValue.Static, default)


class ReadOnly(Value):
    """A value which can be assigned once and is then read-only."""

    __slots__ = ()

    def __init__(self, kind=None, *, default=None, factory=None):
        super(ReadOnly, self).__init__(default, factory=factory)
        self.set_setattr_mode(SetAttr.ReadOnly, None)
        self.set_delattr_mode(DelAttr.ReadOnly, None)
        self.set_getstate_mode(GetState.IncludeNonDefault, None)
        if kind:
            self.set_validate_mode(Validate.Instance, extract_types(kind))


class Constant(Value):
    """A value which cannot be changed from its default."""

    __slots__ = ()

    def __init__(self, default=None, *, factory=None, kind=None):
        super(Constant, self).__init__(default, factory=factory)
        self.set_setattr_mode(SetAttr.Constant, None)
        self.set_delattr_mode(DelAttr.Constant, None)
        self.set_getstate_mode(GetState.Exclude, None)
        if kind:
            self.set_validate_mode(Validate.Instance, extract_types(kind))


class Callable(Value):
    """A value which is callable."""

    __slots__ = ()

    def __init__(self, default=None, *, factory=None):
        super(Callable, self).__init__(default, factory=factory)
        self.set_validate_mode(Validate.Callable, None)


class Bool(Value):
    """A value of type `bool`."""

    __slots__ = ()

    def __init__(self, default=False, *, factory=None):
        super(Bool, self).__init__(default, factory=factory)
        self.set_validate_mode(Validate.Bool, None)


class Int(Value):
    """A value of type `int`.

    By default, ints are strictly typed.  Pass strict=False to the
    constructor to enable int casting for longs and floats.

    """

    __slots__ = ()

    def __init__(self, default=0, *, factory=None, strict=True):
        super(Int, self).__init__(default, factory=factory)
        if strict:
            self.set_validate_mode(Validate.Int, None)
        else:
            self.set_validate_mode(Validate.IntPromote, None)


class FloatRange(Value):
    """A float value clipped to a range.

    By default, ints and longs will be promoted to floats. Pass
    strict=True to the constructor to enable strict float checking.

    """

    __slots__ = ()

    def __init__(self, low=None, high=None, value=None, *, strict=False):
        if low is not None and high is not None and low > high:
            low, high = high, low
        default = 0.0
        if value is not None:
            default = value
        elif low is not None:
            default = low
        elif high is not None:
            default = high
        super(FloatRange, self).__init__(default)
        if strict:
            self.set_validate_mode(Validate.FloatRange, (low, high))
        else:
            if low is not None:
                low = float(low)
            if high is not None:
                high = float(high)
            self.set_validate_mode(Validate.FloatRangePromote, (low, high))


class Range(Value):
    """An integer value clipped to a range."""

    __slots__ = ()

    def __init__(self, low=None, high=None, value=None):
        if low is not None and high is not None and low > high:
            low, high = high, low
        default = 0
        if value is not None:
            default = value
        elif low is not None:
            default = low
        elif high is not None:
            default = high
        super(Range, self).__init__(default)
        self.set_validate_mode(Validate.Range, (low, high))


class Float(Value):
    """A value of type `float`.

    By default, ints and longs will be promoted to floats. Pass
    strict=True to the constructor to enable strict float checking.

    """

    __slots__ = ()

    def __init__(self, default=0.0, *, factory=None, strict=False):
        super(Float, self).__init__(default, factory=factory)
        if strict:
            self.set_validate_mode(Validate.Float, None)
        else:
            self.set_validate_mode(Validate.FloatPromote, None)


class Bytes(Value):
    """A value of type `bytes`.

    By default, strings will NOT be promoted to bytes. Pass strict=False to the
    constructor to enable loose byte checking.

    """

    __slots__ = ()

    def __init__(self, default=b"", *, factory=None, strict=True):
        super(Bytes, self).__init__(default, factory=factory)
        if strict:
            self.set_validate_mode(Validate.Bytes, None)
        else:
            self.set_validate_mode(Validate.BytesPromote, None)


class Str(Value):
    """A value of type `str`.

    By default, bytes will NOT be promoted to strings. Pass strict=False to the
    constructor to enable loose string checking.

    """

    def __init__(self, default="", *, factory=None, strict=True):
        super(Str, self).__init__(default, factory=factory)
        if strict:
            self.set_validate_mode(Validate.Str, None)
        else:
            self.set_validate_mode(Validate.StrPromote, None)