Skip to content

Instantly share code, notes, and snippets.

@wapj
Last active December 1, 2020 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wapj/256605c28d5650620ffbb1f395dd59fe to your computer and use it in GitHub Desktop.
Save wapj/256605c28d5650620ffbb1f395dd59fe to your computer and use it in GitHub Desktop.
파이썬으로 테스트 데이터 만들기
"""
https://blog.gyus.me/2020/generate-sample-data-with-python/ 에 관련 글이 있습니다.
student_score DDL
create table student_score
(
uid int auto_increment
primary key,
name varchar(30) null comment '이름',
grade int null,
class_no int null,
year int null comment '연도',
semester int null comment '1 혹은 2',
subject varchar(30) null comment '과목',
score int null comment '점수'
)
comment '학생의 성적테이블';
"""
import os
import pathlib
import random
from getpass import getpass
from faker import Faker
fake = Faker('ko_KR')
과목들 = ['국어', '영어', '수학', '과학', '사회', '윤리', '지리', '기술', '체육']
filename = "student_score.sql"
def get_test_data():
이름 = fake.name()
연도 = random.randrange(2010, 2021) # 2010 ~ 2020
학년 = random.randrange(1, 4) # 1~3
반 = random.randrange(1, 6) # 1~5
datas = []
for y in range(2):
학기 = y + 1
for idx, 과목 in enumerate(과목들):
점수 = random.randrange(0, 101)
data = f"('{이름}',{연도}, {학기}, {학년}, {반}, '{과목}', {점수}),"
datas.append(data)
return datas
def create_datas():
f = open(filename, "w")
f.write("""TRUNCATE TABLE my_db.student_score;
INSERT INTO my_db.student_score (name, year, semester, grade, class_no, subject, score)
VALUES
""")
datas = []
for x in range(1000):
one_person_data = get_test_data()
datas += one_person_data
# 마지막에 있는 ,(콤마) 를 ; (세미콜론) 으로 변경하기 위한 코드
datas[-1] = datas[-1][:-1] + ';'
for data in datas:
f.write(data)
f.close()
if __name__ == '__main__':
create_datas()
current_dir = pathlib.Path().absolute()
dump_file = f"{current_dir}/{filename}"
print("덤프파일 경로 : ", dump_file)
mypass = getpass("패스워드를 입력해주세요 : ")
os.system(f"mysql -u root -p{mypass} < {dump_file}")
@smileof
Copy link

smileof commented Dec 1, 2020

파이썬을 이용해서 업무 적용하기 딱 좋은 코드네요!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment