I'm a notetaker for my college, and as part of my duties I have to upload my notes daily. As I type them, this is something I can do from my computer, without needing a scanner. Generally, this involves:

  1. Find the .pdf with the "highest number" (The .pdf's are named <word>XY.pdf where XY is a number. I number them starting at 01 on the first lecture and going up. I want to get the most recent one).

  2. Copying the .pdf to a specific folder (I'll need to rename the .pdf, to something else, so working with a copy makes more sense).

  3. Renaming it in the following format - SUBJ XYZ MM-DD-YYYY.pdf, where SUBJ is the subject classification (like MATH, PHY, MUS, etc), and XYZ is the course number.

  4. Opening a specific webpage, and the GUI file manager so I can "drag and drop" the file correctly (I don't know how to do this in a better way, the webpage has a "select a file" and then "click to upload", I haven't seen any protocol I can upload it without this kind of stuff).

  5. Move the renamed folder into some subfolder of the "specific folder" mentioned before, so "clean things up". Deleting the file would also probably be fine, but this is the implementation I currently have.

There are various improvements I've considered. One is insteading of appending the current date, looking for the most recent date I had that class (which is usually something like MWF or TuTh).

The files are generically stored in the following directory: /home/<user>/classes/Sp17/<course name>-Spring-2017/notes/<fileXY.pdf>. In this particular example, <course name> is cryptography, and files are named crypt15.pdf as an example.

I'm wondering if people have generic suggestions for how to clean this up. Some things (like the try/except used) seem like it's pretty obvious I should change, and I just need to sit down and work on it.

import time
import os
import glob
import shutil
import webbrowser
import atexit
import subprocess
import sys

FILE_PATHS = ["/home/<user>/classes/Sp17/", "-Spring-2017/notes/"]
DEST_PATH = "/home/<user>/Desktop/Notetaking/"

def find_ext(dr, ext):
    Return a list of all files with a given file extension in a folder.
    return glob.glob(os.path.join(dr, "*." + ext))

def move_renamed_file(old_file_path, new_file_path):
    Move the staged .pdf to an "archive" folder. Written to be registered with atexit so it runs after the program is done.

    shutil.move(old_file_path, new_file_path)

def get_number_in_string(string):
    Given a string that contains a number, returns that number.

    String expected to be of the form <word>XY.pdf, where <word> contains no numbers.

    Try/except hacking needed for if the string doesn't contain a number to stop int("") error.
        return int(''.join(list(filter(str.isdigit, string))))
    except ValueError:
        return 0

class Course:

    def __init__(self, class_name, class_schedule, subject, course_number, moodle_id):
        self.directory = "".join([FILE_PATHS[0], class_name, FILE_PATHS[1]])
        self.class_schedule = class_schedule
        self.subject = subject
        self.course_number = str(course_number)
        self.moodle_id = str(moodle_id)

    def make_new_name(self):
        Makes a new name of form SUBJ NUM MM-DD-YYYY.pdf, where SUBJ is the department the class is in (i.e. MATH), NUM is the three-digit class number, and MM-DD-YYYY is the most recent date.

        Idea to improve - make MM-DD-YYYY the most recent date from the class_schedule.
        date = time.strftime("%x").lstrip("0").replace("/", "-")
        return "".join([self.subject, " ", self.course_number, " ", date, ".pdf"])

    def find_highest_pdf(self):
        Finds the .pdf with file name containing the highest number in the directory.
        pdfs = find_ext(self.directory, "pdf")
        last_backslash = pdfs[0].rfind("/")
        for num in range(len(pdfs)):
            pdfs[num] = pdfs[num][last_backslash + 1:]
        self.target_pdf = pdfs[-1]
        return "".join([self.directory, self.target_pdf])

    def move_file(self, file_name):
        Takes a file, renames it, and moves it to the "staging" folder.
        new_file_name = self.make_new_name()
        dest = os.path.join(DEST_PATH, new_file_name)
        shutil.copy(file_name, dest)

    def open_browser(self):
        Opens the webpage and "staging" folder to allow drag and drop of the .pdf to the website.
            "".join(["https://moodle.<college>.edu/mod/data/view.php?id=", self.moodle_id]))
        subprocess.Popen(["xdg-open", DEST_PATH])

    def upload_notes(self):
        Runs all the prior functions in the right order.
        pdf = self.find_highest_pdf()
        new_file_name = self.make_new_name()
        atexit.register(move_renamed_file, "".join([DEST_PATH, new_file_name]), "".join(
            [DEST_PATH, self.course_number, " archive/", new_file_name]))
        input("Press any key to exit")

if __name__ == "__main__":
    crypto = Course("cryptography", "MWF", "MATH", 101, 00000)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.