dryparse.util
Utility functions and objects used throughout dryparse.
- parse_str(text: str, target_type: type)
Parse string into primitive type
type
.
- first_token_from_regex(regex: str) Pattern
Get first valid token from regular expression
regex
.The first valid token is the smallest substring taken from the beginning of
regex
that forms a valid regex by itself.
- class reassignable_property(getter: Callable[[Any], Any])
Property whose getter function can be assigned per instance.
Caveats
The getter must behave as if its single parameter is the only information it knows about the instance that owns this property. This is necessary to properly facilitate deep copying. (TODO: enhance and clarify example)
Example:
>>> class C: >>> @reassignable_property >>> def prop(self): >>> return "default_value" >>> >>> def __init__(self, value): >>> self.value = value >>> # Correct: >>> self.prop = lambda self_: self_.value >>> # Wrong: >>> self.prop = lambda _: self.value
- verify_function_callable_with_args(func, *args, **kwargs)
Verify if
func(*args, **kwargs)
is a valid call without actually calling the function. If yes, do nothing, else raise an exception.- Raises
dryparse.errors.CallbackDoesNotSupportAllArgumentsError – If the verification fails.