arganic
arguments
Argument
Argument class.
Represents an argument with properties such as name, default value, and validation rules. Provides a method for validation.
Attributes:
-
name
(str
) –The name of the argument.
-
default
((Any, optional)
) –The default value of the argument.
-
read_only
(bool, default=True
) –Whether the argument is read-only.
-
required
(bool, default=True
) –Whether the argument is required.
-
type
((Type | tuple[Type], optional)
) –The data type(s) of the argument value.
-
validator
((Validator | tuple[Validator], optional)
) –A Validator object or list of Validator objects used to validate the argument value.
-
choices
((tuple, optional)
) –A tuple of choices the argument value can take.
Methods:
-
validate
–Validate the argument value based on the specified rules.
Examples:
Example of a full-featured argument construction:
choices
property
Optional
A list of choices limiting the values that the supplied arguments can have.
default
property
Optional
The default value the argument will take if no value is provided.
read_only
property
Default=True
The argument is read-only, so it can no longer be modified.
required
property
Default=True
The argument is required if the value is missing: an Exception will occur.
type
property
Optional
Defines the type(s) of values accepted for this argument, if the type provided is not valid an Exception will occur.
validator
property
validate
Validate the argument value based on the specified rules.
Parameters:
-
value
(Any
) –The value to validate.
Returns:
-
bool
–True if the value is valid, False otherwise.
Raises:
-
TypeError
–If the value is not of the specified type.
-
ValueError
–If the value is required but not provided, or if it is not among the specified choices.
ArgumentHandler
Handles arguments or properties for decorated classes, methods or functions.
Attributes
values: dict The provided arguments values validated and correctly formatted.
Methods:
-
get
–Retrieves the value of a specified argument or property.
-
set
–Sets the value of a specified argument or property.
get
Retrieves the value of a specified argument or property.
Parameters:
-
key
(str
) –The key of the argument or property to retrieve.
Returns:
-
Any
–The value of the argument or property.
class_properties
class_properties(**_properties: Argument) -> Callable
Decorator for class properties.
A decorator for class properties allowing you to define the data managed by the class during construction and then access these values within the class.
Parameters:
Returns:
-
Callable
–The decorator function.
function_arguments
function_arguments(**_arguments: Argument) -> Callable
Decorator for function arguments.
A decorator for functions allowing you to constrain the arguments provided during the call but also to find the correctly formatted values within the function.
Parameters:
Returns:
-
Callable
–The decorator function.
method_arguments
method_arguments(**_arguments: Argument) -> Callable
Decorator for method arguments.
A decorator for class methods allowing you to constrain the arguments provided during the call but also to find the correctly formatted values within the method.
Parameters:
Returns:
-
Callable
–The decorator function.
validators
Dir
Bases: Validator
Directory Validator.
validate
Test the existence of a directory according to the path provided.
Parameters:
-
value
–Path to the directory that must exist on the file system.
Returns:
-
bool
–True if the directory exists.
Raises:
-
FileNotFoundError
–If the directory does not exist on the file system.
Examples:
Email
Bases: Validator
Email address Validator.
validate
Validates the syntax of an email address.
Parameters:
-
value
–Email address whose syntax must be checked.
Returns:
-
bool
–If the value provided is a correct email address format.
Raises:
-
ValueError
–If the value provided is not a valid email address.
Examples:
File
Bases: Validator
File Validator
validate
Test the existence of a file according to the path provided.
Parameters:
-
value
–Path to the file that must exist on the file system.
Returns:
-
bool
–True if the file exists.
Raises:
-
FileNotFoundError
–If the file does not exist on the file system.
Examples:
MaxLength
Bases: Validator
Maximum length validator.
__init__
Max length Validator constructor.
Parameters:
-
max_length
(int
) –The maximum length that the values to validate must not exceed.
validate
Validates a value whose maximum length must not be greater than the value specified in the validator constructor.
The value must be of a type supporting the builtin len() Python function.
Parameters:
-
value
–The value to validate.
Returns:
-
bool
–True if the validation succeeded.
Raises:
-
TypeError
–If the length of the value is longer than the specified maximum length.
Examples:
MinLength
Bases: Validator
Minimum length validator.
__init__
Max length Validator constructor.
Parameters:
-
min_length
(int
) –The minimum length that the value must be.
validate
Verifies that the provided value have a length must be at least the minimum value given in the validator constructor.
The value must be of a type supporting the builtin len() Python function.
Parameters:
-
value
–The value to validate.
Returns:
-
bool
–True if the validation succeeded.
Raises:
-
TypeError
–If the length of the value is shorter than the specified minimum length.
Examples:
Url
Bases: Validator
URL Validator.
validate
Validate if an URL is well formatted. supported protocols: http, https, ftp, ftps.
Parameters:
-
value
–The value of the URL to validate.
Returns:
-
bool
–True if the URL is well formatted.
Raises:
-
ValueError
–If the provided value is an invalid Url.
Examples:
Validator
Bases: ABC
Base class for validators.
It's possible to define your own validators by extending this class.
Examples:
Example of a custom validator.