Skip to main content

Mock.js for Python3

Project description

py-mock

pip install py-mock

py-mock 介绍

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

一些说明

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

使用示例

from pprint import pprint

from pymock import Mock

Mock = Mock()

pprint(Mock.mock('@csentence 变量示例'))
'何思许型面率儿相算加阶角难角看有资。 变量示例'
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}/',
    'regexp4': r'\/\d{5,10}\/'  # output raw regexp with escape slash
}))
{'regexp1': 'xP9',
 'regexp2': 'B \xa0V7O',
 'regexp3': '98356203',
 'regexp4': '/\\d{5,10}/'}
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'}}
pprint(Mock.mock({
    'random': {
        'name': [
            '@name',
            '@cname'
        ],
        'image': [
            # @image( size, background, foreground, format, text )
            "@image",
            "@image('300x400')",
            "@image('300x400', '占位图文字')",
            "@image('300x400', '#234567', '#FFFFFF', 'png', 'HelloWorld')"
        ],
        'emoji': [
            # emoji( pool, min, max )
            "@emoji",
            "@emoji('😀😁😂😃😄')",
            "@emoji(3, 6)",
            "@emoji('123🌘😷🙊★♠♫', 3, 6)"
        ]
    }
}
))
{'random': {'emoji': ['🌛', '😀', '👲🌐👧🍢🌂🐁', '♫1★🙊'],
            'image': ['https://iph.href.lu/400x300?bg=&fg=&text=',
                      'https://iph.href.lu/300x400?bg=&fg=&text=',
                      'https://iph.href.lu/300x400?bg=&fg=&text=占位图文字',
                      'https://dummyimage.com/300x400/234567/FFFFFF.png?text=HelloWorld'],
            'name': ['Mary Thompson', '高刚']}}

You can also Mock form JSON string or JavaScript Object like-string

pprint(Mock.mock_js("""
{
    name: {
        first: "@cfirst", 
        last: "@clast",
        name: "@first@last",
    }
}
"""))
{'name': {'first': '卢', 'last': '强', 'name': '卢强'}}

更多示例

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

Uploaded Source

File details

Details for the file py-mock-1.2.1.tar.gz.

File metadata

  • Download URL: py-mock-1.2.1.tar.gz
  • Upload date:
  • Size: 52.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.9

File hashes

Hashes for py-mock-1.2.1.tar.gz
Algorithm Hash digest
SHA256 2d51074192ba278637b9b82b5f46a783826384d99ebac48f60884f4da23c4cf2
MD5 6a5825724674143a441592f9aa4d02ad
BLAKE2b-256 99edd863568f2135393719cad38f72e610c9ee8fe4183a4a44ce578736c15541

See more details on using hashes here.

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