Skip to main content

Mock.js for Python

Project description

pymock

pip install py-mock

pymock 介绍

pymock 移植了 Mock.jsbetter-mock的功能到 Python,如果你熟悉 Mock .js 的模板语法, 那么在 Python 中也能轻而易举地 Mock 出你想要的数据,pymock 100% 兼容 Mock.js。

一些说明

实际上 pymock 是借助了 py_mini_racer 来运行 Mock.js 中的 mock 函数,且仅移植了 Mock.mock 方法,如果有问题可以在 github 上给我提 issue。

使用示例

from pprint import pprint

from pymock import Mock

Mock = Mock()

pprint(Mock.mock({
    'list|1-10': [{
        'id|+1': 1,
        'email': '@EMAIL'
    }]
}))
{'list': [{'email': 'n.metv@mddwjpjxo.cf', 'id': 1},
          {'email': 'e.vseuqc@viiuxwde.biz', 'id': 2},
          {'email': 'v.eoje@mklgh.ba', 'id': 3},
          {'email': 'm.xobzjwhegf@hsclkd.uk', 'id': 4}]}
pprint(Mock.mock(Mock.mock({
    'number1|1-100.1-10': 1,
    'number2|123.1-10': 1,
    'number3|123.3': 1,
    'number4|123.10': 1.123
})))
{'number1': 56.5787,
 'number2': 123.14013355,
 'number3': 123.695,
 'number4': 123.1236478526}
pprint(Mock.mock({
    'regexp1': r'/[a-z][A-Z][0-9]/',
    'regexp2': r'/\w\W\s\S\d\D/',
    'regexp3': r'/\d{5,10}/'
}))
{'regexp1': 'mM8', 'regexp2': 'I;\rI5j', 'regexp3': '575824'}
pprint(Mock.mock({
        'name': {
            'first': '@first',
            'middle': '@first',
            'last': '@last',
            'email': 'example\\@gmail.com',
            'full': '@first @middle @last'
        }
    }
))
{'name': {'email': 'example@gmail.com',
          'first': 'Nancy',
          'full': 'Nancy Nancy Lee',
          'last': 'Lee',
          'middle': 'Nancy'}}
# JS object like string
pprint(Mock.mock(
    """{
        name: {
            first: '@cfirst',
            middle: '@cfirst',
            last: '@clast',
            email: 'example\\@gmail.com',
            full: '@first@middle@last'
        }
    }"""
))
{'name': {'email': 'example@gmail.com',
          'first': '萧',
          'full': '萧白强',
          'last': '强',
          'middle': '白'}}

更多示例

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

py-mock-1.0.0.tar.gz (47.3 kB view hashes)

Uploaded Source

Supported by

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