tkmilan.validation#
Validation objects for Spec variables.
All validation representations should be defined here, as standalone objects
that can be tested directly, without Tk dependencies.
tkmilan.var includes the Tk interfaces.
Module Attributes
Mapping between inclusion boolean, and boundary representation. |
|
String representation of infinite boundary. |
|
Metadata for |
|
Generic Type for |
|
Generic Type-Checking variable type for |
Functions
|
Turn a list of labels into a mapping, by applying a function to get the value. |
|
Turn a list of values into a mapping, by applying a function to get the label. |
Classes
|
Represent a bounded range to limit a value, for integers ( |
|
Represent a potentially unbounded range to limit a value, generic for any |
|
Represent a static list of labels, therefore a |
|
Represent a static mapping between labels and its corresponding values. |
|
Abstract parent class for all validation representations. |
- class tkmilan.validation.LimitBounded(min_entry: dataclasses.InitVar[Union[int, str]], max_entry: dataclasses.InitVar[Union[int, str]], fn: Callable[[str], int | None], step: int = 1, imin: bool = True, imax: bool = True, default: dataclasses.InitVar[Union[int, str, NoneType]] = None)#
Bases:
Mapping[str,int],VSpecRepresent a bounded range to limit a value, for integers (
int).The range can be made to remove the limits themselves with
iminandimax. The default is the limit being closed on both sides.The range is bounded, there are no infinite limits. The
stepparameter controls if all intermediate values are valid.The following arguments are not available on the final object:
- Parameters:
min_entry – Minimum value, as string or number.
max_entry – Maximum value, as string or number.
default – Default value to be set, as string, number, or
Noneto choose a valid value (the default). WhenNone, a sane value is chosen. If bothiminandimaxasFalse,0is chosen, which might not be included on the range. Configures the default label.
The following parameters are available on the final object:
- Parameters:
fn – A function that turns a string into
number. Usually some kind of parser.step – The step amount for the range of values. Defaults to
1.imin – Include the limit on the minimum value itself. Defaults to
True.imax – Include the limit on the maximum value itself. Defaults to
True.
The following parameters are available on the final object, calculated from the arguments:
- Parameters:
min – Minimum value, as number.
min_value – Minimum value, as string. Good for showing to the user.
max – Maximum value, as number.
max_value – Maximum value, as string. Good for showing to the user.
rrange – The
rangeobject implementing this object.ldefault – The default label to be shown to the user.
See also
Use
LimitUnboundedif the limit is unbounded in any direction, or if thenumberis not anintsubclass.- __len__() int#
Count the amount of valid values.
Should be used as
len(LimitBounded).
- __getitem__(label: str) int#
Get the value corresponding to the label.
If the value is invalid,
KeyErroris raised.Should be used as
LimitBounded[label].
- __iter__() Iterator[str]#
Iterate through all valid labels.
Should be used as:
for label in LimitBounded: print(label)
- __contains__(label: object) bool#
Check if the label is valid. Must be a
str.Should be used as
label in LimitBounded.
- count_padsize(base: int = 10) int#
Count the strictly necessary amount of padding digits for the range of values.
Depending on the range of supported values, this will calculate the necessary amount of digits to store all possible values, when formatted with the given base. This allows for a label with fixed size.
- Parameters:
base (int) – The base for the positional numeral system, amount of different symbols. For example, hexadecimal uses
base=16.
See also
Use
format_valueto format a value directly.
- format_value(value: int, *, base: Literal[2, 8, 10, 16], invalid: str = '') str#
Format a given
valuewith the strictly necessary amount of padding.Uses standard format for integers with the given
base. The amount of padding is given by thecount_padsizefunction. If the range includes negative values, there will be an extra character on the left, for the signal.- Parameters:
value (int) – The value to format
base (Literal[2, 8, 10, 16]) – The base for the positional numeral system, amount of different symbols. For example, hexadecimal uses
base=16.invalid (str) – The invalid label. Make sure this is really invalid.
- get_label(value: Any | None, *, invalid: str = '') str#
Get a label that produces the given value.
The invalid label is produced for all non-
intvalues.- Parameters:
value (Any | None) – The value object, or
Noneto produce an invalid label.invalid (str) – The invalid label. Make sure this is really invalid.
- w(**kwargs) LimitBounded#
Create a new
LimitBoundedwith different parameters.Reuse all parameters which are not given.
The only parameters that should be changed are:
imin: Include the limit on the minimum value itself.
imax: Include the limit on the maximum value itself.
step: The step amount for the range of values.
default: Default value to be set.
- class tkmilan.validation.LimitUnbounded(min_entry: dataclasses.InitVar[Union[limT, str, NoneType]], max_entry: dataclasses.InitVar[Union[limT, str, NoneType]], fn: Callable[[str], limT | None], imin: bool = True, imax: bool = True, step: limT | None = None, default: dataclasses.InitVar[Union[limT, str, NoneType]] = None, infinite: dataclasses.InitVar[bool] = False)#
Bases:
Mapping[str,limT],Generic[limT],VSpecRepresent a potentially unbounded range to limit a value, generic for any
numbertype.The range can be made to remove the limits themselves with
iminandimax. The default is the limit being closed on both sides.The range is unbounded, which mean some limits might be infinite. If both limits are infinite and this is intended, set
infiniteto avoid warnings.The following arguments are not available on the final object:
- Parameters:
min_entry – Minimum value, as string or number.
Nonerepresents an infinite value.max_entry – Maximum value, as string or number.
Nonerepresents an infinite value.default – Default value to be set, as string, number, or
Noneto choose a valid value (the default). WhenNone, a sane value is chosen. If bothiminandimaxasFalse,0is chosen, which might not be included on the range. Configures the default label.infinite – Do not warn about completely unbounded min and max.
The following parameters are available on the final object:
- Parameters:
fn – A function that turns a string into
number. Usually some kind of parser.step – The step amount for the range of values. Changing this value is unsupported for now, it included for compatibility with
LimitBounded. Defaults toNone.imin – Include the limit on the minimum value itself. Defaults to
True.imax – Include the limit on the maximum value itself. Defaults to
True.
The following parameters are available on the final object, calculated from the arguments:
- Parameters:
min – Minimum value, as number.
Noneif infinite.min_value – Minimum value, as string. Good for showing to the user.
max – Maximum value, as number.
Noneif infinite.max_value – Maximum value, as string. Good for showing to the user.
ldefault – The default label to be shown to the user.
- __getitem__(label: str) limT#
Get the value corresponding to the label.
If the value is invalid,
KeyErroris raised.Should be used as
LimitUnbounded[label].
- __contains__(label: object) bool#
Check if the label is valid. Must be a
str.Should be used as
label in LimitUnbounded.
- get_label(value: limT | None, *, invalid: str = '') str#
Get a label that produces the given value.
- Parameters:
value (limT | None) – The value object, or
Noneto produce an invalid label.invalid (str) – The invalid label. Make sure this is really invalid.
- w(**kwargs) LimitUnbounded#
Create a new
LimitUnboundedwith different parameters.Reuse all parameters which are not given.
The only parameters that should be changed are:
imin: Include the limit on the minimum value itself.
imax: Include the limit on the maximum value itself.
step: The step amount for the range of values.
default: Default value to be set.
- class tkmilan.validation.StaticList(values: Iterable[str], *, default: str | None = None, defaultIndex: int | None = None)#
Bases:
Tuple[str, …],VSpecRepresent a static list of labels, therefore a
tupleofstr.The default label must be explicitely given, either directly as
default, or indirectly asdefaultIndex. Either way is validated to verify the default is a valid label.- Parameters:
values (Iterable[str]) – Values to construct the list.
default (str | None) – Default label. Optional.
defaultIndex (int | None) – Index for the default label, in the
valuessequence. Optional.
- ldefault: str#
The default label
- class tkmilan.validation.StaticMap(mapping: Mapping[str, smvT], *, defaultLabel: str | None = None, defaultValue: smvT | None = None)#
Bases:
UserDict,Mapping[str,smvT],Generic[smvT],VSpecRepresent a static mapping between labels and its corresponding values.
The
mappingparameter matches labels to values. This must be bi-injective, that is, there cannot be values corresponding to multiple labels.The default label must be explicitely given, either directly as
defaultLabel, or indirectly asdefaultValue. Either way is validated to verify the default is a valid label.- Parameters:
mapping – Mapping between labels and values.
defaultLabel – Default label. Optional.
defaultValue – Default value, to derive the default label. Optional.
- ldefault: str#
The default label
- class tkmilan.validation.VSpec#
Bases:
objectAbstract parent class for all validation representations.
- class tkmilan.validation.limT#
Generic Type for
var.Limit.This is a
typing.TypeVar, to be used only when typechecking. Seetyping.alias of TypeVar(‘limT’, bound=
Number)
- class tkmilan.validation.smvT#
Generic Type-Checking variable type for
SpecParsed.This is a
typing.TypeVar, to be used only when typechecking. Seetyping.alias of TypeVar(‘smvT’)
- tkmilan.validation.StaticMapLabels(fn: Callable[[str], smvT], lst: Sequence[str], *, defaultIndex: int | None = None, **kwargs) StaticMap[smvT]#
Turn a list of labels into a mapping, by applying a function to get the value.
Wrapper for
StaticMap.
- tkmilan.validation.StaticMapValues(fn: Callable[[smvT], str], lst: Sequence[smvT], *, defaultIndex: int | None = None, **kwargs) StaticMap[smvT]#
Turn a list of values into a mapping, by applying a function to get the label.
Wrapper for
StaticMap.
- tkmilan.validation.INFO_FORMAT_BASE: Mapping[int, Tuple[str, str, str]] = {2: ('0b', '0', 'b'), 8: ('0o', '0', 'o'), 10: ('', ' ', 'd'), 16: ('0x', '0', 'X')}#
Metadata for
LimitBounded.format_value, per supportedbase.
- tkmilan.validation.LIMIT_INFINITE: str = '∞'#
String representation of infinite boundary.
- tkmilan.validation.LIMIT_ISTRING: Mapping[bool, str] = {False: ']', True: '['}#
Mapping between inclusion boolean, and boundary representation.