Skip to main content

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.

Usage in Specs

You can use the xeger as a type in your datacraft data specs. See example:

{
  "ssn":{
    "type": "xeger",
    "data": "\\d{3]-\\d{2}-\\d{4}"
  }
}
$ datacraft -s xeger.json -i 3 --format json-pretty -x -l error
[
    {
        "ssn": "322-81-1469"
    },
    {
        "ssn": "697-21-8178"
    },
    {
        "ssn": "340-78-5377"
    }
]

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


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.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

datacraft_xeger-0.1.1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file datacraft-xeger-0.1.1.tar.gz.

File metadata

  • Download URL: datacraft-xeger-0.1.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.2

File hashes

Hashes for datacraft-xeger-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e1d3aa9370da85e9729f2ff844d7f7ee649c28ef78df284b982d3cbd40f3379f
MD5 2fe030563800e69610aa22e234a47bcf
BLAKE2b-256 cebe3a7aa6e619cde5c000dad12c17c946a8b5319a2eeb32b88c0419dfe1dbce

See more details on using hashes here.

File details

Details for the file datacraft_xeger-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: datacraft_xeger-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.2

File hashes

Hashes for datacraft_xeger-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ec36abedcce8814acce9ea5b1ccc0dc7718e282a72b8803172b62811b0f1adbb
MD5 3d5ad7afeb08cad84883579cf3a4fe0e
BLAKE2b-256 6d054d4539b2ed4be186f1113c249f9489b16271a01ade03fc2e4b08c2d8b0d8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page