Skip to main content

字符串标记解析器

项目描述

令牌解析器

我经常发现自己创建脚本来生成测试数据,而这些脚本总是有某种令牌解析器。所以我决定以更可重用的形式创建这个脚本。

这有什么用例?

通常我创建一个json模板,然后生成一批测试数据。json 将作为我的测试项目的主体。虽然此脚本将为每个实例提供随机数据。

安装

pip install token-parser

你可以在PyPi上查看这个项目。

这个 parse_token 函数能做什么?

  1. 获取当前时间:“$now()”
  2. 获取当前时间(UTC):“$utcNow()”
  3. 将时间戳转换为日期时间:“$now('2021-12-31T23:59:59.000')”
  4. 将时间戳转换为日期时间 (UTC):“$now('2021-12-31T23:59:59.000Z')”
  5. 操作日期时间字符串并生成日期时间对象:“$dateAdd('2021-12-31T23:59:59.000Z', {'minute': 10})”
  6. 将字符串转换为 int:“$int(1)”
  7. 将字符串转换为浮点数:“$float(1.5)”
  8. 生成一个随机整数:"$int(0, 10)" ($int(min, max))
  9. 生成随机浮点数:"$float(0.5, 42.7)" ($float(min, max))
  10. 从列表中随机选择一个 int:"$int(1,2,3,4,5,6,7)"
  11. 从列表中随机选择一个浮点数:“$float(1,2,3,4,5,6,7)"
  12. 生成顺序 int(正负):“$inc()”/“$dec()”
  13. 生成增量(按 N)int(正负):“$inc(5)”/“$dec(5)”
  14. 生成 uuid4 字符串(整个会话或个人唯一):“$guid()”/“$guid(true)”
  15. 返回列表中的下一个元素。每个元素都将由 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'}
]

笔记

这是一个非常早期的项目,只有一些功能。如果您发现任何错误,请与我联系。

去做

  1. 制作一个像样的文档。
  2. 查看 $now 和 $utcNow。
  3. 添加 strict_mode,以在出现问题时停止所有处理。
  4. 支持嵌套令牌
  5. 为 $next 创建标识符,以便您可以在不同的迭代中同时使用相同的序列。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

token_parser-0.0.8.tar.gz (11.0 kB 查看哈希)

已上传 source

内置分布

token_parser-0.0.8-py3-none-any.whl (11.5 kB 查看哈希)

已上传 py3