dhi 1.0.0__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.
Potentially problematic release.
This version of dhi might be problematic. Click here for more details.
- dhi-1.0.0/LICENSE +21 -0
- dhi-1.0.0/MANIFEST.in +5 -0
- dhi-1.0.0/PKG-INFO +115 -0
- dhi-1.0.0/README.md +79 -0
- dhi-1.0.0/dhi/__init__.py +48 -0
- dhi-1.0.0/dhi/_native.c +379 -0
- dhi-1.0.0/dhi/batch.py +236 -0
- dhi-1.0.0/dhi/libsatya.dylib +0 -0
- dhi-1.0.0/dhi/validator.py +212 -0
- dhi-1.0.0/dhi.egg-info/PKG-INFO +115 -0
- dhi-1.0.0/dhi.egg-info/SOURCES.txt +16 -0
- dhi-1.0.0/dhi.egg-info/dependency_links.txt +1 -0
- dhi-1.0.0/dhi.egg-info/requires.txt +6 -0
- dhi-1.0.0/dhi.egg-info/top_level.txt +1 -0
- dhi-1.0.0/pyproject.toml +49 -0
- dhi-1.0.0/setup.cfg +4 -0
- dhi-1.0.0/setup.py +37 -0
- dhi-1.0.0/tests/test_validators.py +91 -0
dhi-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Rach Pradhan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
dhi-1.0.0/MANIFEST.in
ADDED
dhi-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dhi
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Ultra-fast data validation for Python - 28M validations/sec, 3x faster than Rust alternatives
|
|
5
|
+
Author-email: Rach Pradhan <rach@rachpradhan.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/justrach/satya-zig
|
|
8
|
+
Project-URL: Documentation, https://github.com/justrach/satya-zig#readme
|
|
9
|
+
Project-URL: Repository, https://github.com/justrach/satya-zig
|
|
10
|
+
Project-URL: Issues, https://github.com/justrach/satya-zig/issues
|
|
11
|
+
Keywords: validation,data-validation,pydantic,zig,performance,fast,validator,email,url,uuid,schema
|
|
12
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
15
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Programming Language :: Other
|
|
25
|
+
Classifier: Operating System :: OS Independent
|
|
26
|
+
Classifier: Typing :: Typed
|
|
27
|
+
Requires-Python: >=3.8
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Provides-Extra: dev
|
|
31
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
32
|
+
Requires-Dist: pytest-benchmark>=4.0; extra == "dev"
|
|
33
|
+
Requires-Dist: black>=23.0; extra == "dev"
|
|
34
|
+
Requires-Dist: mypy>=1.0; extra == "dev"
|
|
35
|
+
Dynamic: license-file
|
|
36
|
+
|
|
37
|
+
# dhi - Ultra-Fast Data Validation for Python
|
|
38
|
+
|
|
39
|
+
**The fastest data validation library for Python.** Powered by Zig for maximum performance.
|
|
40
|
+
|
|
41
|
+
## 🚀 Performance
|
|
42
|
+
|
|
43
|
+
**28 million validations/sec** - 3x faster than satya (Rust), 3x faster than msgspec (C)
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
# Validate 10,000 users in 0.36ms
|
|
47
|
+
from dhi import _dhi_native
|
|
48
|
+
|
|
49
|
+
users = [{"name": "Alice", "email": "alice@example.com", "age": 25}, ...]
|
|
50
|
+
|
|
51
|
+
field_specs = {
|
|
52
|
+
'name': ('string', 2, 100),
|
|
53
|
+
'email': ('email',),
|
|
54
|
+
'age': ('int_positive',),
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
results, valid_count = _dhi_native.validate_batch_direct(users, field_specs)
|
|
58
|
+
# 28M users/sec! 🔥
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## ✨ Features
|
|
62
|
+
|
|
63
|
+
- **�� Fastest**: 3x faster than satya (Rust) and msgspec (C)
|
|
64
|
+
- **🎯 24+ Validators**: Email, URL, UUID, IPv4, dates, numbers, strings
|
|
65
|
+
- **🔋 Zero Python Overhead**: C extension extracts directly from dicts
|
|
66
|
+
- **🌍 General Purpose**: Works with any dict structure
|
|
67
|
+
- **💪 Production Ready**: Thoroughly tested and benchmarked
|
|
68
|
+
|
|
69
|
+
## 📦 Installation
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
pip install dhi
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## 🎯 Quick Start
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from dhi import _dhi_native
|
|
79
|
+
|
|
80
|
+
users = [
|
|
81
|
+
{"name": "Alice", "email": "alice@example.com", "age": 25},
|
|
82
|
+
{"name": "Bob", "email": "bob@example.com", "age": 30},
|
|
83
|
+
]
|
|
84
|
+
|
|
85
|
+
field_specs = {
|
|
86
|
+
'name': ('string', 2, 100),
|
|
87
|
+
'email': ('email',),
|
|
88
|
+
'age': ('int_positive',),
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
results, valid_count = _dhi_native.validate_batch_direct(users, field_specs)
|
|
92
|
+
print(f"Valid: {valid_count}/{len(users)}")
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## �� Available Validators
|
|
96
|
+
|
|
97
|
+
### String: `email`, `url`, `uuid`, `ipv4`, `base64`, `iso_date`, `iso_datetime`, `string`
|
|
98
|
+
### Number: `int`, `int_gt`, `int_gte`, `int_lt`, `int_lte`, `int_positive`, `int_non_negative`, `int_multiple_of`
|
|
99
|
+
|
|
100
|
+
## 🏆 Benchmarks
|
|
101
|
+
|
|
102
|
+
```
|
|
103
|
+
dhi: 28M users/sec 🥇
|
|
104
|
+
satya: 9M users/sec (3.0x slower)
|
|
105
|
+
msgspec: 9M users/sec (3.1x slower)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## 📝 License
|
|
109
|
+
|
|
110
|
+
MIT License - see LICENSE file
|
|
111
|
+
|
|
112
|
+
## 🔗 Links
|
|
113
|
+
|
|
114
|
+
- GitHub: https://github.com/justrach/satya-zig
|
|
115
|
+
- PyPI: https://pypi.org/project/dhi/
|
dhi-1.0.0/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# dhi - Ultra-Fast Data Validation for Python
|
|
2
|
+
|
|
3
|
+
**The fastest data validation library for Python.** Powered by Zig for maximum performance.
|
|
4
|
+
|
|
5
|
+
## 🚀 Performance
|
|
6
|
+
|
|
7
|
+
**28 million validations/sec** - 3x faster than satya (Rust), 3x faster than msgspec (C)
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
# Validate 10,000 users in 0.36ms
|
|
11
|
+
from dhi import _dhi_native
|
|
12
|
+
|
|
13
|
+
users = [{"name": "Alice", "email": "alice@example.com", "age": 25}, ...]
|
|
14
|
+
|
|
15
|
+
field_specs = {
|
|
16
|
+
'name': ('string', 2, 100),
|
|
17
|
+
'email': ('email',),
|
|
18
|
+
'age': ('int_positive',),
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
results, valid_count = _dhi_native.validate_batch_direct(users, field_specs)
|
|
22
|
+
# 28M users/sec! 🔥
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## ✨ Features
|
|
26
|
+
|
|
27
|
+
- **�� Fastest**: 3x faster than satya (Rust) and msgspec (C)
|
|
28
|
+
- **🎯 24+ Validators**: Email, URL, UUID, IPv4, dates, numbers, strings
|
|
29
|
+
- **🔋 Zero Python Overhead**: C extension extracts directly from dicts
|
|
30
|
+
- **🌍 General Purpose**: Works with any dict structure
|
|
31
|
+
- **💪 Production Ready**: Thoroughly tested and benchmarked
|
|
32
|
+
|
|
33
|
+
## 📦 Installation
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install dhi
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## 🎯 Quick Start
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from dhi import _dhi_native
|
|
43
|
+
|
|
44
|
+
users = [
|
|
45
|
+
{"name": "Alice", "email": "alice@example.com", "age": 25},
|
|
46
|
+
{"name": "Bob", "email": "bob@example.com", "age": 30},
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
field_specs = {
|
|
50
|
+
'name': ('string', 2, 100),
|
|
51
|
+
'email': ('email',),
|
|
52
|
+
'age': ('int_positive',),
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
results, valid_count = _dhi_native.validate_batch_direct(users, field_specs)
|
|
56
|
+
print(f"Valid: {valid_count}/{len(users)}")
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## �� Available Validators
|
|
60
|
+
|
|
61
|
+
### String: `email`, `url`, `uuid`, `ipv4`, `base64`, `iso_date`, `iso_datetime`, `string`
|
|
62
|
+
### Number: `int`, `int_gt`, `int_gte`, `int_lt`, `int_lte`, `int_positive`, `int_non_negative`, `int_multiple_of`
|
|
63
|
+
|
|
64
|
+
## 🏆 Benchmarks
|
|
65
|
+
|
|
66
|
+
```
|
|
67
|
+
dhi: 28M users/sec 🥇
|
|
68
|
+
satya: 9M users/sec (3.0x slower)
|
|
69
|
+
msgspec: 9M users/sec (3.1x slower)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## 📝 License
|
|
73
|
+
|
|
74
|
+
MIT License - see LICENSE file
|
|
75
|
+
|
|
76
|
+
## 🔗 Links
|
|
77
|
+
|
|
78
|
+
- GitHub: https://github.com/justrach/satya-zig
|
|
79
|
+
- PyPI: https://pypi.org/project/dhi/
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""
|
|
2
|
+
dhi - High-performance data validation for Python, powered by Zig
|
|
3
|
+
|
|
4
|
+
A Python wrapper around satya-zig, providing blazing-fast validation
|
|
5
|
+
with a Pydantic-like API.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
__version__ = "0.1.0"
|
|
9
|
+
__author__ = "Rach Pradhan"
|
|
10
|
+
|
|
11
|
+
from .validator import (
|
|
12
|
+
BoundedInt,
|
|
13
|
+
BoundedString,
|
|
14
|
+
Email,
|
|
15
|
+
ValidationError,
|
|
16
|
+
ValidationErrors,
|
|
17
|
+
HAS_NATIVE_EXT,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
from .batch import (
|
|
21
|
+
BatchValidationResult,
|
|
22
|
+
validate_users_batch,
|
|
23
|
+
validate_ints_batch,
|
|
24
|
+
validate_strings_batch,
|
|
25
|
+
validate_emails_batch,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
# Try to import native extension
|
|
29
|
+
try:
|
|
30
|
+
from . import _dhi_native
|
|
31
|
+
except ImportError:
|
|
32
|
+
_dhi_native = None
|
|
33
|
+
|
|
34
|
+
__all__ = [
|
|
35
|
+
"BoundedInt",
|
|
36
|
+
"BoundedString",
|
|
37
|
+
"Email",
|
|
38
|
+
"ValidationError",
|
|
39
|
+
"ValidationErrors",
|
|
40
|
+
"HAS_NATIVE_EXT",
|
|
41
|
+
"_dhi_native",
|
|
42
|
+
# Batch validation
|
|
43
|
+
"BatchValidationResult",
|
|
44
|
+
"validate_users_batch",
|
|
45
|
+
"validate_ints_batch",
|
|
46
|
+
"validate_strings_batch",
|
|
47
|
+
"validate_emails_batch",
|
|
48
|
+
]
|
dhi-1.0.0/dhi/_native.c
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Native CPython extension for dhi
|
|
3
|
+
* Links against libsatya.dylib (Zig backend)
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
#define PY_SSIZE_T_CLEAN
|
|
7
|
+
#include <Python.h>
|
|
8
|
+
|
|
9
|
+
// External Zig functions from libsatya - COMPREHENSIVE VALIDATORS
|
|
10
|
+
// Basic validators
|
|
11
|
+
extern int satya_validate_int(long value, long min, long max);
|
|
12
|
+
extern int satya_validate_string_length(const char* str, size_t min_len, size_t max_len);
|
|
13
|
+
extern int satya_validate_email(const char* str);
|
|
14
|
+
|
|
15
|
+
// String validators (Zod-style)
|
|
16
|
+
extern int satya_validate_url(const char* str);
|
|
17
|
+
extern int satya_validate_uuid(const char* str);
|
|
18
|
+
extern int satya_validate_ipv4(const char* str);
|
|
19
|
+
extern int satya_validate_base64(const char* str);
|
|
20
|
+
extern int satya_validate_iso_date(const char* str);
|
|
21
|
+
extern int satya_validate_iso_datetime(const char* str);
|
|
22
|
+
extern int satya_validate_contains(const char* str, const char* substring);
|
|
23
|
+
extern int satya_validate_starts_with(const char* str, const char* prefix);
|
|
24
|
+
extern int satya_validate_ends_with(const char* str, const char* suffix);
|
|
25
|
+
|
|
26
|
+
// Number validators (Pydantic-style)
|
|
27
|
+
extern int satya_validate_int_gt(long value, long min);
|
|
28
|
+
extern int satya_validate_int_gte(long value, long min);
|
|
29
|
+
extern int satya_validate_int_lt(long value, long max);
|
|
30
|
+
extern int satya_validate_int_lte(long value, long max);
|
|
31
|
+
extern int satya_validate_int_positive(long value);
|
|
32
|
+
extern int satya_validate_int_non_negative(long value);
|
|
33
|
+
extern int satya_validate_int_negative(long value);
|
|
34
|
+
extern int satya_validate_int_non_positive(long value);
|
|
35
|
+
extern int satya_validate_int_multiple_of(long value, long divisor);
|
|
36
|
+
|
|
37
|
+
// Float validators
|
|
38
|
+
extern int satya_validate_float_gt(double value, double min);
|
|
39
|
+
extern int satya_validate_float_finite(double value);
|
|
40
|
+
|
|
41
|
+
// Python wrapper: validate_int(value, min, max) -> bool
|
|
42
|
+
static PyObject* py_validate_int(PyObject* self, PyObject* args) {
|
|
43
|
+
long value, min, max;
|
|
44
|
+
|
|
45
|
+
if (!PyArg_ParseTuple(args, "lll", &value, &min, &max)) {
|
|
46
|
+
return NULL;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
int result = satya_validate_int(value, min, max);
|
|
50
|
+
return PyBool_FromLong(result);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Python wrapper: validate_string_length(str, min_len, max_len) -> bool
|
|
54
|
+
static PyObject* py_validate_string_length(PyObject* self, PyObject* args) {
|
|
55
|
+
const char* str;
|
|
56
|
+
Py_ssize_t min_len, max_len;
|
|
57
|
+
|
|
58
|
+
if (!PyArg_ParseTuple(args, "snn", &str, &min_len, &max_len)) {
|
|
59
|
+
return NULL;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
int result = satya_validate_string_length(str, (size_t)min_len, (size_t)max_len);
|
|
63
|
+
return PyBool_FromLong(result);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Python wrapper: validate_email(str) -> bool
|
|
67
|
+
static PyObject* py_validate_email(PyObject* self, PyObject* args) {
|
|
68
|
+
const char* str;
|
|
69
|
+
|
|
70
|
+
if (!PyArg_ParseTuple(args, "s", &str)) {
|
|
71
|
+
return NULL;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
int result = satya_validate_email(str);
|
|
75
|
+
return PyBool_FromLong(result);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Validator type enum for fast dispatch
|
|
79
|
+
enum ValidatorType {
|
|
80
|
+
VAL_INT = 0,
|
|
81
|
+
VAL_INT_GT,
|
|
82
|
+
VAL_INT_GTE,
|
|
83
|
+
VAL_INT_LT,
|
|
84
|
+
VAL_INT_LTE,
|
|
85
|
+
VAL_INT_POSITIVE,
|
|
86
|
+
VAL_INT_NON_NEGATIVE,
|
|
87
|
+
VAL_INT_MULTIPLE_OF,
|
|
88
|
+
VAL_STRING,
|
|
89
|
+
VAL_EMAIL,
|
|
90
|
+
VAL_URL,
|
|
91
|
+
VAL_UUID,
|
|
92
|
+
VAL_IPV4,
|
|
93
|
+
VAL_BASE64,
|
|
94
|
+
VAL_ISO_DATE,
|
|
95
|
+
VAL_ISO_DATETIME,
|
|
96
|
+
VAL_UNKNOWN
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// Convert string to enum (do this ONCE, not per item!)
|
|
100
|
+
static enum ValidatorType parse_validator_type(const char* type_str) {
|
|
101
|
+
// Use first char for fast dispatch
|
|
102
|
+
switch (type_str[0]) {
|
|
103
|
+
case 'i':
|
|
104
|
+
if (strcmp(type_str, "int") == 0) return VAL_INT;
|
|
105
|
+
if (strcmp(type_str, "int_gt") == 0) return VAL_INT_GT;
|
|
106
|
+
if (strcmp(type_str, "int_gte") == 0) return VAL_INT_GTE;
|
|
107
|
+
if (strcmp(type_str, "int_lt") == 0) return VAL_INT_LT;
|
|
108
|
+
if (strcmp(type_str, "int_lte") == 0) return VAL_INT_LTE;
|
|
109
|
+
if (strcmp(type_str, "int_positive") == 0) return VAL_INT_POSITIVE;
|
|
110
|
+
if (strcmp(type_str, "int_non_negative") == 0) return VAL_INT_NON_NEGATIVE;
|
|
111
|
+
if (strcmp(type_str, "int_multiple_of") == 0) return VAL_INT_MULTIPLE_OF;
|
|
112
|
+
if (strcmp(type_str, "ipv4") == 0) return VAL_IPV4;
|
|
113
|
+
if (strcmp(type_str, "iso_date") == 0) return VAL_ISO_DATE;
|
|
114
|
+
if (strcmp(type_str, "iso_datetime") == 0) return VAL_ISO_DATETIME;
|
|
115
|
+
break;
|
|
116
|
+
case 's':
|
|
117
|
+
if (strcmp(type_str, "string") == 0) return VAL_STRING;
|
|
118
|
+
break;
|
|
119
|
+
case 'e':
|
|
120
|
+
if (strcmp(type_str, "email") == 0) return VAL_EMAIL;
|
|
121
|
+
break;
|
|
122
|
+
case 'u':
|
|
123
|
+
if (strcmp(type_str, "url") == 0) return VAL_URL;
|
|
124
|
+
if (strcmp(type_str, "uuid") == 0) return VAL_UUID;
|
|
125
|
+
break;
|
|
126
|
+
case 'b':
|
|
127
|
+
if (strcmp(type_str, "base64") == 0) return VAL_BASE64;
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
return VAL_UNKNOWN;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Field spec with pre-parsed validator type AND cached PyObject
|
|
134
|
+
struct FieldSpec {
|
|
135
|
+
PyObject* field_name_obj; // Cached PyObject* for fast dict lookup
|
|
136
|
+
const char* field_name;
|
|
137
|
+
enum ValidatorType validator_type;
|
|
138
|
+
long param1;
|
|
139
|
+
long param2;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// OPTIMIZED: validate_batch_direct with enum dispatch
|
|
143
|
+
static PyObject* py_validate_batch_direct(PyObject* self, PyObject* args) {
|
|
144
|
+
PyObject* items_list;
|
|
145
|
+
PyObject* field_specs_dict;
|
|
146
|
+
|
|
147
|
+
if (!PyArg_ParseTuple(args, "O!O!",
|
|
148
|
+
&PyList_Type, &items_list,
|
|
149
|
+
&PyDict_Type, &field_specs_dict)) {
|
|
150
|
+
return NULL;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
Py_ssize_t count = PyList_Size(items_list);
|
|
154
|
+
if (count == 0) {
|
|
155
|
+
return Py_BuildValue("([]i)", 0);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Pre-process field specs (convert strings to enums ONCE!)
|
|
159
|
+
Py_ssize_t num_fields = PyDict_Size(field_specs_dict);
|
|
160
|
+
struct FieldSpec* field_specs = malloc(num_fields * sizeof(struct FieldSpec));
|
|
161
|
+
if (!field_specs) {
|
|
162
|
+
return PyErr_NoMemory();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
PyObject *field_name, *spec;
|
|
166
|
+
Py_ssize_t pos = 0;
|
|
167
|
+
Py_ssize_t field_idx = 0;
|
|
168
|
+
|
|
169
|
+
while (PyDict_Next(field_specs_dict, &pos, &field_name, &spec)) {
|
|
170
|
+
field_specs[field_idx].field_name_obj = field_name; // Cache PyObject* (borrowed ref)
|
|
171
|
+
field_specs[field_idx].field_name = PyUnicode_AsUTF8(field_name);
|
|
172
|
+
|
|
173
|
+
if (PyTuple_Check(spec) && PyTuple_Size(spec) >= 1) {
|
|
174
|
+
const char* type_str = PyUnicode_AsUTF8(PyTuple_GET_ITEM(spec, 0));
|
|
175
|
+
field_specs[field_idx].validator_type = parse_validator_type(type_str);
|
|
176
|
+
|
|
177
|
+
// Extract params (do this once, not per item!)
|
|
178
|
+
field_specs[field_idx].param1 = 0;
|
|
179
|
+
field_specs[field_idx].param2 = 0;
|
|
180
|
+
if (PyTuple_Size(spec) >= 2) {
|
|
181
|
+
field_specs[field_idx].param1 = PyLong_AsLong(PyTuple_GET_ITEM(spec, 1));
|
|
182
|
+
}
|
|
183
|
+
if (PyTuple_Size(spec) >= 3) {
|
|
184
|
+
field_specs[field_idx].param2 = PyLong_AsLong(PyTuple_GET_ITEM(spec, 2));
|
|
185
|
+
}
|
|
186
|
+
} else {
|
|
187
|
+
field_specs[field_idx].validator_type = VAL_UNKNOWN;
|
|
188
|
+
}
|
|
189
|
+
field_idx++;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Allocate results array
|
|
193
|
+
unsigned char* results = malloc(count * sizeof(unsigned char));
|
|
194
|
+
if (!results) {
|
|
195
|
+
free(field_specs);
|
|
196
|
+
return PyErr_NoMemory();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Initialize all as valid
|
|
200
|
+
for (Py_ssize_t i = 0; i < count; i++) {
|
|
201
|
+
results[i] = 1;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
size_t valid_count = count;
|
|
205
|
+
|
|
206
|
+
// Iterate through each item and validate all fields (OPTIMIZED with enum dispatch)
|
|
207
|
+
for (Py_ssize_t i = 0; i < count; i++) {
|
|
208
|
+
PyObject* item = PyList_GET_ITEM(items_list, i); // Borrowed ref
|
|
209
|
+
|
|
210
|
+
// Prefetch next item for better cache performance
|
|
211
|
+
if (i + 1 < count) {
|
|
212
|
+
__builtin_prefetch(PyList_GET_ITEM(items_list, i + 1), 0, 3);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Fast dict check with branch prediction hint (usually true)
|
|
216
|
+
if (__builtin_expect(!PyDict_Check(item), 0)) {
|
|
217
|
+
free(field_specs);
|
|
218
|
+
free(results);
|
|
219
|
+
PyErr_SetString(PyExc_TypeError, "Expected list of dicts");
|
|
220
|
+
return NULL;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Iterate through pre-parsed field specs (ULTRA-FAST: use cached PyObject*)
|
|
224
|
+
for (Py_ssize_t f = 0; f < num_fields; f++) {
|
|
225
|
+
// Use PyDict_GetItem with cached PyObject* - FASTEST (borrowed ref, no refcount overhead)
|
|
226
|
+
PyObject* field_value = PyDict_GetItem(item, field_specs[f].field_name_obj);
|
|
227
|
+
|
|
228
|
+
if (!field_value) {
|
|
229
|
+
// Missing field
|
|
230
|
+
if (results[i] == 1) {
|
|
231
|
+
results[i] = 0;
|
|
232
|
+
valid_count--;
|
|
233
|
+
}
|
|
234
|
+
break; // Missing field, skip remaining validations
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Fast dispatch using switch/case (NO string comparisons!)
|
|
238
|
+
int is_valid = 1;
|
|
239
|
+
|
|
240
|
+
switch (field_specs[f].validator_type) {
|
|
241
|
+
case VAL_INT: {
|
|
242
|
+
long value = PyLong_AsLong(field_value);
|
|
243
|
+
is_valid = satya_validate_int(value, field_specs[f].param1, field_specs[f].param2);
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
case VAL_INT_GT: {
|
|
247
|
+
long value = PyLong_AsLong(field_value);
|
|
248
|
+
is_valid = satya_validate_int_gt(value, field_specs[f].param1);
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
case VAL_INT_GTE: {
|
|
252
|
+
long value = PyLong_AsLong(field_value);
|
|
253
|
+
is_valid = satya_validate_int_gte(value, field_specs[f].param1);
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
case VAL_INT_LT: {
|
|
257
|
+
long value = PyLong_AsLong(field_value);
|
|
258
|
+
is_valid = satya_validate_int_lt(value, field_specs[f].param1);
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
case VAL_INT_LTE: {
|
|
262
|
+
long value = PyLong_AsLong(field_value);
|
|
263
|
+
is_valid = satya_validate_int_lte(value, field_specs[f].param1);
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
case VAL_INT_POSITIVE: {
|
|
267
|
+
long value = PyLong_AsLong(field_value);
|
|
268
|
+
is_valid = satya_validate_int_positive(value);
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
case VAL_INT_NON_NEGATIVE: {
|
|
272
|
+
long value = PyLong_AsLong(field_value);
|
|
273
|
+
is_valid = satya_validate_int_non_negative(value);
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
case VAL_INT_MULTIPLE_OF: {
|
|
277
|
+
long value = PyLong_AsLong(field_value);
|
|
278
|
+
is_valid = satya_validate_int_multiple_of(value, field_specs[f].param1);
|
|
279
|
+
break;
|
|
280
|
+
}
|
|
281
|
+
case VAL_STRING: {
|
|
282
|
+
const char* value = PyUnicode_AsUTF8(field_value);
|
|
283
|
+
is_valid = satya_validate_string_length(value, (size_t)field_specs[f].param1, (size_t)field_specs[f].param2);
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
case VAL_EMAIL: {
|
|
287
|
+
const char* value = PyUnicode_AsUTF8(field_value);
|
|
288
|
+
is_valid = satya_validate_email(value);
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
case VAL_URL: {
|
|
292
|
+
const char* value = PyUnicode_AsUTF8(field_value);
|
|
293
|
+
is_valid = satya_validate_url(value);
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
case VAL_UUID: {
|
|
297
|
+
const char* value = PyUnicode_AsUTF8(field_value);
|
|
298
|
+
is_valid = satya_validate_uuid(value);
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
case VAL_IPV4: {
|
|
302
|
+
const char* value = PyUnicode_AsUTF8(field_value);
|
|
303
|
+
is_valid = satya_validate_ipv4(value);
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
case VAL_BASE64: {
|
|
307
|
+
const char* value = PyUnicode_AsUTF8(field_value);
|
|
308
|
+
is_valid = satya_validate_base64(value);
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
case VAL_ISO_DATE: {
|
|
312
|
+
const char* value = PyUnicode_AsUTF8(field_value);
|
|
313
|
+
is_valid = satya_validate_iso_date(value);
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
case VAL_ISO_DATETIME: {
|
|
317
|
+
const char* value = PyUnicode_AsUTF8(field_value);
|
|
318
|
+
is_valid = satya_validate_iso_datetime(value);
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
case VAL_UNKNOWN:
|
|
322
|
+
default:
|
|
323
|
+
is_valid = 1; // Skip unknown validators
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// Update result if invalid (FAST: branch prediction - valid is common case)
|
|
328
|
+
if (__builtin_expect(!is_valid, 0)) { // Hint: validation usually succeeds
|
|
329
|
+
if (results[i] == 1) {
|
|
330
|
+
results[i] = 0;
|
|
331
|
+
valid_count--;
|
|
332
|
+
}
|
|
333
|
+
break; // Already invalid, skip remaining validations
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// Convert results to Python list (FAST: use singleton bools, no allocations!)
|
|
339
|
+
PyObject* result_list = PyList_New(count);
|
|
340
|
+
for (Py_ssize_t i = 0; i < count; i++) {
|
|
341
|
+
PyObject* bool_obj = results[i] ? Py_True : Py_False;
|
|
342
|
+
Py_INCREF(bool_obj); // Must incref singleton
|
|
343
|
+
PyList_SET_ITEM(result_list, i, bool_obj);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// Cleanup
|
|
347
|
+
free(field_specs);
|
|
348
|
+
free(results);
|
|
349
|
+
|
|
350
|
+
// Return (results, valid_count)
|
|
351
|
+
return Py_BuildValue("(Ni)", result_list, (Py_ssize_t)valid_count);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Method definitions
|
|
355
|
+
static PyMethodDef DhiNativeMethods[] = {
|
|
356
|
+
{"validate_int", py_validate_int, METH_VARARGS,
|
|
357
|
+
"Validate integer bounds (value, min, max) -> bool"},
|
|
358
|
+
{"validate_string_length", py_validate_string_length, METH_VARARGS,
|
|
359
|
+
"Validate string length (str, min_len, max_len) -> bool"},
|
|
360
|
+
{"validate_email", py_validate_email, METH_VARARGS,
|
|
361
|
+
"Validate email format (str) -> bool"},
|
|
362
|
+
{"validate_batch_direct", py_validate_batch_direct, METH_VARARGS,
|
|
363
|
+
"GENERAL batch validation: (items, field_specs) -> (list[bool], int)"},
|
|
364
|
+
{NULL, NULL, 0, NULL}
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
// Module definition
|
|
368
|
+
static struct PyModuleDef dhi_native_module = {
|
|
369
|
+
PyModuleDef_HEAD_INIT,
|
|
370
|
+
"_dhi_native",
|
|
371
|
+
"Native Zig validators for dhi (CPython extension)",
|
|
372
|
+
-1,
|
|
373
|
+
DhiNativeMethods
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
// Module initialization
|
|
377
|
+
PyMODINIT_FUNC PyInit__dhi_native(void) {
|
|
378
|
+
return PyModule_Create(&dhi_native_module);
|
|
379
|
+
}
|