pip install 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': '[email protected]', 'id': 1},
{'email': '[email protected]', 'id': 2},
{'email': '[email protected]', 'id': 3},
{'email': '[email protected]', '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': '[email protected]',
'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': '卢强'}}