examples.05_truthiness_and_ok
Demonstrates how the truthiness (bool()) and .ok attribute of a
ConstrainedValue instance mirror its internal Status.OK state.
This example shows:
- A successful value returns
Truefor both.okandbool(instance).- A failed value returns
Falsefor both.- How to access
detailsfor additional diagnostic information.
Run directly:
python examples/05_truthiness_and_ok.py
Expected output:
g.ok, bool(g) g.details: True True ok b.ok, bool(b) b.details: False False nope, nothing to see here
1"""Demonstrates how the truthiness (`bool()`) and `.ok` attribute of a 2`ConstrainedValue` instance mirror its internal `Status.OK` state. 3 4This example shows: 5 * A successful value returns `True` for both `.ok` and `bool(instance)`. 6 * A failed value returns `False` for both. 7 * How to access `details` for additional diagnostic information. 8 9Run directly: 10 11 python examples/05_truthiness_and_ok.py 12 13Expected output: 14 15 g.ok, bool(g) g.details: True True ok 16 b.ok, bool(b) b.details: False False nope, nothing to see here 17""" 18 19import sys 20import pathlib 21from typing import Any, List 22 23# --------------------------------------------------------------------------- 24# Make repo root importable when running this file directly 25# --------------------------------------------------------------------------- 26sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 27 28from constrained_values import Response, Status 29from constrained_values.value import ( 30 TransformationStrategy, 31 ConstrainedValue, 32 PipeLineStrategy, 33) 34 35 36class Pass(TransformationStrategy[Any, Any]): 37 """A strategy that always succeeds. 38 39 This transformation simply returns the input value unchanged with 40 `Status.OK`, simulating a successful validation or transformation step. 41 """ 42 43 def transform(self, value: Any) -> Response[Any]: 44 """Return an OK response with the value unchanged. 45 46 Args: 47 value: The input to pass through. 48 49 Returns: 50 Response[Any]: Contains `status=Status.OK`, `details='ok'`, and 51 the same `value`. 52 """ 53 return Response(status=Status.OK, details="ok", value=value) 54 55 56class Fail(TransformationStrategy[Any, Any]): 57 """A strategy that always fails. 58 59 This demonstrates how a `ConstrainedValue` propagates a non-OK status and 60 becomes falsy when evaluated in a boolean context. 61 """ 62 63 def transform(self, value: Any) -> Response[Any]: 64 """Return a failed response. 65 66 Args: 67 value: Any input value (ignored). 68 69 Returns: 70 Response[Any]: Contains `status=Status.EXCEPTION`, 71 `details='nope, nothing to see here'`, and `value=None`. 72 """ 73 return Response( 74 status=Status.EXCEPTION, 75 details="nope, nothing to see here", 76 value=None, 77 ) 78 79 80class Good(ConstrainedValue[int]): 81 """A `ConstrainedValue[int]` that always succeeds. 82 83 The pipeline consists of a single `Pass` strategy. 84 """ 85 86 def get_strategies(self) -> List[PipeLineStrategy]: 87 """Return a one-step pass-through pipeline.""" 88 return [Pass()] 89 90 91class Bad(ConstrainedValue[int]): 92 """A `ConstrainedValue[int]` that always fails. 93 94 The pipeline consists of a single `Fail` strategy. 95 """ 96 97 def get_strategies(self) -> List[PipeLineStrategy]: 98 """Return a one-step failing pipeline.""" 99 return [Fail()] 100 101 102def main() -> None: 103 """Run the truthiness and `.ok` demonstration. 104 105 Creates one successful (:class:`Good`) and one failing (:class:`Bad`) 106 constrained value, then prints their `.ok` property, `bool()` result, 107 and `.details`. 108 109 Prints: 110 - `g.ok, bool(g), g.details` → `True True validation successful` 111 - `b.ok, bool(b), b.details` → `False False nope, nothing to see here` 112 """ 113 g, b = Good(1), Bad(2) 114 print("g.ok, bool(g) g.details:", g.ok, bool(g), g.details) 115 print("b.ok, bool(b) b.details:", b.ok, bool(b), b.details) 116 117 118if __name__ == "__main__": 119 main()
37class Pass(TransformationStrategy[Any, Any]): 38 """A strategy that always succeeds. 39 40 This transformation simply returns the input value unchanged with 41 `Status.OK`, simulating a successful validation or transformation step. 42 """ 43 44 def transform(self, value: Any) -> Response[Any]: 45 """Return an OK response with the value unchanged. 46 47 Args: 48 value: The input to pass through. 49 50 Returns: 51 Response[Any]: Contains `status=Status.OK`, `details='ok'`, and 52 the same `value`. 53 """ 54 return Response(status=Status.OK, details="ok", value=value)
A strategy that always succeeds.
This transformation simply returns the input value unchanged with
Status.OK, simulating a successful validation or transformation step.
44 def transform(self, value: Any) -> Response[Any]: 45 """Return an OK response with the value unchanged. 46 47 Args: 48 value: The input to pass through. 49 50 Returns: 51 Response[Any]: Contains `status=Status.OK`, `details='ok'`, and 52 the same `value`. 53 """ 54 return Response(status=Status.OK, details="ok", value=value)
Return an OK response with the value unchanged.
Arguments:
- value: The input to pass through.
Returns:
Response[Any]: Contains
status=Status.OK,details='ok', and the samevalue.
57class Fail(TransformationStrategy[Any, Any]): 58 """A strategy that always fails. 59 60 This demonstrates how a `ConstrainedValue` propagates a non-OK status and 61 becomes falsy when evaluated in a boolean context. 62 """ 63 64 def transform(self, value: Any) -> Response[Any]: 65 """Return a failed response. 66 67 Args: 68 value: Any input value (ignored). 69 70 Returns: 71 Response[Any]: Contains `status=Status.EXCEPTION`, 72 `details='nope, nothing to see here'`, and `value=None`. 73 """ 74 return Response( 75 status=Status.EXCEPTION, 76 details="nope, nothing to see here", 77 value=None, 78 )
A strategy that always fails.
This demonstrates how a ConstrainedValue propagates a non-OK status and
becomes falsy when evaluated in a boolean context.
64 def transform(self, value: Any) -> Response[Any]: 65 """Return a failed response. 66 67 Args: 68 value: Any input value (ignored). 69 70 Returns: 71 Response[Any]: Contains `status=Status.EXCEPTION`, 72 `details='nope, nothing to see here'`, and `value=None`. 73 """ 74 return Response( 75 status=Status.EXCEPTION, 76 details="nope, nothing to see here", 77 value=None, 78 )
Return a failed response.
Arguments:
- value: Any input value (ignored).
Returns:
Response[Any]: Contains
status=Status.EXCEPTION,details='nope, nothing to see here', andvalue=None.
81class Good(ConstrainedValue[int]): 82 """A `ConstrainedValue[int]` that always succeeds. 83 84 The pipeline consists of a single `Pass` strategy. 85 """ 86 87 def get_strategies(self) -> List[PipeLineStrategy]: 88 """Return a one-step pass-through pipeline.""" 89 return [Pass()]
A ConstrainedValue[int] that always succeeds.
The pipeline consists of a single Pass strategy.
87 def get_strategies(self) -> List[PipeLineStrategy]: 88 """Return a one-step pass-through pipeline.""" 89 return [Pass()]
Return a one-step pass-through pipeline.
Inherited Members
- constrained_values.value.ConstrainedValue
- ConstrainedValue
- status
- details
- value
- unwrap
- ok
92class Bad(ConstrainedValue[int]): 93 """A `ConstrainedValue[int]` that always fails. 94 95 The pipeline consists of a single `Fail` strategy. 96 """ 97 98 def get_strategies(self) -> List[PipeLineStrategy]: 99 """Return a one-step failing pipeline.""" 100 return [Fail()]
A ConstrainedValue[int] that always fails.
The pipeline consists of a single Fail strategy.
98 def get_strategies(self) -> List[PipeLineStrategy]: 99 """Return a one-step failing pipeline.""" 100 return [Fail()]
Return a one-step failing pipeline.
Inherited Members
- constrained_values.value.ConstrainedValue
- ConstrainedValue
- status
- details
- value
- unwrap
- ok
103def main() -> None: 104 """Run the truthiness and `.ok` demonstration. 105 106 Creates one successful (:class:`Good`) and one failing (:class:`Bad`) 107 constrained value, then prints their `.ok` property, `bool()` result, 108 and `.details`. 109 110 Prints: 111 - `g.ok, bool(g), g.details` → `True True validation successful` 112 - `b.ok, bool(b), b.details` → `False False nope, nothing to see here` 113 """ 114 g, b = Good(1), Bad(2) 115 print("g.ok, bool(g) g.details:", g.ok, bool(g), g.details) 116 print("b.ok, bool(b) b.details:", b.ok, bool(b), b.details)
Run the truthiness and .ok demonstration.
Creates one successful (Good) and one failing (Bad)
constrained value, then prints their .ok property, bool() result,
and .details.
Prints:
g.ok, bool(g), g.details→True True validation successfulb.ok, bool(b), b.details→False False nope, nothing to see here