fix: optimize insert
This commit is contained in:
6
app.py
6
app.py
@@ -8,9 +8,9 @@ from model import Person, SexEnum, City
|
|||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
config.init_config(app)
|
config.init_config(app)
|
||||||
db.init_app(app)
|
db.init_app(app)
|
||||||
# with app.app_context():
|
db.app = app
|
||||||
# print(db.session.query(City).one())
|
with app.app_context():
|
||||||
|
db.create_all()
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def hello_world(): # put application's code here
|
def hello_world(): # put application's code here
|
||||||
|
|||||||
@@ -25,6 +25,11 @@ faker_hotel_infos = [
|
|||||||
|
|
||||||
faker_train_ids = ['D7359', 'T4108', 'D776', 'D3023', 'K1461', 'L1500', 'G662']
|
faker_train_ids = ['D7359', 'T4108', 'D776', 'D3023', 'K1461', 'L1500', 'G662']
|
||||||
|
|
||||||
|
ident_valid_ratio = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
|
||||||
|
ident_valid_result = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
|
||||||
|
ident_area = ['320700', '320722', '320707', '320723', '320724', '320706', '320703', '320500', '320581', '320582',
|
||||||
|
'320583', '320506', '320505', '320509', '320585', '320507', '320508']
|
||||||
|
|
||||||
|
|
||||||
# Due to the limit of reptile, turn to query the city by city code
|
# Due to the limit of reptile, turn to query the city by city code
|
||||||
@deprecated
|
@deprecated
|
||||||
@@ -46,16 +51,22 @@ def faker_identification_old() -> Person:
|
|||||||
|
|
||||||
|
|
||||||
def faker_identification() -> Person:
|
def faker_identification() -> Person:
|
||||||
identification = faker.ssn(max_age=70)
|
area = ident_area[random.randint(0, len(ident_area) - 1)]
|
||||||
birthday = parser.parse(identification[6:14])
|
birthdate = datetime(
|
||||||
age = rrule.rrule(rrule.YEARLY, dtstart=birthday, until=datetime.today())
|
year=2022 - random.randint(18, 70),
|
||||||
|
month=random.randint(1, 12),
|
||||||
|
day=random.randint(1, 28)
|
||||||
|
)
|
||||||
|
birthday = birthdate.__format__("%Y%m%d")
|
||||||
|
random_num = random.randint(100, 999).__str__()
|
||||||
|
identification = area + birthday + random_num
|
||||||
|
valid = ident_valid_result[sum([int(identification[i]) * ident_valid_ratio[i] for i in range(17)]) % 11]
|
||||||
|
identification = identification + valid
|
||||||
|
birthdate = parser.parse(identification[6:14])
|
||||||
|
age = rrule.rrule(rrule.YEARLY, dtstart=birthdate, until=datetime.today()).count()
|
||||||
sex = 0 if int(identification[16:17]) % 2 == 0 else 1
|
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(
|
return Person(
|
||||||
identification=identification,
|
identification=identification,
|
||||||
address=address,
|
|
||||||
sex=sex,
|
sex=sex,
|
||||||
name=faker.last_name() + (faker.first_name_male() if sex == SexEnum.MALE.sex else faker.first_name_female()),
|
name=faker.last_name() + (faker.first_name_male() if sex == SexEnum.MALE.sex else faker.first_name_female()),
|
||||||
age=age,
|
age=age,
|
||||||
@@ -64,18 +75,20 @@ def faker_identification() -> Person:
|
|||||||
|
|
||||||
|
|
||||||
def faker_identifications(num: int) -> list[Person]:
|
def faker_identifications(num: int) -> list[Person]:
|
||||||
all_tasks = [executor.submit(faker_identification) for i in range(num)]
|
persons = [faker_identification() for i in range(num)]
|
||||||
futures = as_completed(all_tasks)
|
area_codes = [person.identification[0:6] for person in persons]
|
||||||
persons = [future.result() for future in futures]
|
rows = City.query.filter(City.ad_code.in_(area_codes))
|
||||||
db.session.add_all(persons)
|
code_area_dict = {row.ad_code: '{} {} {} {}'.format(row.country_cn, row.province_cn, row.admin_district_cn, row.city_cn) for row in rows}
|
||||||
db.session.commit()
|
for person in persons:
|
||||||
|
person.address = code_area_dict[person.identification[0:6]]
|
||||||
|
db.session.bulk_save_objects(persons)
|
||||||
return persons
|
return persons
|
||||||
|
|
||||||
|
|
||||||
def faker_railways(num: int) -> list[Railway]:
|
def faker_railways(num: int) -> list[Railway]:
|
||||||
persons = faker_identifications(num)
|
persons = faker_identifications(num)
|
||||||
railways = [faker_railway(person) for person in persons]
|
railways = [faker_railway(person) for person in persons]
|
||||||
db.session.add_all(railways)
|
db.session.bulk_save_objects(railways)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return railways
|
return railways
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user