system-pri/backend/app/coordinator/utils.py

38 lines
984 B
Python
Raw Normal View History

from typing import List
from random import randint
import pandas as pd
from .exceptions import InvalidNameOrTypeHeaderException
from ..students.models import Student
def check_columns(df: pd.DataFrame, columns: List[str]) -> bool:
flag = True
col_types = ['object', 'object', 'int', 'int']
print(df.dtypes['first_name'])
for name, col_type in zip(columns, col_types):
if name not in df and df.dtypes[name].startswith(col_type):
flag = False
break
return flag
def parse_csv(file) -> List[Student]:
df = pd.read_csv(file)
columns = ['first_name', 'last_name', 'index', 'mode']
if not check_columns(df, columns):
raise InvalidNameOrTypeHeaderException
students = []
for _, item in df.iterrows():
data = {}
for c in columns:
data[c] = item[c]
data['email'] = f'student{randint(1, 300_000)}@gmail.com'
students.append(Student(**data))
return students