NDOJ/judge/management/commands/generate_data.py

54 lines
1.6 KiB
Python
Raw Normal View History

2022-04-12 02:18:01 +00:00
from django.core.management.base import BaseCommand
from judge.models import *
from collections import defaultdict
import csv
import os
from django.conf import settings
def gen_submissions():
2022-05-14 17:57:27 +00:00
headers = ["uid", "pid"]
with open(os.path.join(settings.ML_DATA_PATH, "submissions.csv"), "w") as csvfile:
2022-04-12 02:18:01 +00:00
f = csv.writer(csvfile)
f.writerow(headers)
2022-05-14 17:57:27 +00:00
2022-04-12 02:18:01 +00:00
last_pid = defaultdict(int)
for u in Profile.objects.all():
used = set()
2022-05-14 17:57:27 +00:00
print("Processing user", u.id)
for s in Submission.objects.filter(user=u).order_by("-date"):
2022-04-12 02:18:01 +00:00
if s.problem.id not in used:
used.add(s.problem.id)
f.writerow([u.id, s.problem.id])
2022-05-14 17:57:27 +00:00
2022-04-12 02:18:01 +00:00
def gen_users():
2022-05-14 17:57:27 +00:00
headers = ["uid", "username", "rating", "points"]
with open(os.path.join(settings.ML_DATA_PATH, "profiles.csv"), "w") as csvfile:
2022-04-12 02:18:01 +00:00
f = csv.writer(csvfile)
f.writerow(headers)
2022-05-14 17:57:27 +00:00
2022-04-12 02:18:01 +00:00
for u in Profile.objects.all():
f.writerow([u.id, u.username, u.rating, u.performance_points])
2022-05-14 17:57:27 +00:00
2022-04-12 02:18:01 +00:00
def gen_problems():
2022-05-14 17:57:27 +00:00
headers = ["pid", "code", "name", "points", "url"]
with open(os.path.join(settings.ML_DATA_PATH, "problems.csv"), "w") as csvfile:
2022-04-12 02:18:01 +00:00
f = csv.writer(csvfile)
f.writerow(headers)
2022-05-14 17:57:27 +00:00
2022-04-12 02:18:01 +00:00
for p in Problem.objects.all():
2022-05-14 17:57:27 +00:00
f.writerow(
[p.id, p.code, p.name, p.points, "lqdoj.edu.vn/problem/" + p.code]
)
2022-04-12 02:18:01 +00:00
class Command(BaseCommand):
2022-05-14 17:57:27 +00:00
help = "generate data for ML"
2022-04-12 02:18:01 +00:00
def handle(self, *args, **options):
gen_users()
gen_problems()
2022-05-14 17:57:27 +00:00
gen_submissions()