Field Mapper a Python library for seamless data field validation, transformation, and mapping with support for types, lengths, and custom rules.
Project description
Field Mapper: Documentation
Field Mapper is a Python library designed for validating, mapping, and transforming data fields. It is particularly useful for integrating third-party systems, ensuring compatibility when third-party fields and internal system fields differ. The library supports type checking, length constraints, optional fields, custom validation rules, and seamless data transformation for structured data validation.
Installation
Install the library using pip
pip install field-mapper
Quick Start
- Define Fields Create a field map dictionary. Also, Create a fields dictionary to define the rules for your data fields.
field_map = {
"name": "full_name",
"email": "contact_email",
"phone": "mobile_number",
"income": "monthly_income"
}
fields = {
"name": {"type": str, "max_length": 50, "required_field": True, "required_value":True},
"email": {"type": str, "max_length": 100, "required_field": True, "required_value":True},
"phone": {"type": str, "max_length": 15, "required_field": False, "required_value":False},
"income": {"type": int, "max_length": 15, "required_field": True, "required_value":False}
}
- Prepare Data The input should be a list of dictionaries.
data = [
{"name": "Alice", "email": "alice@example.com", "phone": "1234567890"},
{"name": "Bob", "email": "charlieexample.com", "phone": "453543535", "income":0},
{"name": "Charlie", "email": "charlie@example.com", "phone": "34534523", "income":0}
]
- Data Process Use the process method to check and transform the data.
from field_mapper import FieldMapper
field_map = {
"name": "full_name",
"email": "contact_email",
"phone": "mobile_number",
"income": "monthly_income"
}
fields = {
"name": {"type": str, "max_length": 50, "required_field": True, "required_value":True},
"email": {"type": str, "max_length": 100, "required_field": True, "required_value":True},
"phone": {"type": str, "max_length": 15, "required_field": False, "required_value":False},
"income": {"type": int, "max_length": 15, "required_field": True, "required_value":False}
}
data = [
{"name": "Alice", "email": "alice@example.com", "phone": "1234567890"},
{"name": "Bob", "email": "charlieexample.com", "phone": "453543535", "income":0},
{"name": "Charlie", "email": "charlie@example.com", "phone": "34534523", "income":0}
]
mapper = FieldMapper(fields, field_map)
processed_data = mapper.process(data)
print(processed_data)
print(mapper.error)
- Custom Validation Define custom validation logic for specific fields.
def validate_email(value):
import re
if not re.match(r"[^@]+@[^@]+\.[^@]+", value):
raise ValueError(f"Invalid email address: {value}")
#Add the custom validator in the field definition:
field_map = {
"name": "full_name",
"email": "contact_email",
"phone": "mobile_number",
"income": "monthly_income"
}
fields = {
"name": {"type": str, "max_length": 50, "required_field": True, "required_value":True},
"email": {"type": str, "max_length": 100, "required_field": True, "required_value":True, "custom": validate_email},
"phone": {"type": str, "max_length": 15, "required_field": False, "required_value":False},
"income": {"type": int, "max_length": 15, "required_field": True, "required_value":False}
}
data = [
{"name": "Alice", "email": "alice@example.com", "phone": "1234567890"},
{"name": "Bob", "email": "charlieexample.com", "phone": "453543535", "income":0},
{"name": "Charlie", "email": "charlie@example.com", "phone": "34534523", "income":0}
]
mapper = FieldMapper(fields, field_map)
processed_data = mapper.process(data)
print(processed_data)
print(mapper.error)
- Optional Fields Mark fields as optional with required_field: False.
fields = {
"phone": {"type": str, "max_length": 15, "required_field": False},
}
- Required Value If required_value=True is set, their presence is mandatory and values can't be empty.
fields = {"email": {"type": str, "max_length": 100, "required_field": True, "required_value":True}
- Skip Duplicates Data If skip_duplicate=True is set, Skip duplicate data. Raises:DuplicatesDataError: If duplicate entries are found.
from field_mapper.mapper import FieldMapper
field_map = {
"name": "full_name",
"email": "contact_email",
"phone": "mobile_number",
"income": "monthly_income"
}
fields = {
"name": {"type": str, "max_length": 50, "required_field": True, "required_value":True},
"email": {"type": str, "max_length": 100, "required_field": True, "required_value":True},
"phone": {"type": str, "max_length": 15, "required_field": False, "required_value":False},
"income": {"type": int, "max_length": 15, "required_field": True, "required_value":False}
}
data = [
{"name": "Charlie", "email": "charlie@example.com", "phone": "888888", "income":0},
{"name": "Charlie", "email": "charlie@example.com", "phone": "444444", "income":0},
{"name": "Charlie", "email": "charlie@example.com", "phone": "444444", "income":0}
]
mapper = FieldMapper(fields, field_map)
processed_data = mapper.process(data, skip_duplicate=True)
print("Output:", processed_data)
print("Error:",mapper.error)
Example usage
from field_mapper import FieldMapper
def validate_email(value: str) -> bool:
return "@" in value and "." in value
field_map = {
"name": "full_name",
"email": "contact_email",
"phone": "mobile_number",
"income": "monthly_income"
}
fields = {
"name": {"type": str, "max_length": 50, "required_field": True, "required_value":True},
"email": {"type": str, "max_length": 100, "required_field": True, "required_value":True, "custom": validate_email},
"phone": {"type": str, "max_length": 15, "required_field": False, "required_value":False},
"income": {"type": int, "max_length": 15, "required_field": True, "required_value":False}
}
data = [
{"name": "Alice", "email": "alice@example.com", "phone": "1234567890"},
{"name": "Bob", "email": "charlieexample.com", "phone": "453543535", "income":0},
{"name": "Charlie", "email": "charlie@example.com", "phone": "34534523", "income":0}
]
mapper = FieldMapper(fields, field_map)
processed_data = mapper.process(data)
print(processed_data)
print(mapper.error)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file field_mapper-0.2.0.tar.gz.
File metadata
- Download URL: field_mapper-0.2.0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
85fcf1fe505476b8b6423472f94f296f9bd5795bf1e69cc8f9e6488d3ddca819
|
|
| MD5 |
cba5c76cdad406fd33b23af2db3d2efa
|
|
| BLAKE2b-256 |
c2f6ad3e0a60d96251138b0ab0fbbe6d474ad9441496096ea1150d5b718926cd
|
File details
Details for the file field_mapper-0.2.0-py3-none-any.whl.
File metadata
- Download URL: field_mapper-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b34095c8cf81039aa35f57e8da31dc572c583bb8e4b66a72fbbce37024583af
|
|
| MD5 |
69bd61a290cf20226bf6c37504e394cf
|
|
| BLAKE2b-256 |
33c0334b3880296091f12424ecb1fb51591c813cf1e172d9a75432017df38c19
|