Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 125 lines (97 sloc) 3.26 KB
#!/usr/local/bin/python3
import pandas as pd
import sys
import os
from io import StringIO
import re
from random import randint
def main():
'''Takes data stream (string), writes results to FWF on Desktop'''
assert sys.stdin, "No input"
results_string = StringIO(sys.stdin.read())
# Import into DataFrame
columns = ["Student Name", "Month", "Day", "Year", "ID", "Special Codes", "Total Score", "Grade"]
results = pd.read_csv(
results_string,
usecols = columns,
dtype = object,
na_values = [" ", ' ', 'BLANK']
)
## Added these when I wrote scripts to automatically fix errors from email in SIS
results[["Month", "Day", "Year"]] = results[["Month", "Day", "Year"]].fillna('01')
results['ID'] = results['ID'].fillna(str(randint(100000, 999999)))
results["ID"] = results["ID"].apply(lambda x: '{0:0<10}'.format(x))
results['Student Name'] = results['Student Name'].fillna('FORGOT YOUR NAME')
# CLEAN
results["Test Date"] = pd.to_datetime("today")
results["Month"] = results["Month"].apply(lambda x: '{0:0>2}'.format(x))
results["Day"] = results["Day"].apply(lambda x: '{0:0>2}'.format(x))
results["Year"] = results["Year"].apply(lambda x: '{0:0>2}'.format(x))
results["Total Score"] = results["Total Score"].str.replace('.00', '')
## Checking for missing language codes
assert results.isnull().values.any() == False, "There is NaN values. Did you check Spanish?"
# Checking for duplicate ID numbers
dupe_check = results.duplicated(subset = "ID")
dupes = results.loc[dupe_check.index[dupe_check == True], 'Student Name'].to_string().replace('\n', ', ')
assert dupe_check.any() == False, "There are duplicate results: " + dupes
# Adding columns to DataFrame that are SIS friendly
results["Grade"] = results["Grade"].apply(nums)
results["RNumerals"] = results["Grade"].apply(romans)
results["Special Codes"] = results["Special Codes"].apply(lang_code)
# Writing data
file = open(make_file_path(), "w")
for lab, row in results.iterrows() :
string = makestring(row)
if len(string) == 61:
file.write(string + "\n")
else:
print(string)
string = string + "@"
file.write(string + "\n")
file.close()
def make_file_path():
date_string = pd.to_datetime("today").strftime("%Y-%m-%d")
file_name = date_string + "-sis_upload.txt"
user = os.path.expanduser('~')
file_path = os.path.join(user, 'Desktop', file_name)
return file_path
# Functions for converting data into SIS-friendly format
def nums(x):
if x == "MET" :
return "4"
else :
return x
def romans(x):
if x == "1" :
return " I "
elif x == "2" :
return " II "
elif x == "3" :
return "III "
else :
return "MET "
def lang_code(x):
'''For dealing with typos in the Special Codes section'''
lang_reg = r'^\s*0\s*0\s*(\d{2}|\d)$'
match = re.search(lang_reg, x)
lang = re.sub(lang_reg, r'\1', match.group())
code = " 0 0" + lang.rjust(2)
return code
# Function for converting DataFrame rows into rows in fixed-width file
def makestring(row):
string = str(
str("%-20s" % row["Student Name"]) +
row["ID"] +
str(row["Total Score"]).rjust(3) +
" 0 0 0 " +
row["Special Codes"][-2:] +
row["Grade"] +
row["RNumerals"] +
row["Test Date"].strftime("%m/%d/%y") +
row["Month"] +
row["Day"] +
row["Year"]
)
return string
if __name__ == '__main__':
main()