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

43 lines
1.3 KiB
Python
Raw Normal View History

from typing import Generator, Any
from random import randint
import pandas as pd
from .exceptions import InvalidNameOrTypeHeaderException
from ..students.models import Student
def check_columns(df: pd.DataFrame) -> bool:
headers = set(df.keys().values)
2022-06-12 21:20:11 +02:00
columns = ['NAZWISKO','IMIE','INDEKS','PESEL','EMAIL']
if len(headers - set(columns)) != 0:
return False
flag = True
2022-06-12 21:20:11 +02:00
col_types = ['object', 'object', 'int', 'float64', 'object']
for name, col_type in zip(columns, col_types):
if not str(df.dtypes[name]).startswith(col_type):
flag = False
break
return flag
def parse_csv(file, mode) -> Generator[Student, Any, None]:
df = pd.read_csv(file)
2022-06-12 21:20:11 +02:00
# if not check_columns(df):
# raise InvalidNameOrTypeHeaderException
2022-06-12 21:20:11 +02:00
students = (Student(last_name = dict(item.items())['NAZWISKO'],
first_name = dict(item.items())['NAZWISKO'],
index = dict(item.items())['INDEKS'],
pesel = str(int(dict(item.items())['PESEL'])) if not pd.isna(dict(item.items())['PESEL']) else '',
email = dict(item.items())['EMAIL'],
mode=mode)
for _, item in df.iterrows())
2022-06-12 21:20:11 +02:00
return students