Detail: Require: Constructor

Overview: Require: Constructor

Require.Apply(func)
Apply()[source]

Apply a function to a container with an arbitrary function. The validation passes if the function returns True (or a truthy value).

Parameters:

func – A function that takes a container and returns a Boolean.

>>> import typing as tp
>>> def func1(ix: tp.Annotated[sf.Index[np.int64], sf.Require.Apply(lambda ix: (ix > 0).all())]): return ix.max()
>>> func2 = sf.CallGuard.check(func1)
>>> ix1 = sf.Index((1024, 2048, 4096), name='y')
>>> ix1
<Index: y>
1024
2048
4096
<int64>
>>> func2(ix1)
4096
>>> ix2 = sf.Index((0, 1024, -2048, 4096))
>>> ix2
<Index>
0
1024
-2048
4096
<int64>
>>> func2(ix2)
ClinicError('\nIn args of (ix: Annotated[Index[int64], Apply(<lambda>)]) -> Any\n└── Annotated[Index[int64], Apply(<lambda>)]\n    └── Apply(<lambda>)\n        └── Index failed validation with <lambda>')
Require.LabelsMatch(*labels)
LabelsMatch()[source]

Validate the presence of one or more labels, specified with the value, a pattern, or set of values. Order of labels is not relevant.

Parameters:

*labels – Provide labels matchers as args. A label matcher can be a label, a set of labels (of which at least one contained label must match), or a compiled regular expression (with which the search method is used to determine a match of string labels). Each label matcher provided must find at least one match, otherwise an error is returned.

>>> import re
>>> import typing as tp
>>> def func1(ix: tp.Annotated[sf.Index[np.object_], sf.Require.LabelsMatch(True, None, {1024, 2048}, re.compile("^A"))]): return len(ix)
>>> func2 = sf.CallGuard.check(func1)
>>> ix1 = sf.Index((None, 'A', 1024, True), name='x')
>>> ix1
<Index: x>
None
A
1024
True
<object>
>>> func2(ix1)
4
>>> ix2 = sf.Index((None, 'b', False, True, 2048), name='x')
>>> ix2
<Index: x>
None
b
False
True
2048
<object>
>>> func2(ix2)
ClinicError("\nIn args of (ix: Annotated[Index[object_], LabelsMatch(True, None, {1024, 2048}, re.compile('^A'))]) -> Any\n└── Annotated[Index[object_], LabelsMatch(True, None, {1024, 2048}, re.compile('^A'))]\n    └── LabelsMatch(True, None, {1024, 2048}, re.compile('^A'))\n        └── Expected label to match re.compile('^A'), no provided match")
Require.LabelsOrder(*labels)
LabelsOrder()[source]

Validate the ordering of labels.

Parameters:

*labels – Provide labels as args. Use … for regions of zero or more undefined labels.

>>> import typing as tp
>>> def func1(ix: tp.Annotated[sf.Index[np.int64], sf.Require.LabelsOrder(1024, ..., 4096)]): return ix.max()
>>> func2 = sf.CallGuard.check(func1)
>>> ix1 = sf.Index((1024, 2048, 4096), name='y')
>>> ix1
<Index: y>
1024
2048
4096
<int64>
>>> func2(ix1)
4096
>>> ix2 = sf.Index((0, 1024, -2048, 4096))
>>> ix2
<Index>
0
1024
-2048
4096
<int64>
>>> func2(ix2)
ClinicError('\nIn args of (ix: Annotated[Index[int64], LabelsOrder(1024, ..., 4096)]) -> Any\n└── Annotated[Index[int64], LabelsOrder(1024, ..., 4096)]\n    └── LabelsOrder(1024, ..., 4096)\n        └── Expected 1024, provided 0')
Require.Len(len)
Len()[source]

Validate the length of a container.

Parameters:
  • len – The length to validate against.

  • /

>>> import typing as tp
>>> def func1(ix: tp.Annotated[sf.Index[np.int64], sf.Require.Len(3)]): return ix.max()
>>> func2 = sf.CallGuard.check(func1)
>>> ix1 = sf.Index((1024, 2048, 4096), name='y')
>>> ix1
<Index: y>
1024
2048
4096
<int64>
>>> func2(ix1)
4096
>>> ix2 = sf.Index((0, 1024, -2048, 4096))
>>> ix2
<Index>
0
1024
-2048
4096
<int64>
>>> func2(ix2)
ClinicError('\nIn args of (ix: Annotated[Index[int64], Len(3)]) -> Any\n└── Annotated[Index[int64], Len(3)]\n    └── Len(3)\n        └── Expected length 3, provided length 4')
Require.Name(name)
Name()[source]

Validate the name of a container.

Parameters:
  • name – The name to validate against.

  • /

>>> import typing as tp
>>> def func1(ix: tp.Annotated[sf.Index[np.int64], sf.Require.Name("y")]): return ix.max()
>>> func2 = sf.CallGuard.check(func1)
>>> ix1 = sf.Index((1024, 2048, 4096), name='y')
>>> ix1
<Index: y>
1024
2048
4096
<int64>
>>> func2(ix1)
4096
>>> ix2 = sf.Index((0, 1024, -2048, 4096))
>>> ix2
<Index>
0
1024
-2048
4096
<int64>
>>> func2(ix2)
ClinicError("\nIn args of (ix: Annotated[Index[int64], Name(y)]) -> Any\n└── Annotated[Index[int64], Name(y)]\n    └── Name(y)\n        └── Expected name 'y', provided name None")
Require.Shape(*shape)
Shape()[source]

Validate the length of a container.

Parameters:
  • shape – A tuple of one or two values, where values are either an integer or an , specifying any value for that position. The size of the shape always species the dimensionality.

  • /

>>> import typing as tp
>>> def func1(f: tp.Annotated[sf.TFrameAny, sf.Require.Shape(3, 2)]): return f.sum().sum()
>>> func2 = sf.CallGuard.check(func1)
>>> f1 = sf.Frame(np.arange(6).reshape(3,2), index=('p', 'q', 'r'), columns=('a', 'b'), name='x')
>>> f1
<Frame: x>
<Index>    a       b       <<U1>
<Index>
p          0       1
q          2       3
r          4       5
<<U1>      <int64> <int64>
>>> func2(f1)
15
>>> f2 = sf.Frame.from_fields(((2, 9), (3, 8)), columns=('a', 'b'), index=('p', 'q'), name='x')
>>> f2
<Frame: x>
<Index>    a       b       <<U1>
<Index>
p          2       3
q          9       8
<<U1>      <int64> <int64>
>>> func2(f2)
ClinicError('\nIn args of (f: Annotated[Frame[Any, Any, Unpack[Tuple[Any, ...]]], Shape((3, 2))]) -> Any\n└── Annotated[Frame[Any, Any, Unpack[Tuple[Any, ...]]], Shape((3, 2))]\n    └── Shape((3, 2))\n        └── Expected shape ((3, 2)), provided shape (2, 2)')

Require: Constructor