Last active
April 8, 2020 10:53
-
-
Save aman-roy/e06d5b85d5faeff0801d6622911ed678 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import matplotlib.pyplot as plt | |
from matplotlib.pyplot import xticks, yticks | |
# marks to band conversion for listening | |
def get_band_listening(marks): | |
if marks>=39: return 9.0 | |
elif marks >= 37: return 8.5 | |
elif marks >= 35: return 8.0 | |
elif marks >= 33: return 7.5 | |
elif marks >= 30: return 7.0 | |
elif marks >= 27: return 6.5 | |
elif marks >= 23: return 6.0 | |
elif marks >= 19: return 5.5 | |
elif marks >= 15: return 5.0 | |
elif marks >= 13: return 4.5 | |
elif marks >= 10: return 4.0 | |
elif marks >= 8: return 3.5 | |
elif marks >= 6: return 3.0 | |
elif marks >= 4: return 2.5 | |
else: return 0 | |
# marks to band conversion for reading | |
def get_band_reading(marks): | |
if marks >= 39: return 9.0 | |
elif marks >= 37: return 8.5 | |
elif marks >= 35: return 8.0 | |
elif marks >= 33: return 7.5 | |
elif marks >= 30: return 7.0 | |
elif marks >= 27: return 6.5 | |
elif marks >= 23: return 6.0 | |
elif marks >= 19: return 5.5 | |
elif marks >= 15: return 5.0 | |
elif marks >= 13: return 4.5 | |
elif marks >= 10: return 4.0 | |
elif marks >= 8: return 3.5 | |
elif marks >= 6: return 3.0 | |
elif marks >= 4: return 2.5 | |
else: return 0 | |
# Check for any error in command line arguments | |
def check_cl_args(): | |
if len(sys.argv) < 2 or len(sys.argv) > 3: | |
print("Need 2 or 3 arguments") | |
exit(0) | |
if not (sys.argv[1] == "r" or sys.argv[1] == "l"): | |
print("second argument should be 'r' or 'l'.") | |
exit(0) | |
if len(sys.argv)==2: return | |
third_arg = sys.argv[2] | |
if not third_arg.isdigit(): | |
print("third argument must be an positive integer.") | |
exit(0) | |
if int(third_arg) > 40: | |
print("You can't score more than 40 in IELTS.") | |
exit(0) | |
# Create data files if it's not there | |
def create_data_files(): | |
open("reading.txt", 'a').close() | |
open("listening.txt", 'a').close() | |
# Utility function for horizontal line | |
def get_line(): | |
print("------------------------------------") | |
# dictionary for the lingo | |
my_dict = {'l': 'listening', 'r': 'reading'} | |
def print_ielts_details(marks, ielts_paper): | |
print(f"Performance for IELTS {my_dict[ielts_paper]}") | |
get_line() | |
if(ielts_paper=="r"): | |
band = get_band_reading(sum(marks)/len(marks)) | |
else: | |
band = get_band_listening(sum(marks)/len(marks)) | |
print(f"Average marks: {sum(marks)/len(marks):.2f}, i.e. {band} band.") | |
print(f"Highest: {max(marks)}") | |
print(f"Lowest: {min(marks)}") | |
get_line() | |
print(f"Recent test #{len(marks)} marks: {marks[-1]}, i.e. {get_band_listening(marks[-1])} band.") | |
get_line() | |
def print_graph(marks, ielts_paper): | |
plt.bar(range(1, len(marks)+1), marks) | |
plt.xlabel("test #") | |
plt.ylabel("marks") | |
plt.title(f"IELTS statistics for {my_dict[ielts_paper]}") | |
xticks(range(1, len(marks)+1)) | |
yticks(range(0,max(marks)+2, 2)) | |
plt.grid() | |
plt.show() | |
def main(): | |
check_cl_args() | |
create_data_files() | |
arg_len = len(sys.argv) | |
ielts_paper = sys.argv[1] | |
if ielts_paper == 'l': | |
file = "listening.txt" | |
else: | |
file = "reading.txt" | |
marks = [int(i) for i in open(file, "r").read().split("\n") if len(i)] | |
if arg_len == 2: | |
if len(marks) == 0: | |
print("No data available.") | |
else: | |
print_ielts_details(marks, ielts_paper) | |
print_graph(marks, ielts_paper) | |
else: | |
cur_marks = sys.argv[2] | |
fhand = open(file, 'a') | |
fhand.write(f"{cur_marks}\n") | |
fhand.close() | |
print(f"{cur_marks} is added to the {my_dict[ielts_paper]} database.") | |
if ielts_paper == "r": | |
print(f"You got {get_band_reading(int(cur_marks))} band in {my_dict[ielts_paper]}.") | |
else: | |
print(f"You got {get_band_listening(int(cur_marks))} band in {my_dict[ielts_paper]}.") | |
if __name__=='__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment