Loading

Paste #pjmvsiovm

  1. #/usr/bin/env python3
  2. import datetime as dt
  3. import pandas as pd
  4. import math
  5.  
  6. class Calendar(object):
  7.     # :(
  8.     modulecodemap = (
  9.         ("COM00001C-A", "ICAR"),  # Introduction to Computer Architectures 15
  10.         ("COM00001H-A", "ARTS"),  # Analysable Real-Time Systems 20
  11.         ("COM00001I-A", "ARIN"),  # Artificial Intelligence 20
  12.         ("COM00002H-A", "CVIS"),  # Computer Vision 20
  13.         ("COM00002I-A", "COCO"),  # Computability & Complexity 10
  14.         ("COM00003C-A", "HACS"),  # Human Aspects of Computer Science 20
  15.         ("COM00003H-A", "EMBS"),  # Embedded Systems Design & Implementation 20
  16.         ("COM00004H-A", "ENAR"),  # Enterprise Architecture 20
  17.         ("COM00005C-A", "MFCS"),  # Mathematical Foundations of Computer Science 20
  18.         ("COM00005H-A", "GRAT"),  # Computing by Graph Transformation 20
  19.         ("COM00005I-A", "POPL"),  # Principles of Programming Languages 20
  20.         ("COM00006C-A", "NUMA"),  # Numerical Analysis 10
  21.         ("COM00006H-A", "ICOT"),  # Information & Coding Theory 20
  22.         ("COM00007C-A", "TPOP"),  # Theory and Practice of Programming 20
  23.         ("COM00007H-A", "INCA"),  # Introduction to Neural Computing & Applications 20
  24.         ("COM00007I-A", "SYAC"),  # Systems Software & Compilers 30
  25.         ("COM00008C-A", "SKIL"),  # Skills, Knowledge & Independent Learning 5
  26.         ("COM00008I-A", "SEPR"),  # Software Engineering Project 30
  27.         ("COM00009C-A", "FESC"),  # Foundations in Electronics, Signals and Circuits 20
  28.         ("COM00009H-A", "MAIG"),  # Multi-Agent Interaction & Games 20
  29.         ("COM00009I-A", "VIGR"),  # Vision & Graphics 10
  30.         ("COM00010C-A", "PROM"),  # Programming of Micro-controllers 10
  31.         ("COM00010H-A", "MLAP"),  # Machine Learning & Applications 20
  32.         ("COM00012H-A", "PCOC"),  # Programming: Correctness by Construction 20
  33.         ("COM00012I-A", "EMPR"),  # Embedded Systems Project 30
  34.         ("COM00013H-A", "PRBE"),  # Project: Computer Science with Embedded Systems 40
  35.         ("COM00014H-A", "PRBM"),  # Project: Mathematics and Computer Science 40
  36.         ("COM00015H-A", "PRBX"),  # Project: Computer Science 40
  37.         ("COM00016H-A", "IAPT"),  # Interactive Application Programming Techniques 20
  38.         ("COM00042M-A", "QIPR"),  # Quantum Information Processing 10
  39.         ("COM00045M-A", "QUCO"),  # Quantum Computation 10
  40.         ("COM00063M-A", "SAVE"),  # Static Analysis & Verification 10
  41.         ("COM00066M-A", "ALAS"),  # Adaptive & Learning Agents 10
  42.         ("COM00068M-A", "COPR"),  # Constraint Programming 10
  43.         ("COM00069M-A", "CRSY"),  # Critical Systems 10
  44.         ("COM00071M-A", "EVCO"),  # Evolutionary Computation 10
  45.         ("COM00073M-A", "GPIG"),  # Group Project (Integrated Masters) 20
  46.         ("COM00080M-A", "PRIS"),  # Individual Project - Computer Science with Business Enterprise Systems 50
  47.         ("COM00081M-A", "PRIA"),  # Individual Project - Computer Science with Artificial Intelligence 50
  48.         ("COM00081M-A", "PRIY"),  # Individual Project - Computer Systems & Software Engineering 50
  49.         ("COM00082M-A", "PSEC"),  # Topics in Privacy & Security 10
  50.         ("COM00085M-A", "SMAT"),  # Software Measurement & Testing 10
  51.         ("COM00087M-A", "SYAR"),  # Systems Architecture 10
  52.         ("COM00104M-A", "AVIS"),  # Advanced Computer Vision 10
  53.         ("COM00106M-A", "NLPR"),  # Natural Language Processing 10
  54.         ("COM00111M-A", "MODE"),  # Model-Driven Engineering 10
  55.         ("ELE00061M-A", "EVHW"),  # Evolvable Hardware 10
  56.         ("ELE00068M-A", "SWIN"),  # Swarm Intelligence 10
  57.     )
  58.  
  59.     def __init__(self, filename):
  60.         cols = ["start-date", "start-time", "end-date", "end-time",
  61.                 "activity-type", "activity-name", "taught-by", "location",
  62.                 "directions", "module", "activity-notes", "contact-details"]
  63.         self.df = pd.read_csv(filename, skipinitialspace=True, index_col=False,
  64.                               skiprows=1, dayfirst=True, names=cols)
  65.  
  66.         # Merge the date/time fields
  67.         self.df["start-date"] = pd.to_datetime(self.df["start-date"] + ' ' + self.df["start-time"], dayfirst=True)
  68.         self.df["end-date"] = pd.to_datetime(self.df["end-date"] + ' ' + self.df["end-time"], dayfirst=True)
  69.         self.df = self.df.drop("start-time", 1).drop("end-time", 1)
  70.         self.df.index = self.df["start-date"]
  71.  
  72.         # Repurpose activity-name into something useful
  73.         self.df = self.df.rename(columns={"activity-name": "group-num"})
  74.         self.df["group-num"] = self.df["group-num"].str.split("/").str[-1].str.split().str[0]
  75.  
  76.         # Get rid of useless fields
  77.         self.df = self.df.drop(["directions", "activity-notes",
  78.                                 "contact-details"], 1)
  79.  
  80.         # Get rid of rows that are in the past
  81.         self.df = self.df[dt.datetime.now().strftime("%Y-%m-%d %H:%M") <= self.df["end-date"]]
  82.  
  83.         self.df = self.df.sort()
  84.  
  85.     def get_long_code(self, code):
  86.         """raises StopIteration"""
  87.         return next((x[0] for x in self.modulecodemap if x[1] == code))
  88.  
  89.     def get_next_lecture(self, code):
  90.         code = code.upper()
  91.         try:
  92.             longcode = self.get_long_code(code)
  93.         except StopIteration:
  94.             # Fine, we'll try with the existing code
  95.             longcode = code
  96.         module = self.df[self.df["module"] == longcode]
  97.         try:
  98.             first = module.iloc[0]
  99.         except:
  100.             return "Untimetabled module"
  101.         timestr = ""
  102.         if first["start-date"] is not dt.datetime.today():
  103.             timestr += first["start-date"].strftime("%Y-%m-%d")
  104.         timestr += first["start-date"].strftime(" %H:%M")
  105.  
  106.         group = ""
  107.         if (module[(module["activity-type"].apply(str) == first["activity-type"]) &
  108.                    (module["group-num"].apply(str) != first["group-num"])].shape[0] != 0):
  109.             group += "Group{} ".format(first["group-num"])
  110.  
  111.         lecturers = first["taught-by"].split('  ')  # double space
  112.         lecturerstr = lecturers[0]
  113.         for lecturer in lecturers[1:]:
  114.             if len(lecturer) + len(lecturerstr) + 3 <= 60:
  115.                 lecturerstr += ", " + lecturer
  116.             else:
  117.                 lecturerstr += " & others"
  118.                 break
  119.  
  120.         location = first["location"]
  121.         if math.isnan(location):
  122.             location = "unspecified"
  123.         return "{} {activity-type} {}in {} at {} with {}".format(code, group, location, timestr, lecturer, **first)
  124.  
  125.  
  126. cal = Calendar("cs-calendar.csv")
  127. print(cal.get_next_lecture("skil"))
  128.  
  129. #for _, row in cal.df.iterrows():
  130. #    if "Runciman" not in str(row["taught-by"]): continue
  131. #    print(row)
  132. #    input()
  133.  

Comments