字符串标记解析器
项目描述
令牌解析器
我经常发现自己创建脚本来生成测试数据,而这些脚本总是有某种令牌解析器。所以我决定以更可重用的形式创建这个脚本。
这有什么用例?
通常我创建一个json模板,然后生成一批测试数据。json 将作为我的测试项目的主体。虽然此脚本将为每个实例提供随机数据。
安装
pip install token-parser
你可以在PyPi上查看这个项目。
这个 parse_token 函数能做什么?
- 获取当前时间:“$now()”
- 获取当前时间(UTC):“$utcNow()”
- 将时间戳转换为日期时间:“$now('2021-12-31T23:59:59.000')”
- 将时间戳转换为日期时间 (UTC):“$now('2021-12-31T23:59:59.000Z')”
- 操作日期时间字符串并生成日期时间对象:“$dateAdd('2021-12-31T23:59:59.000Z', {'minute': 10})”
- 将字符串转换为 int:“$int(1)”
- 将字符串转换为浮点数:“$float(1.5)”
- 生成一个随机整数:"$int(0, 10)" ($int(min, max))
- 生成随机浮点数:"$float(0.5, 42.7)" ($float(min, max))
- 从列表中随机选择一个 int:"$int(1,2,3,4,5,6,7)"
- 从列表中随机选择一个浮点数:“$float(1,2,3,4,5,6,7)"
- 生成顺序 int(正负):“$inc()”/“$dec()”
- 生成增量(按 N)int(正负):“$inc(5)”/“$dec(5)”
- 生成 uuid4 字符串(整个会话或个人唯一):“$guid()”/“$guid(true)”
- 返回列表中的下一个元素。每个元素都将由 python 解析。到达结束时,会重新开始: "$next(['foo', 'bar'])" / "$next([1,2,3,4])" 15.1 参数必须唯一,否则会共享相同的序列。
例子
这是一个例子。更多在示例文件夹中
考虑以下 JSON 模板:
{
"id": "$inc()",
"testSession": "$guid(true)",
"name": "Dolly",
"age": "$int(18, 42)",
"score": "$float(150, 9999)",
"status": "$int(1,2,3,4,5)",
"generationDate": "$utcNow()"
}
from token_parser.parsers import parse_token
# Helper variables with ISO8601 datetime format (utc and local)
ISO8601_DATE_FORMAT_UTC = "%Y-%m-%dT%H:%M:%S.%fZ"
ISO8601_DATE_FORMAT_LOCAL = ISO8601_DATE_FORMAT_UTC[:-1]
# Storing test items here
test_items = []
# Converting the string JSON to a dictionary (You could use json.load and get it from a file)
template = ast.literal_eval(JSON_TEMPLATE)
# Getting a initial datetime
test_data_creation_start = datetime.utcnow()
# Each datetime will be incremented by 15 minutes
created_at_delay = {"minutes": 15}
# I will create 50 test items
for i in range(50):
# Current test item
test_item = {}
for key, item in template.items():
# Parsing each token and adding it to the current item.
test_item[key] = parse_token(item)
# Adding an extra key named 'createdAt', which will be the initial date + 15 minutes
test_item["createdAt"] = parse_token(
f"$dateAdd({test_data_creation_start.strftime(ISO8601_DATE_FORMAT_UTC)}, {created_at_delay})")
# overwriting the initial datetime, so it will be incremented each time
test_data_creation_start = test_item["createdAt"]
# adding test item to the list
test_items.append(test_item)
# printing output
pprint(test_items)
输出将是:
[
{'age': 33,
'createdAt': datetime.datetime(2021, 7, 1, 1, 22, 38, 183365, tzinfo=<UTC>),
'generationDate': datetime.datetime(2021, 7, 1, 4, 7, 38, 183365, tzinfo=<UTC>),
'id': 1,
'name': 'Dolly',
'score': 9631.926551796414,
'status': 5,
'testSession': 'f7607f8d-774b-4823-8d47-91c3db056e73'},
{'age': 35,
'createdAt': datetime.datetime(2021, 7, 1, 1, 37, 38, 183365, tzinfo=<UTC>),
'generationDate': datetime.datetime(2021, 7, 1, 4, 7, 38, 186086, tzinfo=<UTC>),
'id': 2,
'name': 'Dolly',
'score': 5486.760170377791,
'status': 5,
'testSession': 'f7607f8d-774b-4823-8d47-91c3db056e73'},
{'age': 36,
'createdAt': datetime.datetime(2021, 7, 1, 1, 52, 38, 183365, tzinfo=<UTC>),
'generationDate': datetime.datetime(2021, 7, 1, 4, 7, 38, 186086, tzinfo=<UTC>),
'id': 3,
'name': 'Dolly',
'score': 383.9861640547723,
'status': 1,
'testSession': 'f7607f8d-774b-4823-8d47-91c3db056e73'},
{'age': 28,
'createdAt': datetime.datetime(2021, 7, 1, 2, 7, 38, 183365, tzinfo=<UTC>),
'generationDate': datetime.datetime(2021, 7, 1, 4, 7, 38, 186086, tzinfo=<UTC>),
'id': 4,
'name': 'Dolly',
'score': 6644.5243644456095,
'status': 2,
'testSession': 'f7607f8d-774b-4823-8d47-91c3db056e73'},
...
{'age': 28,
'createdAt': datetime.datetime(2021, 7, 1, 13, 22, 38, 183365, tzinfo=<UTC>),
'generationDate': datetime.datetime(2021, 7, 1, 4, 7, 38, 190470, tzinfo=<UTC>),
'id': 49,
'name': 'Dolly',
'score': 1584.2818807777992,
'status': 3,
'testSession': 'f7607f8d-774b-4823-8d47-91c3db056e73'},
{'age': 18,
'createdAt': datetime.datetime(2021, 7, 1, 13, 37, 38, 183365, tzinfo=<UTC>),
'generationDate': datetime.datetime(2021, 7, 1, 4, 7, 38, 190470, tzinfo=<UTC>),
'id': 50,
'name': 'Dolly',
'score': 6704.023925214241,
'status': 4,
'testSession': 'f7607f8d-774b-4823-8d47-91c3db056e73'}
]
笔记
这是一个非常早期的项目,只有一些功能。如果您发现任何错误,请与我联系。
去做
- 制作一个像样的文档。
- 查看 $now 和 $utcNow。
- 添加 strict_mode,以在出现问题时停止所有处理。
- 支持嵌套令牌
- 为 $next 创建标识符,以便您可以在不同的迭代中同时使用相同的序列。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。
源分布
token_parser-0.0.8.tar.gz
(11.0 kB
查看哈希)
内置分布
token_parser-0.0.8-py3-none-any.whl
(11.5 kB
查看哈希)
关
token_parser -0.0.8.tar.gz 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 8488fe6d4aa573ccd9e5e0935809afc08f50553464da85ec88d7725f7d1d7323 |
|
| MD5 | 774fa6f2b3d3782c20ea4f591d4a7f73 |
|
| 布莱克2-256 | f02f7f83dcdb8b3c9cd249f4b6f4907e947af208615a631d78bbb1d1118f8867 |
关
token_parser -0.0.8-py3-none-any.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | af5f720a9216bd0be84d0ed1e5086d184c22f4164f0b0006167213b88e5f1e5d |
|
| MD5 | db7e6c5bff10ebd2a87e67a57d735c95 |
|
| 布莱克2-256 | e0b1fada68d801e11b0b3bd09442e47f5c37681d99f0ada9271450688d1a89d8 |