Skip to content

Commit

Permalink
fix: fix faker bug for python 3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
ligangc committed Mar 28, 2020
1 parent 33e865b commit 9f8c410
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 30 deletions.
2 changes: 1 addition & 1 deletion datafaker/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def parse_args():
parser.add_argument('--workers', action='store', type=int, default=WORKERS, help='the interval to make stream data')
parser.add_argument('--version', action='store_true', help="print the version number and exit")
parser.add_argument('--outprint', action='store_true', help="print fake date to screen")
parser.add_argument('--outspliter', action='store', help="print data, to split columns")
parser.add_argument('--outspliter', action='store', default=',', help="print data, to split columns")
parser.add_argument('--locale', action='store', default=DEFAULT_LOCALE, help='locale language')
parser.add_argument('--outfile', help='file to write output to (default: stdout)')
parser.add_argument('--format', default=DEFAULT_FORMAT, help='outprint and outfile format: json, text (default: text)')
Expand Down
22 changes: 19 additions & 3 deletions datafaker/dbs/basedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
from time import sleep

from datafaker import compat
from datafaker.compat import safe_decode, queue
from datafaker.constant import INT_TYPES, FLOAT_TYPES, ENUM_FILE, JSON_FORMAT, MAX_QUEUE_SIZE, MIN_RECORDS_FOR_PARALLEL
from datafaker.exceptions import EnumMustNotEmptyError, ParseSchemaError
from datafaker.fakedata import FackData
from datafaker.reg import reg_keyword, reg_cmd, reg_args
from datafaker.utils import count_time, read_file_lines, json_item, process_op_args


class BaseDB(object):
"""
基础数据源类
"""

def __init__(self, args):
self.args = args
Expand Down Expand Up @@ -115,12 +120,23 @@ def print_data(self):
while not self.isover.value or not self.queue.empty():
try:
data = self.queue.get_nowait()
print(data)
items = []
for item in data:
if item is None:
items.append('None')
elif isinstance(item, int):
items.append(str(item))
else:
items.append(safe_decode(item))
row = self.args.outspliter.join(items)
print(row)
# print(data)
if self.args.interval:
time.sleep(self.args.interval)

except:
except queue.Empty:
pass
except Exception as e:
print(e)

def parse_schema(self):
if self.args.meta:
Expand Down
4 changes: 2 additions & 2 deletions datafaker/fakedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import datetime
import random
import time
from faker import Faker
from faker import Faker, Factory
from datafaker import compat
from datafaker.compat import Dict

Expand All @@ -12,7 +12,7 @@ class FackData(object):

def __init__(self, locale):

self.faker = Faker(locale)
self.faker = Factory().create(locale)
self.faker_funcs = dir(self.faker)
self.lock = compat.Lock()
self.auto_inc = Dict()
Expand Down
46 changes: 24 additions & 22 deletions doc/使用举例.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,7 @@ create table stu (

```

### 5.3 从本地文件meta.txt中读取元数据,以,,分隔符构造10条数据,打印在屏幕上
----------------

```
$ datafaker rdb mysql+mysqldb://root:root@localhost:3600/test?charset=utf8 stu 10 --outprint --meta meta.txt --outspliter ',,'
1,,鲍红,,人和中心,,高小王子,,3,,81,,55.6,,13197453222,,[email protected],,192.100.224.255,,江苏省西宁市梁平朱路I座 944204
2,,刘东,,清华中学,,高小王子,,3,,31,,52.4,,15206198472,,[email protected],,203.0.190.6,,内蒙古自治区嘉禾市兴山呼和浩特街E座 706421
3,,匡静,,人和中心,,歌神,,9,,84,,72.51,,18944398099,,[email protected],,203.1.53.166,,安徽省永安市沈河惠州街x座 345415
4,,王宇,,猪场,,逗比,,6,,89,,19.3,,18628114285,,[email protected],,169.4.126.215,,山西省梧州县朝阳何路y座 846430
5,,陆桂芝,,猪场,,逗比,,8,,99,,92.22,,13304570255,,[email protected],,168.136.127.200,,江苏省英县徐汇尹街C座 908240
6,,顾阳,,猪场,,歌神,,9,,32,,43.14,,18025578420,,[email protected],,174.50.222.39,,黑龍江省惠州县梁平大冶街Z座 611736
7,,杨洁,,人和中心,,鬼泣,,6,,35,,81.25,,13654306263,,[email protected],,100.57.79.2,,湖北省琳市沙湾汪街V座 544660
8,,申璐,,人和中心,,鬼泣,,6,,14,,73.61,,13866020503,,[email protected],,198.248.254.56,,陕西省合山县东丽宁德街Q座 810017
9,,申强,,广东中学,,逗比,,7,,48,,90.65,,13915915013,,[email protected],,169.210.122.39,,甘肃省冬梅县城北六安街Z座 619755
10,,李丹丹,,旧大院,,鬼泣,,3,,67,,87.63,,18899812516,,[email protected],,192.52.218.133,,湖南省宜都县萧山澳门街E座 791911
generated records : 10
printed records : 10
time used: 0.458 s
```
这是个学生表描述。
编写元数据meta.txt,这是个学生表描述。
其中meta.txt文件内容为:
```
id||int||自增id[:inc(id,1)]
Expand Down Expand Up @@ -113,7 +93,29 @@ name不加标记则会随机产生20字符内的字符串,可以加上改为
后面将详细介绍构造规则说明

注意:meta.txt和names.txt需要放在同一个目录下,再运行datafaker命令
如果没有enum类型,则不需要names.txt文件
如果没有enum类型从文件读取数据,则不需要names.txt文件

### 5.3 从本地文件meta.txt中读取元数据,以,,分隔符构造10条数据,打印在屏幕上
----------------

```
$ datafaker rdb mysql+mysqldb://root:root@localhost:3600/test?charset=utf8 stu 10 --outprint --meta meta.txt --outspliter ',,'
1,,鲍红,,人和中心,,高小王子,,3,,81,,55.6,,13197453222,,[email protected],,192.100.224.255,,江苏省西宁市梁平朱路I座 944204
2,,刘东,,清华中学,,高小王子,,3,,31,,52.4,,15206198472,,[email protected],,203.0.190.6,,内蒙古自治区嘉禾市兴山呼和浩特街E座 706421
3,,匡静,,人和中心,,歌神,,9,,84,,72.51,,18944398099,,[email protected],,203.1.53.166,,安徽省永安市沈河惠州街x座 345415
4,,王宇,,猪场,,逗比,,6,,89,,19.3,,18628114285,,[email protected],,169.4.126.215,,山西省梧州县朝阳何路y座 846430
5,,陆桂芝,,猪场,,逗比,,8,,99,,92.22,,13304570255,,[email protected],,168.136.127.200,,江苏省英县徐汇尹街C座 908240
6,,顾阳,,猪场,,歌神,,9,,32,,43.14,,18025578420,,[email protected],,174.50.222.39,,黑龍江省惠州县梁平大冶街Z座 611736
7,,杨洁,,人和中心,,鬼泣,,6,,35,,81.25,,13654306263,,[email protected],,100.57.79.2,,湖北省琳市沙湾汪街V座 544660
8,,申璐,,人和中心,,鬼泣,,6,,14,,73.61,,13866020503,,[email protected],,198.248.254.56,,陕西省合山县东丽宁德街Q座 810017
9,,申强,,广东中学,,逗比,,7,,48,,90.65,,13915915013,,[email protected],,169.210.122.39,,甘肃省冬梅县城北六安街Z座 619755
10,,李丹丹,,旧大院,,鬼泣,,3,,67,,87.63,,18899812516,,[email protected],,192.52.218.133,,湖南省宜都县萧山澳门街E座 791911
generated records : 10
printed records : 10
time used: 0.458 s
```



```sh
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/test_datafaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_op():
capStampDate||datetime||捕获日期[:date(-5d,-0d, %Y-%m-%d)]
"""
test_tmpdir, meta_file = _make_tmp_file()
cmd = 'datafaker file . hello.txt 10 --meta {meta_file} --format text --outprint --format json'.format(meta_file=meta_file)
cmd = 'datafaker file . hello.txt 10 --meta {meta_file} --format text --outprint'.format(meta_file=meta_file)
_main(cmd, meta_content)


Expand Down Expand Up @@ -193,6 +193,7 @@ def test_date():
def test_order_enum():
meta_content = """
id||int||not,
name||varchar(20)||学生名字[:name]
nickname||varchar(20)||学生名字[:order_enum(xiao ming, hah, lele, esd, f222)]
nickname2||varchar(20)||学生名字[:order_enum(xiao ming, hah, lele, esd, f222)]
class_num||char(10)||default,
Expand All @@ -202,5 +203,5 @@ def test_order_enum():
address||char(40)||default,
"""
test_tmpdir, meta_file = _make_tmp_file()
cmd = 'datafaker file . hello.txt 21 --meta {meta_file} --format text --outprint'.format(meta_file=meta_file)
cmd = 'datafaker file . hello.txt 21 --meta {meta_file} --format text --outprint --outspliter :'.format(meta_file=meta_file)
_main(cmd, meta_content)
8 changes: 8 additions & 0 deletions tests/unit/test_faker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
fake = Factory().create('zh_CN')


def test_dir():
print(dir(fake))


def test_func():
print(fake.random_number(1))
print(fake.random_number(2))
Expand Down Expand Up @@ -113,4 +117,8 @@ def test_faker():
print(fake.rgb_color())


def test_name():
print(fake.name())



0 comments on commit 9f8c410

Please sign in to comment.