Giter Club home page Giter Club logo

fakergen's Introduction

Fakergen

Generate JSON/YAML/XML... mock data with a structured template using Faker under the hood.

Table of Contents

Features

  • Human-readable syntax as template
  • Fully support Faker providers
  • Customizable extended functions
  • Support any file formats: JSON, YAML, XML...
  • CLI script, easy to be integrated into any CI process
  • Support seed value for generating same output data

Dependency

$ pip install Faker

Usage

usage: fakergen.py [-h] [-q] [-s SEED] template

positional arguments:
  template              template file

optional arguments:
  -h, --help            show this help message and exit
  -q, --quiet           be quiet, no error message
  -s SEED, --seed SEED  set seed value
  • Generate JSON data using default ./template.json:
$ cat ./template.json
[
  {
    "product_number": "{{bothify(text='????-########', letters='ABCDE')}}",
    "hostname": "{{hostname(2)}}",
    "attributes": [
      {
        "text": "static text",
        "ean": "{{ean13(leading_zero=False)}}",
        "random_number": {{pyint(min_value=1, max_value=999, step=1)}},
        "colors": "{{color_name()}} {{color_name()}}",
        "custom_greeting": "{{greeting()}}"
      }
    ]
  }
]

$ ./fakergen.py template.json
[
  {
    "product_number": "CEAD-00367795",
    "hostname": "lt-75.collins.rivera-riley.com",
    "attributes": [
      {
        "text": "static text",
        "ean": "7263776026664",
        "random_number": 65,
        "colors": "OldLace Tomato",
        "custom_greeting": "Hi there"
      }
    ]
  }
]

How to create a Faker template?

Faker template is basically a file with variables inside. As an example, ./template.json shows briefly how a template looks like: Using {{...}} to surround Faker provider name or custom function name will indicate the function to be executed as a Faker provider or a python function.

A list of Faker providers can be found here.

How to create and use custom function?

The preprepared CustomProviders class is located in ./customprovider.py. Firstly, add a function inside this class. Then this custom function can be called from Faker template.

Checkout greeting() function as an example.

How to repeat certain elements?

  • One solution is to generate a new JSON template with repeated elements. Using jq can simply do the job. For example, repeat attributes:
jq '.[].attributes += $el' --argjson el "$(jq '.[].attributes' template.json)" template.json > newtemplate.json

If more repeats are needed, for loop is helpful. For example, repeat attributes 5 times:

j="$(cat template.json)"; for ((i=0;i<5;i++)); do j="$(jq '.[].attributes += $el' --argjson el "$(jq '.[].attributes' template.json)" <<< "$j")"; done; echo "$j" > newtemplate.json

To generate YAML/XML template, similar to jq, yq is recommended.

Credits

Inspired by JSON Generator


Buy Me A Coffee

fakergen's People

Contributors

kevcui avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.