根据意见的问题,何不干脆写一个固定长度的基于模板的模糊器是这样的:
#! /usr/bin/env python
"""Minimal template based dict string value fuzzer."""
from __future__ import print_function
import random
import string
def random_string(rng, length, chars=string.printable):
"""A random string with given length."""
return ''.join(rng.choice(chars) for _ in range(length))
def dict_string_template_fuzz_gen(rng, dict_in):
"""Given a random number generator rng, and starting from
template dict_in expected to have only strings as values,
this generator function yields derived dicts with random
variations in the string values keeping the length of
those identical."""
while True:
yield dict((k, random_string(rng, len(v))) for k, v in dict_in.items())
def main():
"""Drive a test run of minimal template fuzz."""
k1, k2, k3 = 'ka', 'kb', 'kc'
template = {k1: "aaa", k2: "bbb", k3: "ccc"}
print("# Input(template):")
print(template)
rng = random.SystemRandom()
print("# Output(fuzz):")
for n, fuzz in enumerate(dict_string_template_fuzz_gen(rng,
template), start=0):
print(fuzz)
if n > 3:
break
if __name__ == '__main__':
main()
在用例输入可能产生这样的:
# Input(template):
{'kc': 'ccc', 'kb': 'bbb', 'ka': 'aaa'}
# Output(fuzz):
{'kc': '6HZ', 'kb': 'zoD', 'ka': '5>b'}
{'kc': '%<\r', 'kb': 'g>v', 'ka': 'Mo0'}
{'kc': 'Y $', 'kb': '4z.', 'ka': '0".'}
{'kc': '^M.', 'kb': 'QY1', 'ka': 'P0)'}
{'kc': 'FK4', 'kb': 'oZW', 'ka': 'G1q'}
所以这应该给OP一些启动,因为它可能是一个引导问题,在那里Python的知识才刚刚开始......
我刚刚砍了它--PEP8兼容 - 无论是Python v2还是v3,它都应该可以工作。
许多开放的目标......但应该得到一个去评估,如果一个库或一些简单的增强编码可能就足够了。只有OP会知道但欢迎对这个答案建议发表评论或更新问题。提示:我几乎总是使用SystemRandom,因此您可以更稳健地进行并行处理。可能有更快的方法,但是在规范中我看不到性能。这些印刷品当然是被卷入的,因为这是最好的教育。 HTH
更新: 读了OP的评论只改变琴弦的一部分,保留一定的相似性,人们可以通过例如交换上述模糊器功能:
def dict_string_template_fuzz_len_gen(rng, dict_in, f_len=1):
"""Given a random number generator rng, and starting from
template dict_in expected to have only strings as values,
this generator function yields derived dicts with random
variations in the string values keeping the length of
those identical.
Added as hack the f_len parameter that counts the
characters open to be fuzzed from the end of the string."""
r_s = random_string # shorten for line readability below
while True:
yield dict(
(k, v[:f_len + 1] + r_s(rng, f_len)) for k, v in dict_in.items())
然后具有如输出样本:
# Input(template):
{'kc': 'ccc', 'kb': 'bbb', 'ka': 'aaa'}
# Output(fuzz):
{'kc': 'cc\t', 'kb': 'bbd', 'ka': 'aa\\'}
{'kc': 'cc&', 'kb': 'bbt', 'ka': 'aa\\'}
{'kc': 'ccg', 'kb': 'bb_', 'ka': 'aaJ'}
{'kc': 'ccc', 'kb': 'bbv', 'ka': 'aau'}
{'kc': 'ccw', 'kb': 'bbs', 'ka': "aa'"}
当调用此函数,而不是其它。
什么是你的字典的大小,你是什么意思的“一些价值”?为什么不只是随机改变一些值呢? –
请注意,对工具/库的请求明确偏离主题。 – jonrsharpe
请求帮助来完成一段代码来完成这个简单的任务,通常在这里是受欢迎的;-)这样可以吗,你画了一些代码,而其他人帮助你在哪里卡住?正如@AbdulFatir建议/问:是否有某种原因,为什么在样本中只有值字符串的第三个字符被改变,而且只是唯一的? – Dilettant