Skip to main content

py-mock

pip install py-mock

py-mock 介绍

py-mock 移植了 Mock.js、better-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': '卢强'}}

更多示例

Metadata

Release files for py-mock 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-mock 1.2.1
File Size Uploaded
py-mock-1.2.1.tar.gz 52.8 kB Details

Release files / py-mock-1.2.1.tar.gz

Download URL py-mock-1.2.1.tar.gz
Size 52.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2d51074192ba278637b9b82b5f46a783826384d99ebac48f60884f4da23c4cf2
BLAKE2b-256 checksum
How to use checksums
99edd863568f2135393719cad38f72e610c9ee8fe4183a4a44ce578736c15541
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

1.2.1 This release

1 release file

1.2.0

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page