abs-utils 0.2.0__tar.gz → 0.2.2__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.3
2
2
  Name: abs-utils
3
- Version: 0.2.0
3
+ Version: 0.2.2
4
4
  Summary: AutoBridge Systems Utility Library
5
5
  Author: AutoBridgeSystems
6
6
  Author-email: info@autobridgesystems.com
@@ -77,6 +77,7 @@ class FieldType(str, Enum):
77
77
  LIST = "list"
78
78
  ASSOCIATION = "association"
79
79
 
80
+
80
81
  class BaseFieldSchema(BaseModel):
81
82
  """Base schema for field definitions."""
82
83
  name: str = Field(description="Name of the field")
@@ -0,0 +1,28 @@
1
+ from enum import Enum
2
+ from typing import Type, Union, Dict
3
+
4
+
5
+ def extend_enum(
6
+ name: str,
7
+ base_enum: Type[Enum],
8
+ additional_fields: Union[Dict[str, str], Type[Enum]]
9
+ ) -> Type[Enum]:
10
+ """
11
+ Create a new Enum by extending an existing one with additional fields
12
+ provided either as a dict or another Enum class.
13
+
14
+ :param name: Name of the new Enum
15
+ :param base_enum: The base Enum class to extend
16
+ :param additional_fields: A dict or Enum class with new values
17
+ :return: A new Enum class with combined values
18
+ """
19
+ base_members = {e.name: e.value for e in base_enum}
20
+ extra_members = {}
21
+ if isinstance(additional_fields, dict):
22
+ extra_members = additional_fields
23
+ elif isinstance(additional_fields, type) and issubclass(additional_fields, Enum):
24
+ extra_members = {e.name: e.value for e in additional_fields}
25
+
26
+ merged_members = {**base_members, **extra_members}
27
+
28
+ return Enum(name, merged_members, type=str)
@@ -0,0 +1,45 @@
1
+ from typing import Optional, Set, Type
2
+
3
+ from pydantic import BaseModel, Field, create_model
4
+
5
+
6
+ def make_optional(
7
+ model: Type[BaseModel], exclude_fields: Set[str] = None
8
+ ) -> Type[BaseModel]:
9
+ """Make all fields of a Pydantic model optional while preserving validation constraints, and allow field exclusion."""
10
+ fields = {}
11
+ exclude_fields = exclude_fields or set()
12
+
13
+ model_schema = model.model_json_schema()
14
+ properties = model_schema.get("properties", {})
15
+
16
+ for name, field in model.model_fields.items():
17
+ if name in exclude_fields:
18
+ continue # Skip excluded fields
19
+
20
+ field_schema = properties.get(name, {})
21
+ constraints = {}
22
+
23
+ if "minimum" in field_schema:
24
+ constraints["ge"] = field_schema["minimum"]
25
+ if "maximum" in field_schema:
26
+ constraints["le"] = field_schema["maximum"]
27
+ if "exclusiveMinimum" in field_schema:
28
+ constraints["gt"] = field_schema["exclusiveMinimum"]
29
+ if "exclusiveMaximum" in field_schema:
30
+ constraints["lt"] = field_schema["exclusiveMaximum"]
31
+ if "minLength" in field_schema:
32
+ constraints["min_length"] = field_schema["minLength"]
33
+ if "maxLength" in field_schema:
34
+ constraints["max_length"] = field_schema["maxLength"]
35
+ if "pattern" in field_schema:
36
+ constraints["pattern"] = field_schema["pattern"]
37
+ if "description" in field_schema:
38
+ constraints["description"] = field_schema["description"]
39
+
40
+ if "error_messages" in field_schema:
41
+ constraints["error_messages"] = field_schema["error_messages"]
42
+
43
+ fields[name] = (Optional[field.annotation], Field(default=None, **constraints))
44
+
45
+ return create_model(f"Optional{model.__name__}", **fields)
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "abs-utils"
3
- version = "0.2.0"
3
+ version = "0.2.2"
4
4
  description = "AutoBridge Systems Utility Library"
5
5
  authors = [
6
6
  {name = "AutoBridgeSystems", email = "info@autobridgesystems.com"}
File without changes