Custom xeger type extensions for datacraft package
Project description
datacraft-xeger
Custom plugin for datacraft to generate values using regular expressions.
Uses the rstr package. xeger
is regex backwards. Inspiration from the
original Java Package xeger.
Custom Regex Types
Users can make use of the datacraft_xeger module to create custom datacraft value suppliers with regex patterns. The example below shows how to register custom types for different country phone number patterns.
import datacraft
import datacraft_xeger.suppliers as xeger
phone_patterns = {
# type_name: pattern
'uk-phone': r'\+44 \d{4} \d{6}',
'aus-phone': r'\+61 4\d{2} \d{3} \d{3}',
'nz-phone': r'\+64 \d{2} \d{4} \d{4}',
# ...
}
@datacraft.registry.types('uk-phone')
def _custom_regex_uk_phone(spec, loader):
return xeger.xeger_supplier(phone_patterns['uk-phone'])
@datacraft.registry.types('aus-phone')
def _custom_regex_aus_phone(spec, loader):
return xeger.xeger_supplier(phone_patterns['aus-phone'])
@datacraft.registry.types('nz-phone')
def _custom_regex_nz_phone(spec, loader):
return xeger.xeger_supplier(phone_patterns['nz-phone'])
Once registered these types can be used as part of the data generation process. See the example data spec:
{
"name": ["ann", "bob", "carl"],
"age": { "type": "rand_int_range", "data": [25, 75]},
"phone": {
"type": "weighted_ref",
"data": {
"UK": 0.5, "AUS": 0.3, "NZ": 0.2
}
},
"refs": {
"UK": { "type": "uk-phone" },
"AUS": { "type": "aus-phone" },
"NZ": { "type": "nz-phone" }
}
}
Running datacraft against this spec and using the custom code loading feature:
datacraft -s custom.json -c custom.py -i 3 --format json-pretty -x -l warn
[
{
"name": "ann",
"age": 67,
"phone": "+64 07 2500 7403"
},
{
"name": "bob",
"age": 49,
"phone": "+61 435 126 947"
},
{
"name": "carl",
"age": 61,
"phone": "+44 7693 148185"
}
]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
datacraft-xeger-0.1.0.tar.gz
(3.6 kB
view hashes)
Built Distribution
Close
Hashes for datacraft_xeger-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5334024011f720ce6c700b5ee00dfa8c2cc2be316021bde93cd955eb3feb3c47 |
|
MD5 | 0130ba73c2c4cb1fc5ae45538068914d |
|
BLAKE2b-256 | 87a656867f42d52ee80bb12fddcd073fe1da860c03dee85cea9e4e87abec45f8 |