datavalue 0.1.7__tar.gz → 0.1.9__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: datavalue
3
- Version: 0.1.7
3
+ Version: 0.1.9
4
4
  Summary: Librería de tipos de datos primitivos y complejos
5
5
  Author: Specter
6
6
  Requires-Python: >=3.10
@@ -96,7 +96,8 @@ class ComplexData:
96
96
 
97
97
  return True
98
98
 
99
- def _serialize_recursive(self, element: Any) -> Any:
99
+ @classmethod
100
+ def _serialize_recursive(cls, element: Any) -> Any:
100
101
  # 1. Caso: Instancias de tus clases
101
102
  if isinstance(element, (PrimitiveData, ComplexData)):
102
103
  return {
@@ -110,15 +111,16 @@ class ComplexData:
110
111
 
111
112
  # 3. Caso: Colecciones estándar (recursión profunda)
112
113
  if isinstance(element, (list, tuple, set, frozenset)):
113
- return [self._serialize_recursive(i) for i in element]
114
+ return [cls._serialize_recursive(i) for i in element]
114
115
 
115
116
  if isinstance(element, dict):
116
- return {str(k): self._serialize_recursive(v) for k, v in element.items()}
117
+ return {str(k): cls._serialize_recursive(v) for k, v in element.items()}
117
118
 
118
119
  # 4. Caso: Literales serializables (int, str, float, bool, None)
119
120
  return element
120
121
 
121
- def _deserialize_recursive(self, element: Any) -> Any:
122
+ @classmethod
123
+ def _deserialize_recursive(cls, element: Any) -> Any:
122
124
  SAFE_TYPES = {
123
125
  "list": list, "tuple": tuple, "set": set, "frozenset": frozenset,
124
126
  "dict": dict, "str": str, "int": int, "float": float, "bool": bool,
@@ -133,9 +135,9 @@ class ComplexData:
133
135
  content = element["content"]
134
136
 
135
137
  if obj_type == "PrimitiveData":
136
- return PrimitiveData(value=None, data_type=None, data_class=True).from_dict(content)
138
+ return PrimitiveData.from_dict(content)
137
139
  elif obj_type == "ComplexData":
138
- return self.from_dict(content)
140
+ return cls.from_dict(content)
139
141
  else:
140
142
  raise ValueError(f"Unknown serialized object type: {obj_type}")
141
143
 
@@ -147,11 +149,11 @@ class ComplexData:
147
149
  raise ValueError(f"Type '{type_name}' is not allowed or unknown.")
148
150
 
149
151
  # Caso 3: Es un diccionario de datos común (recurse keys & values)
150
- return {k: self._deserialize_recursive(v) for k, v in element.items()}
152
+ return {k: cls._deserialize_recursive(v) for k, v in element.items()}
151
153
 
152
154
  # B. Manejo de Listas (recurse items)
153
155
  if isinstance(element, list):
154
- return [self._deserialize_recursive(item) for item in element]
156
+ return [cls._deserialize_recursive(item) for item in element]
155
157
 
156
158
  # C. Literales (retorno directo)
157
159
  return element
@@ -165,11 +167,13 @@ class ComplexData:
165
167
  "MAXIMUM_LENGTH":self.maximum_length,
166
168
  "MINIMUM_LENGTH":self.minimum_length,
167
169
  "POSSIBLE_VALUES":self._serialize_recursive(self.possible_values) if self.possible_values is not None else None,
168
- "DATA_CLASS":self.data_class
170
+ "DATA_CLASS":self.data_class,
171
+ "__type__":"ComplexData"
169
172
  }
170
173
 
171
174
 
172
- def from_dict(self, data: dict) -> 'ComplexData':
175
+ @classmethod
176
+ def from_dict(cls, data: dict) -> 'ComplexData':
173
177
  # 1. Secure types mapping
174
178
  SAFE_TYPES = {
175
179
  "list": list, "tuple": tuple, "set": set, "frozenset": frozenset,
@@ -186,7 +190,7 @@ class ComplexData:
186
190
 
187
191
  # Procesamos los possible_values con el motor recursivo
188
192
  raw_possible = data.get("POSSIBLE_VALUES")
189
- possible_values = self._deserialize_recursive(raw_possible) if raw_possible is not None else None
193
+ possible_values = cls._deserialize_recursive(raw_possible) if raw_possible is not None else None
190
194
 
191
195
  # Corrección de tipo para tuplas (JSON no tiene tuplas, devuelve listas)
192
196
  # Si su __init__ es estricto y requiere tupla para possible_values, convertimos aquí:
@@ -199,7 +203,7 @@ class ComplexData:
199
203
  # aunque su validación actual acepta listas.
200
204
  pass
201
205
 
202
- return ComplexData(
206
+ return cls(
203
207
  data_type=data_type,
204
208
  value=data.get("VALUE"), # Asumimos valor literal o serializable simple
205
209
  maximum_length=data.get("MAXIMUM_LENGTH"),
@@ -209,12 +213,13 @@ class ComplexData:
209
213
  )
210
214
 
211
215
 
212
- def from_json(self, text_content: str) -> 'ComplexData':
216
+ @classmethod
217
+ def from_json(cls, text_content: str) -> 'ComplexData':
213
218
  try:
214
219
  data = json.loads(text_content)
215
220
  except json.JSONDecodeError as e:
216
221
  raise ValueError(f"Invalid JSON: {e}")
217
- return self.from_dict(data)
222
+ return cls.from_dict(data)
218
223
 
219
224
  def to_json(self) -> str:
220
225
  return json.dumps(self.to_dict(), indent=4)
@@ -53,19 +53,20 @@ class PrimitiveData:
53
53
  "MINIMUM_SIZE":self.minimum_size,
54
54
  "POSSIBLE_VALUES":self.possible_values if self.possible_values is not None else None,
55
55
  "REGULAR_EXPRESSION":self.regular_expression,
56
- "DATA_CLASS":self.data_class
56
+ "DATA_CLASS":self.data_class,
57
+ "__type__":"PrimitiveData"
57
58
  }
58
59
 
59
60
  def to_json(self) -> str:
60
61
  return json.dumps(self.to_dict(), indent=4)
61
-
62
62
 
63
- def from_dict(self, data: dict) -> 'PrimitiveData':
63
+ @classmethod
64
+ def from_dict(cls, data: dict) -> 'PrimitiveData':
64
65
  # Expected keys definition
65
66
  expected_keys = {
66
67
  "DATA_TYPE", "VALUE", "MAXIMUM_LENGTH", "MINIMUM_LENGTH",
67
68
  "MAXIMUM_SIZE", "MINIMUM_SIZE", "POSSIBLE_VALUES",
68
- "REGULAR_EXPRESSION", "DATA_CLASS"
69
+ "REGULAR_EXPRESSION", "DATA_CLASS", "__type__"
69
70
  }
70
71
 
71
72
  # Verify unknown keys on the table
@@ -91,7 +92,7 @@ class PrimitiveData:
91
92
  raise TypeError(f"Unsupported or unsafe data type for deserialization: {type_str}")
92
93
 
93
94
  # return instance result
94
- return PrimitiveData(
95
+ return cls(
95
96
  data_type=real_type,
96
97
  value=data.get("VALUE"),
97
98
  maximum_length=data.get("MAXIMUM_LENGTH"),
@@ -103,13 +104,14 @@ class PrimitiveData:
103
104
  data_class=data.get("DATA_CLASS", False)
104
105
  )
105
106
 
106
- def from_json(self, text_content: str) -> 'PrimitiveData':
107
+ @classmethod
108
+ def from_json(cls, text_content: str) -> 'PrimitiveData':
107
109
  try:
108
110
  data_table = json.loads(text_content)
109
111
  except json.JSONDecodeError as Error:
110
112
  raise ValueError(f"Invalid JSON format: {Error}")
111
113
 
112
- return self.from_dict(data_table)
114
+ return cls.from_dict(data_table)
113
115
 
114
116
  def validate(self, data: Optional[Any] = None) -> bool:
115
117
  # Define the data to validate
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: datavalue
3
- Version: 0.1.7
3
+ Version: 0.1.9
4
4
  Summary: Librería de tipos de datos primitivos y complejos
5
5
  Author: Specter
6
6
  Requires-Python: >=3.10
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "datavalue"
7
- version = "0.1.7"
7
+ version = "0.1.9"
8
8
  description = "Librería de tipos de datos primitivos y complejos"
9
9
  readme = "README.md"
10
10
  authors = [{ name="Specter" }]
File without changes
File without changes