浅析Go语言版本的forgery

Oriel ·
更新时间:2024-09-20
· 545 次阅读

使用过Python语言的朋友们可能使用过 forgery_py ,它是一个伪造数据的工具。能伪造一些常用的数据。在我们开发过程和效果展示是十分有用。但是没有Go语言版本的,所以就动手折腾吧。

从源码入手

在forgery_py的 PyPi 有一段的实例代码:

>>> import forgery_py >>> forgery_py.address.street_address() u'4358 Shopko Junction' >>> forgery_py.basic.hex_color() '3F0A59' >>> forgery_py.currency.description() u'Slovenia Tolars' >>> forgery_py.date.date() datetime.date(2012, 7, 27) >>> forgery_py.internet.email_address() u'brian@zazio.mil' >>> forgery_py.lorem_ipsum.title() u'Pretium nam rhoncus ultrices!' >>> forgery_py.name.full_name() u'Mary Peters' >>> forgery_py.personal.language() u'Hungarian'

从以上的方法调用我们可以看出forgery_py下有一系列的 *.py 文件,里面有各种方法,实现各种功能,我们在来通过分析下Python版本的forgery_py的源码来看看它的实现原理。

# ForgeryPy 包的一级目录 ├── dictionaries # 伪造内容和来源目录,目录下存放的都是一些文本文件 ├── dictionaries_loader.py # 加载文件脚本 ├── forgery # 主目录,实现各种数据伪造功能,目录下存放的都是python文件 ├── __init__.py

我们在来看下forgery目录下的脚本

$ cat name.py import random from ..dictionaries_loader import get_dictionary __all__ = [ 'first_name', 'last_name', 'full_name', 'male_first_name', 'female_first_name', 'company_name', 'job_title', 'job_title_suffix', 'title', 'suffix', 'location', 'industry' ] def first_name(): """Random male of female first name.""" _dict = get_dictionary('male_first_names') _dict += get_dictionary('female_first_names') return random.choice(_dict).strip()

__all__ 设置能被调用的方法。

first_name() 方法是forgery_py中一个典型伪造数据方法,我们只要来分析它就可以知道forgery_py的工作原理了。

这个方法代码很少,能容易就看出 _dict = get_dictionary('male_first_names') _dict += get_dictionary('female_first_names') 获取的数据合并,在最后的 return random.choice(_dict).strip() 返回随机的数据。它的重点在于 get_dictionary() ,所以我们需要来看它的所在位置 dictionaries_loader.py 文件。

$ cat dictionaries_loader import random DICTIONARIES_PATH = abspath(join(dirname(__file__), 'dictionaries')) dictionaries_cache = {} def get_dictionary(dict_name): """ Load a dictionary file ``dict_name`` (if it's not cached) and return its contents as an array of strings. """ global dictionaries_cache if dict_name not in dictionaries_cache: try: dictionary_file = codecs.open( join(DICTIONARIES_PATH, dict_name), 'r', 'utf-8' ) except IOError: None else: dictionaries_cache[dict_name] = dictionary_file.readlines() dictionary_file.close() return dictionaries_cache[dict_name]

以上就是 dictionaries_loader.py 文件去掉注释后的所以要内容。它的主要实现就是:定义一个全局的字典参数 dictionaries_cache 作为缓存,然后定义方法 get_dictionary() 获取源数据, get_dictionary() 中每次forgery目录底下方法调用时先查看缓存,缓存字典中存在数据就直接输出,不存在就读取 dictionaries 底下的对应文件,并存入缓存。最后是返回数据。

总的来说forgery_py的原理就是:一个方法调用,去读内存中的缓存,存在就直接返回,不存在就到对应的文本文件中读取并写入缓存并返回。返回来的数据再随机选取输出结果。

使用Go语言实现

在了解了forgery_py的工作原理之后,我们就可以来使用Go语言来实现了。

# forgery的基本目录 $ cat forgery ├── dictionaries # 数据源 │ ├── male_first_names ├── name.go # 具体功能实现 └── loader.go # 加载数据

根据python版本的我们也来创建对应的目录。

实现数据的读取的缓存:

// forgery/loader.go package forgery import ( "os" "io" "bufio" "math/rand" "time" "strings" ) // 全局的缓存map var dictionaries map[string][]string = make(map[string][]string) // 在获取数据之后随机输出 func random(slice []string) string { rand.Seed(time.Now().UnixNano()) n := rand.Intn(len(slice)) return strings.TrimSpace(slice[n]) } // 主要的数据加载方法 func loader(name string) (slice []string, err error) { slice, ok := dictionaries[name] // 缓存中存在数据,直接返回 if ok { return slice, nil } // 读取对应文件 file, err := os.Open("./dictionaries/" + name) if err != nil { return slice, err } defer file.Close() rd := bufio.NewReader(file) for { line, err := rd.ReadString('\n') slice = append(slice, line) if err != nil || io.EOF == err { break } } dictionaries[name] = slice return slice, nil } // 统一的错误处理 func checkErr(err error) (string, error) { return "", err }

实现具体的功能:

// forgery/name.go // Random male of female first name. func FirstName() (string, error) { slice, err := loader("male_first_names") checkErr(err) slice1, err := loader("female_first_names") checkErr(err) slice = append(slice, slice1...) return random(slice), nil }

这样就将python语言版本的forgery_py使用Go来实现了。

最后

上面只是提及了一些工作原理,具体的源代码可以看 https://github.com/xingyys/fo... ,也十分感谢 https://github.com/tomekwojci... ,具体的思路和里面的数据源都是他提供的。本人就是做了一些 翻译 的的工作。

总结

以上所述是小编给大家介绍的Go语言版本的forgery,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对软件开发网网站的支持!



GO 浅析go 版本 go语言

需要 登录 后方可回复, 如果你还没有账号请 注册新账号