Files
pydantic/benchmarks/test_voluptuous.py
T
Stephen Brown II 938335c46f Add benchmark for voluptuous library (#1292)
Voluptuous, despite the name, is a Python data validation library.

- GitHub: https://github.com/alecthomas/voluptuous
- PyPi: https://pypi.org/project/voluptuous

Package | Version | Relative Performance | Mean validation time
--- | --- | --- | ---
valideer | `0.4.2` |  | 104.2μs
attrs + cattrs | `19.3.0` | 1.1x slower | 114.4μs
pydantic | `1.4a1` | 1.2x slower | 124.3μs
marshmallow | `3.5.0` | 1.8x slower | 190.1μs
voluptuous | `0.11.7` | 2.2x slower | 227.6μs
trafaret | `2.0.2` | 2.4x slower | 253.0μs
django-rest-framework | `3.11.0` | 8.5x slower | 881.8μs
cerberus | `1.3.2` | 19.1x slower | 1993.8μs
2020-03-06 13:31:43 +00:00

52 lines
2.0 KiB
Python

from dateutil.parser import parse as parse_datetime
import voluptuous as v
from voluptuous.humanize import humanize_error
class TestVoluptuous:
package = 'voluptuous'
version = v.__version__
def __init__(self, allow_extra):
self.schema = v.Schema(
{
v.Required('id'): int,
v.Required('client_name'): v.All(str, v.Length(max=255)),
v.Required('sort_index'): float,
# v.Optional('client_email'): v.Maybe(v.Email),
v.Optional('client_phone'): v.Maybe(v.All(str, v.Length(max=255))),
v.Optional('location'): v.Maybe(
v.Schema(
{
'latitude': v.Maybe(float),
'longitude': v.Maybe(float)
},
required=True
)
),
v.Optional('contractor'): v.Maybe(v.All(v.Coerce(int), v.Range(min=1))),
v.Optional('upstream_http_referrer'): v.Maybe(v.All(str, v.Length(max=1023))),
v.Required('grecaptcha_response'): v.All(str, v.Length(min=20, max=1000)),
v.Optional('last_updated'): v.Maybe(parse_datetime),
v.Required('skills', default=[]): [
v.Schema(
{
v.Required('subject'): str,
v.Required('subject_id'): int,
v.Required('category'): str,
v.Required('qual_level'): str,
v.Required('qual_level_id'): int,
v.Required('qual_level_ranking', default=0): float,
}
)
],
},
extra=allow_extra,
)
def validate(self, data):
try:
return True, self.schema(data)
except v.MultipleInvalid as e:
return False, humanize_error(data, e)