feat: faker data: railway

This commit is contained in:
Zhaolong
2022-01-27 18:00:03 +08:00
parent 1e3d584870
commit c374acc1db
9 changed files with 178 additions and 20 deletions

View File

@@ -31,6 +31,7 @@ sqlalchemy = "*"
flask-sqlalchemy = "*"
pymysql = "*"
cryptography = "*"
deprecated = "*"
[dev-packages]

67
Pipfile.lock generated
View File

@@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "8f60d328de801415c331d15d1df414d97c273a3c70eac47aec0392631a0b2102"
"sha256": "2fe4324591b62180dbf2b2ca8c54c357d397257cdb0ed7fa01937a119be5ea6a"
},
"pipfile-spec": 6,
"requires": {
@@ -129,6 +129,14 @@
"index": "pypi",
"version": "==36.0.1"
},
"deprecated": {
"hashes": [
"sha256:43ac5335da90c31c24ba028af536a91d41d53f9e6901ddb021bcc572ce44e38d",
"sha256:64756e3e14c8c5eea9795d93c524551432a0be75629f8f29e67ab8caf076c76d"
],
"index": "pypi",
"version": "==1.2.13"
},
"distlib": {
"hashes": [
"sha256:6564fe0a8f51e734df6333d08b8b94d4ea8ee6b99b5ed50613f731fd4089f34b",
@@ -547,6 +555,63 @@
],
"index": "pypi",
"version": "==2.0.2"
},
"wrapt": {
"hashes": [
"sha256:086218a72ec7d986a3eddb7707c8c4526d677c7b35e355875a0fe2918b059179",
"sha256:0877fe981fd76b183711d767500e6b3111378ed2043c145e21816ee589d91096",
"sha256:0a017a667d1f7411816e4bf214646d0ad5b1da2c1ea13dec6c162736ff25a374",
"sha256:0cb23d36ed03bf46b894cfec777eec754146d68429c30431c99ef28482b5c1df",
"sha256:1fea9cd438686e6682271d36f3481a9f3636195578bab9ca3382e2f5f01fc185",
"sha256:220a869982ea9023e163ba915077816ca439489de6d2c09089b219f4e11b6785",
"sha256:25b1b1d5df495d82be1c9d2fad408f7ce5ca8a38085e2da41bb63c914baadff7",
"sha256:2dded5496e8f1592ec27079b28b6ad2a1ef0b9296d270f77b8e4a3a796cf6909",
"sha256:2ebdde19cd3c8cdf8df3fc165bc7827334bc4e353465048b36f7deeae8ee0918",
"sha256:43e69ffe47e3609a6aec0fe723001c60c65305784d964f5007d5b4fb1bc6bf33",
"sha256:46f7f3af321a573fc0c3586612db4decb7eb37172af1bc6173d81f5b66c2e068",
"sha256:47f0a183743e7f71f29e4e21574ad3fa95676136f45b91afcf83f6a050914829",
"sha256:498e6217523111d07cd67e87a791f5e9ee769f9241fcf8a379696e25806965af",
"sha256:4b9c458732450ec42578b5642ac53e312092acf8c0bfce140ada5ca1ac556f79",
"sha256:51799ca950cfee9396a87f4a1240622ac38973b6df5ef7a41e7f0b98797099ce",
"sha256:5601f44a0f38fed36cc07db004f0eedeaadbdcec90e4e90509480e7e6060a5bc",
"sha256:5f223101f21cfd41deec8ce3889dc59f88a59b409db028c469c9b20cfeefbe36",
"sha256:610f5f83dd1e0ad40254c306f4764fcdc846641f120c3cf424ff57a19d5f7ade",
"sha256:6a03d9917aee887690aa3f1747ce634e610f6db6f6b332b35c2dd89412912bca",
"sha256:705e2af1f7be4707e49ced9153f8d72131090e52be9278b5dbb1498c749a1e32",
"sha256:766b32c762e07e26f50d8a3468e3b4228b3736c805018e4b0ec8cc01ecd88125",
"sha256:77416e6b17926d953b5c666a3cb718d5945df63ecf922af0ee576206d7033b5e",
"sha256:778fd096ee96890c10ce96187c76b3e99b2da44e08c9e24d5652f356873f6709",
"sha256:78dea98c81915bbf510eb6a3c9c24915e4660302937b9ae05a0947164248020f",
"sha256:7dd215e4e8514004c8d810a73e342c536547038fb130205ec4bba9f5de35d45b",
"sha256:7dde79d007cd6dfa65afe404766057c2409316135cb892be4b1c768e3f3a11cb",
"sha256:81bd7c90d28a4b2e1df135bfbd7c23aee3050078ca6441bead44c42483f9ebfb",
"sha256:85148f4225287b6a0665eef08a178c15097366d46b210574a658c1ff5b377489",
"sha256:865c0b50003616f05858b22174c40ffc27a38e67359fa1495605f96125f76640",
"sha256:87883690cae293541e08ba2da22cacaae0a092e0ed56bbba8d018cc486fbafbb",
"sha256:8aab36778fa9bba1a8f06a4919556f9f8c7b33102bd71b3ab307bb3fecb21851",
"sha256:8c73c1a2ec7c98d7eaded149f6d225a692caa1bd7b2401a14125446e9e90410d",
"sha256:936503cb0a6ed28dbfa87e8fcd0a56458822144e9d11a49ccee6d9a8adb2ac44",
"sha256:944b180f61f5e36c0634d3202ba8509b986b5fbaf57db3e94df11abee244ba13",
"sha256:96b81ae75591a795d8c90edc0bfaab44d3d41ffc1aae4d994c5aa21d9b8e19a2",
"sha256:981da26722bebb9247a0601e2922cedf8bb7a600e89c852d063313102de6f2cb",
"sha256:ae9de71eb60940e58207f8e71fe113c639da42adb02fb2bcbcaccc1ccecd092b",
"sha256:b73d4b78807bd299b38e4598b8e7bd34ed55d480160d2e7fdaabd9931afa65f9",
"sha256:d4a5f6146cfa5c7ba0134249665acd322a70d1ea61732723c7d3e8cc0fa80755",
"sha256:dd91006848eb55af2159375134d724032a2d1d13bcc6f81cd8d3ed9f2b8e846c",
"sha256:e05e60ff3b2b0342153be4d1b597bbcfd8330890056b9619f4ad6b8d5c96a81a",
"sha256:e6906d6f48437dfd80464f7d7af1740eadc572b9f7a4301e7dd3d65db285cacf",
"sha256:e92d0d4fa68ea0c02d39f1e2f9cb5bc4b4a71e8c442207433d8db47ee79d7aa3",
"sha256:e94b7d9deaa4cc7bac9198a58a7240aaf87fe56c6277ee25fa5b3aa1edebd229",
"sha256:ea3e746e29d4000cd98d572f3ee2a6050a4f784bb536f4ac1f035987fc1ed83e",
"sha256:ec7e20258ecc5174029a0f391e1b948bf2906cd64c198a9b8b281b811cbc04de",
"sha256:ec9465dd69d5657b5d2fa6133b3e1e989ae27d29471a672416fd729b429eb554",
"sha256:f122ccd12fdc69628786d0c947bdd9cb2733be8f800d88b5a37c57f1f1d73c10",
"sha256:f99c0489258086308aad4ae57da9e8ecf9e1f3f30fa35d5e170b4d4896554d80",
"sha256:f9c51d9af9abb899bd34ace878fbec8bf357b3194a10c4e8e0a25512826ef056",
"sha256:fd76c47f20984b43d93de9a82011bb6e5f8325df6c9ed4d8310029a55fa361ea"
],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'",
"version": "==1.13.3"
}
},
"develop": {}

10
app.py
View File

@@ -3,11 +3,13 @@ import config
import utils.response as response
import service.faker_data as faker
from base import db
from model import Person, SexEnum
from model import Person, SexEnum, City
app = Flask(__name__)
config.init_config(app)
db.init_app(app)
# with app.app_context():
# print(db.session.query(City).one())
@app.route('/')
@@ -18,10 +20,10 @@ def hello_world(): # put application's code here
@app.route("/faker")
def faker_data():
persons = faker.store_faker_identification(1)
results = [person.__to_dict__() for person in persons]
# persons = faker.store_faker_identification(1)
# results = [person.__to_dict__() for person in persons]
# return response.succ(persons)
return response.succ(faker.faker_train(3))
return response.succ(faker.faker_railways(30))
if __name__ == '__main__':

View File

@@ -3,12 +3,12 @@ from urllib.parse import quote_plus as urlquote
DIALECT = 'mysql'
DRIVER = 'pymysql'
USERNAME = 'root'
PASSWORD = '13291004986@lm'
PASSWORD = '13291004986'
HOST = 'myhost.fallen-angle.com'
PORT = '3306'
DATABASE = 'ncov_track'
SQLALCHEMY_DATABASE_URI = f'{DIALECT}+{DRIVER}://{USERNAME}:{urlquote(PASSWORD)}@{HOST}:{PORT}/{DATABASE}?charset=utf8'
SQLALCHEMY_DATABASE_URI = f'{DIALECT}+{DRIVER}://{USERNAME}:{PASSWORD}@{HOST}:{PORT}/{DATABASE}?charset=utf8'
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_ECHO = True
@@ -16,3 +16,4 @@ SQLALCHEMY_ECHO = True
def init_config(app):
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SQLALCHEMY_ECHO"] = True

View File

@@ -1,2 +1,5 @@
from .person import Person
from .person import SexEnum
from .person import SexEnum
from .hotel import Hotel
from .city import City
from .railway import Railway

20
model/city.py Normal file
View File

@@ -0,0 +1,20 @@
from sqlalchemy import Column, String
from base import db
class City(db.Model):
__tablename__ = 'city'
city_id = Column(String(255), primary_key=True)
city_en = Column(String(255), nullable=False)
city_cn = Column(String(255), nullable=False)
country_code = Column(String(255))
country_en = Column(String(255))
country_cn = Column(String(255))
province_en = Column(String(255))
province_cn = Column(String(255))
admin_district_en = Column(String(255))
admin_district_cn = Column(String(255))
latitude = Column(String(255))
longitude = Column(String(255))
ad_code = Column(String(255))

14
model/hotel.py Normal file
View File

@@ -0,0 +1,14 @@
from sqlalchemy import Column, Date, Integer, String, text
from base import db
class Hotel(db.Model):
__tablename__ = 'faker_hotel'
id = Column(Integer, primary_key=True, comment='id')
hotel_code = Column(String(10), nullable=False, server_default=text("''"), comment='酒店编号')
hotel_name = Column(String(50), nullable=False, server_default=text("''"), comment='酒店名')
locate_city_id = Column(String(10), nullable=False, server_default=text("''"), comment='酒店所在城市ID')
identification = Column(String(18), server_default=text("''"), comment='入住者身份证号')
in_data = Column(Date, nullable=False, comment='入住日期')
out_data = Column(Date, nullable=False, comment='离开日期')

13
model/railway.py Normal file
View File

@@ -0,0 +1,13 @@
from sqlalchemy import Column, Date, Integer, String, text
from base import db
class Railway(db.Model):
__tablename__ = 'faker_railway'
id = Column(Integer, primary_key=True)
name = Column(String(10), nullable=False, server_default=text("''"), comment='乘客姓名')
identification = Column(String(18), nullable=False, server_default=text("''"), comment='乘客身份证号码')
phone = Column(String(20), nullable=False, server_default=text("''"), comment='手机号码')
train = Column(String(10), nullable=False, server_default=text("''"), comment='车次')
launch = Column(Date, nullable=False, comment='发车时间')

View File

@@ -1,20 +1,34 @@
import random
import requests
import threading
import random
from concurrent.futures import ThreadPoolExecutor, as_completed
from lxml import etree
from faker import Faker
from model import SexEnum, Person
from model import SexEnum, Person, Hotel, Railway, City
from base import db
from datetime import datetime, timedelta
from dateutil import parser, rrule
from deprecated import deprecated
executor = ThreadPoolExecutor(max_workers=10)
faker = Faker(["zh_CN"])
train_prefix = ['T', 'K', 'D', 'G', 'L', '']
faker_hotel_infos = [
Hotel(hotel_code='1150001', hotel_name='北京华侨大厦', locate_city_id='101011600'),
Hotel(hotel_code='3250069', hotel_name='南京绿地洲际酒店', locate_city_id='101190111'),
Hotel(hotel_code='3350039', hotel_name='义乌锦都酒店', locate_city_id='101210904'),
Hotel(hotel_code='3650011', hotel_name='九江信华建国酒店', locate_city_id='101240211'),
Hotel(hotel_code='3750005', hotel_name='青岛香格里拉大酒店', locate_city_id='101120203'),
Hotel(hotel_code='3250028', hotel_name='徐州开元名都大酒店', locate_city_id='101190811'),
]
faker_train_ids = ['D7359', 'T4108', 'D776', 'D3023', 'K1461', 'L1500', 'G662']
def faker_identification() -> Person:
# Due to the limit of reptile, turn to query the city by city code
@deprecated
def faker_identification_old() -> Person:
identification = faker.ssn(max_age=70)
resp = requests.get("https://shenfenzheng.bmcx.com/" + identification + "__shenfenzheng/")
html = etree.HTML(resp.text)
@@ -31,29 +45,54 @@ def faker_identification() -> Person:
return person
def time(times):
return times
def faker_identification() -> Person:
identification = faker.ssn(max_age=70)
birthday = parser.parse(identification[6:14])
age = rrule.rrule(rrule.YEARLY, dtstart=birthday, until=datetime.today())
sex = 0 if int(identification[16:17]) % 2 == 0 else 1
# Better out of cycle
city = db.session.query(City).filter(City.ad_code == identification[0:6]).one()
address = '{} {} {} {}'.format(city.country_cn, city.province_cn, city.admin_district_cn, city.city_cn)
return Person(
identification=identification,
address=address,
sex=sex,
name=faker.last_name() + (faker.first_name_male() if sex == SexEnum.MALE.sex else faker.first_name_female()),
age=age,
phone=faker.phone_number()
)
def faker_identifications(num: int) -> list[Person]:
all_tasks = [executor.submit(faker_identification) for i in range(num)]
futures = as_completed(all_tasks)
persons = [future.result() for future in futures]
return persons
def store_faker_identification(num: int) -> list[Person]:
persons = faker_identifications(num)
db.session.add_all(persons)
db.session.commit()
return persons
def faker_train(num: int) -> list[str]:
return [generate_train_id() for i in range(num)]
def faker_railways(num: int) -> list[Railway]:
persons = faker_identifications(num)
railways = [faker_railway(person) for person in persons]
db.session.add_all(railways)
db.session.commit()
return railways
def faker_railway(person: Person) -> Railway:
return Railway(
identification=person.identification,
phone=person.phone,
name=person.name,
train=faker_train_ids[int(random.random() * len(faker_train_ids))],
launch=datetime.today() + timedelta(days=(int(random.random() * 21) - 10))
)
def generate_train_id():
prefix = train_prefix[int(random.random() * len(train_prefix))]
num = int(random.random() * 10000).__str__()
return prefix + num
# def faker_hotel(num: int):