datavalue 0.1.2__py3-none-any.whl → 0.1.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,112 @@
1
+ # Library import
2
+ import re
3
+ from typing import Type, Optional, Any, Iterable, Union
4
+ from .. import exceptions
5
+
6
+ # Classes definition
7
+ class PrimitiveData:
8
+ def __init__(self,
9
+ data_type: Type,
10
+ value: Any,
11
+ maximum_length: Optional[int] = None, minimum_length: Optional[int] = None,
12
+ maximum_size: Optional[Union[int, float]] = None, minimum_size: Optional[Union[int, float]] = None,
13
+ possible_values: Optional[Iterable] = None,
14
+ regular_expression: Optional[str] = None,
15
+
16
+ data_class: Optional[bool] = False
17
+ ) -> None:
18
+ # Instance properties assignment
19
+ self.data_type = data_type
20
+ self.value = value
21
+ self.maximum_length = maximum_length; self.minimum_length = minimum_length
22
+ self.maximum_size = maximum_size; self.minimum_size = minimum_size
23
+ self.possible_values = possible_values
24
+ self.regular_expression = regular_expression
25
+ self.data_class = data_class
26
+
27
+ if not self.data_class:
28
+ self.validate()
29
+
30
+ # Private methods
31
+ def __get_length(self, data: Optional[Any] = None) -> Optional[int]:
32
+ if data is None:
33
+ data_objective = self.value
34
+ else:
35
+ data_objective = data
36
+
37
+ if isinstance(data_objective, (str, bytes, bytearray)):
38
+ return len(data_objective)
39
+ elif isinstance(data_objective, (int, float)):
40
+ return len([digit for digit in str(data_objective) if digit.isdigit()])
41
+ else:
42
+ return None
43
+
44
+ # Public methods
45
+ def to_dict(self) -> dict:
46
+ return {
47
+ "DATA_TYPE":self.data_type.__name__ if hasattr(self.data_type, '__name__') else str(self.data_type),
48
+ "VALUE":self.value,
49
+ "MAXIMUM_LENGTH":self.maximum_length,
50
+ "MINIMUM_LENGTH":self.minimum_length,
51
+ "MAXIMUM_SIZE":self.maximum_size,
52
+ "MINIMUM_SIZE":self.minimum_size,
53
+ "POSSIBLE_VALUES":self.possible_values if self.possible_values is not None else None,
54
+ "REGULAR_EXPRESSION":self.regular_expression,
55
+ "DATA_CLASS":self.data_class
56
+ }
57
+
58
+ def validate(self, data: Optional[Any] = None) -> bool:
59
+ # Define the data to validate
60
+ if data is None:
61
+ data_objective = self.value
62
+ else:
63
+ data_objective = data
64
+
65
+ # Validacion de tipo de dato
66
+ if not isinstance(data_objective, self.data_type):
67
+ raise exceptions.DataTypeException(
68
+ f"Incorrect data type.\nExpected: {self.data_type.__name__} - Received: {type(data_objective).__name__}"
69
+ )
70
+
71
+ # Validacion de longitud de caracteres (minimo, y maximo)
72
+ if self.minimum_length is not None or self.maximum_length is not None:
73
+ length = self.__get_length(data_objective)
74
+
75
+ if length is not None:
76
+ if self.minimum_length is not None and length < self.minimum_length:
77
+ raise exceptions.LengthException(f"Character/digit length below the minimum: {length} < {self.minimum_length}")
78
+ elif self.maximum_length is not None and length > self.maximum_length:
79
+ raise exceptions.LengthException(f"Character/digit length above the maximum: {length} > {self.maximum_length}")
80
+
81
+ # Validacion de tamaño (magnitud)
82
+ if self.minimum_size is not None or self.maximum_size is not None:
83
+ if isinstance(data_objective, (int, float)):
84
+ if self.minimum_size is not None and data_objective < self.minimum_size:
85
+ raise exceptions.SizeException(f"Numerical value below the minimum: {data_objective} < {self.minimum_size}")
86
+ if self.maximum_size is not None and data_objective > self.maximum_size:
87
+ raise exceptions.SizeException(f"Numerical value above the maximum: {data_objective} > {self.maximum_size}")
88
+
89
+ # Validacion de posibles valores (conjunto)
90
+ if self.data_type is bool:
91
+ if data_objective not in (True, False):
92
+ raise exceptions.PossibleValueException(
93
+ f"The boolean value has to be True or False: {data_objective} != True/False"
94
+ )
95
+ if self.possible_values is not None:
96
+ if data_objective not in self.possible_values:
97
+ raise exceptions.PossibleValueException(
98
+ f"The value is not in the possible values set: {data_objective} not in {self.possible_values}"
99
+ )
100
+
101
+ # Validacion de expresion regular
102
+ if self.regular_expression is not None:
103
+ if isinstance(data_objective, (str, bytes, bytearray)):
104
+ # Soporte para bytes/bytearray convirtiendo el patrón si es necesario
105
+ pattern = self.regular_expression
106
+ if isinstance(data_objective, (bytes, bytearray)) and isinstance(pattern, str):
107
+ pattern = pattern.encode()
108
+
109
+ if not re.fullmatch(pattern, data_objective):
110
+ raise exceptions.RegularExpressionException(f"The value does not meet the required pattern: {self.regular_expression}")
111
+
112
+ return True
@@ -0,0 +1,10 @@
1
+ # Classes definition
2
+ # Base exception
3
+ class DataValueException(Exception): pass
4
+
5
+ # Specific exceptions
6
+ class DataTypeException(DataValueException): pass
7
+ class LengthException(DataValueException): pass
8
+ class SizeException(DataValueException): pass
9
+ class PossibleValueException(DataValueException): pass
10
+ class RegularExpressionException(DataValueException): pass
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: datavalue
3
- Version: 0.1.2
3
+ Version: 0.1.4
4
4
  Summary: Librería de tipos de datos primitivos y complejos
5
5
  Author: Specter
6
6
  Requires-Python: >=3.10
@@ -0,0 +1,9 @@
1
+ datavalue/__init__.py,sha256=QHthd6CQaDPqpvS3ay1r_Z7WvIleUXbTAottuML8nPA,66
2
+ datavalue/classes/__init__.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
3
+ datavalue/classes/primitive_data.py,sha256=qsKlds33RqR0bIGjy-5kgZdNBcrBMITbrvVx4HqeO-M,5281
4
+ datavalue/exceptions/__init__.py,sha256=T9X1N0kAv6RNTuTNQR7aTAxxvF25ihMsrtZGD-QZrBI,361
5
+ tests/test.py,sha256=8-AhuPJk0Yxt49MmbbdqGamhWM87T9mkbfmIU3BZZJk,1845
6
+ datavalue-0.1.4.dist-info/METADATA,sha256=85qTmyY5lcP96JpIi000IqYtvd-O23ZSTiL03SZymHc,1597
7
+ datavalue-0.1.4.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
8
+ datavalue-0.1.4.dist-info/top_level.txt,sha256=zlD2cuGB4gcH2hf8HIBij-0hqp_7e-c2e5nn6-A9j1Y,16
9
+ datavalue-0.1.4.dist-info/RECORD,,
tests/test.py ADDED
@@ -0,0 +1,62 @@
1
+ # Library import
2
+ from datavalue import PrimitiveData
3
+
4
+ # Phone number validation
5
+ print("="*10)
6
+ print("Validating phone number...")
7
+ phone_number = PrimitiveData(
8
+ data_type=str,
9
+ value="+34600111222",
10
+ minimum_length=7, # Minimum character length
11
+ maximum_length=15, # Maximum character length
12
+ minimum_size=None, # Not validate the minimum number
13
+ maximum_size=None, # Not validate the maximum number,
14
+ possible_values=None, # Not specify obligatory possible values
15
+ regular_expression=r"^\+[1-9]\d{6,14}$"
16
+ )
17
+
18
+ print("Phone number validated sucessfully:", phone_number.value)
19
+
20
+ # Connection port validation
21
+ print("="*10)
22
+ print("Validating connection port...")
23
+ connection_port = PrimitiveData(
24
+ data_type=int,
25
+ value=45321,
26
+ minimum_length=1, # Maximum digits
27
+ maximum_length=5, # Minimum digits
28
+ minimum_size=1, # Minimum numerical value
29
+ maximum_size=65535, # Maximum numerical value
30
+ possible_values=None, # Possible options
31
+ regular_expression=None # Regular expression applied
32
+ )
33
+ print("Connection port validated:", connection_port.value)
34
+
35
+ # Transport protocol validation
36
+ print("="*10)
37
+ print("Validating transport protocol...")
38
+ transport_protocol = PrimitiveData(
39
+ data_type=str,
40
+ value="TCP",
41
+ maximum_length=None,
42
+ minimum_length=None,
43
+ minimum_size=None,
44
+ maximum_size=None,
45
+ possible_values=("TCP", "UDP"),
46
+ regular_expression=None
47
+ )
48
+ print("Transport protocol validated:", transport_protocol.value)
49
+
50
+ # Validating IPv4 address
51
+ print("="*10)
52
+ print("Validating IPv4 address...")
53
+ ip_address = PrimitiveData(
54
+ data_type=str,
55
+ value="192.168.0.1",
56
+ maximum_length=15,
57
+ minimum_length=7,
58
+ maximum_size=None, minimum_size=None,
59
+ possible_values=None,
60
+ regular_expression=r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
61
+ )
62
+ print("IPv4 address validated:", ip_address.value)
@@ -1,5 +0,0 @@
1
- datavalue/__init__.py,sha256=QHthd6CQaDPqpvS3ay1r_Z7WvIleUXbTAottuML8nPA,66
2
- datavalue-0.1.2.dist-info/METADATA,sha256=eFUJRXdcLKGBfgfGqFq16SgN8uECGtYKzoIP7ssCisE,1597
3
- datavalue-0.1.2.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
4
- datavalue-0.1.2.dist-info/top_level.txt,sha256=tVZ_--yYzvlZ2XTCkdORn4KKL7BwXPZUOVpL6PBJaRY,10
5
- datavalue-0.1.2.dist-info/RECORD,,