source code of /small-scripts/school-stuff/homework.py

Last modified
Lines 77
Indexable No

Parent directory Download CGIread sitemap Main page

Quick links: add_homework nthday

  1. #!/usr/bin/python
  2. import pprint
  3. import time
  4. import subprocess
  5. assert __name__ != "__main__"
  6. adj = int(time.strftime('%w')) - 1
  7. def nthday(n):
  8.     args = ['date', '--date={}day'.format(n), '+%F']
  9.     proc = subprocess.Popen(args, stdout=subprocess.PIPE)
  10.     out, err = proc.communicate()
  11.     return out[:-1]
  12. homework = eval(open('/var/www/PG/homework').read())
  13. homework.sort(key=lambda x: x['time'])
  14. classes = eval(open('/var/www/PG/classes').read())
  15. times = [       # Must be 4 chars.
  16.     '0800',
  17.     '0925',
  18.     '1110',
  19.     '1230',
  20.     '1350',
  21. ]
  22. known_classes = set()
  23. for row in classes:
  24.     for col in row:
  25.         if col != '':
  26.             known_classes.add(col)
  27. def add_homework(hw_time, hw_class, hw_homework, source):
  28.     if hw_homework is not None:
  29.         if not (hw_time is None and hw_class not in known_classes):
  30.             # Generate hw_time from hw_class if needed.
  31.             if hw_time is None:
  32.                 flat_classes = []
  33.                 # It will look for the day by first finding now, and then
  34.                 # find the next class which may be the next week.
  35.                 for x in range(2):
  36.                     for day, col in enumerate(zip(*classes)):
  37.                         for index, row in enumerate(col):
  38.                             sortable = '{}:{}'.format(
  39.                                 nthday(x*7 + day - adj),
  40.                                 times[index]
  41.                             )
  42.                             flat_classes.append((sortable, row))
  43.                         # Don't need to add Saturday and Sunday.
  44.                 # Find today.
  45.                 flat_classes = list(filter(
  46.                     lambda x: x[0] > time.strftime('%F:%H%M'),
  47.                     flat_classes
  48.                 ))
  49.                 # Find next time.
  50.                 flat_classes = list(filter(
  51.                     lambda x: x[1] == hw_class,
  52.                     flat_classes
  53.                 ))
  54.                 date, classtime = flat_classes[0][0].split(':')
  55.                 hw_time = '{}:{}'.format(date, times.index(classtime))
  56.             
  57.             record = {
  58.                 'time':         hw_time,
  59.                 'class':        hw_class,
  60.                 'src':          source,
  61.                 'homework':     hw_homework,
  62.             }
  63.             if record not in homework:
  64.                 homework.append(record)
  65.                 homework.sort(key=lambda x: x['time'])
  66.     
  67.     # Write back.
  68.     f = open('/var/www/PG/homework', 'w')
  69.     f.write(pprint.pformat(homework))
  70.     f.close()