Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My code is designed to simply trigger alarms; it is not supposed to be used by anyone but me.

import subprocess,time,threading, winsound, sys, basepipe
commands = ''
def alarm_checker(alarms):
    """This will be the main threaded process, and will be the meat of the program."""
    global commands
    hours = [int(a.split(":")[0]) for a in alarms]
    mins = [int(a.split(":")[1]) for a in alarms]
    while True:
        localtime = time.localtime()
        for hour,minute in zip(hours,mins):
            if localtime.tm_hour == hour and localtime.tm_min == minute:
                guiserver,guiclient = basepipe.create_double()
                process = subprocess.Popen(r"c:\python34\pythonw.exe c:\python34\guibutton.py",stdout=guiclient)
                while guiserver.read() == '':
                    winsound.Beep(1000,2000)
                hours.remove(hour)
                mins.remove(minute)
        if commands != '':
            for y in commands.split("
"):
                try:
                    exec(y)
                except BaseException as e:
                    sys.stderr.write(str(e))
            commands = ''
        time.sleep(2)
if __name__ == "__main__":
    today = time.localtime().tm_wday
    everyday = ['6:55','7:15','8:12']
    advising = ['13:25','13:55']
    tuethur = ['10:55']
    monday = everyday + advising + ['15:00']
    tuesday = everyday + tuethur
    wednesday = everyday + ['3:50']
    thursday = everyday + advising + tuethur
    friday = everyday
    week = [monday,tuesday,wednesday,thursday,friday,['25:00'],['25:00']]
    thread = threading.Thread(target=alarm_checker,args=(week[today],))
    thread.start()

The basepipe program is a wrapper around windows named pipes in Python. All it does is attempt to implement them in the same way as any other type of IO. One of the peculiarities of windows named pipes is that client read operations are always blocking, while server read operations always raise an error when there is no data in the buffer. I changed this error to simply return ''. create_double() creates a pair of server/client objects. The guibutton.py program does nothing other than open a tcl/tk window. Winsound provides direct access to the sound card, and 1000,2000 means 1000 hertz for 2 seconds, which is annoying as hell.

share|improve this question
up vote 2 down vote accepted

Instead of splitting each item in alarms to hours and minutes and then zipping them again like this:

hours = [int(a.split(":")[0]) for a in alarms]
mins = [int(a.split(":")[1]) for a in alarms]

for hour,minute in zip(hours,mins):
    # ...

You could do it in one go without zipping:

hours_minutes = [[int(x) for x in a.split(":")] for a in alarms]
for hour, minute in hours_minutes:
    # ...

It's recommended to follow the coding style guide PEP8.

For example you could simplify things like this:

    if commands != '':

To this:

    if commands:

It's also recommended to avoid multiple imports on the same line like this:

import subprocess,time,threading, winsound, sys, basepipe

And it would be better to move all code out of the if __name__ == "__main__": guard into a def main(): method to avoid variable shadowing in the other methods called from there, as the code in the if guard is in global scope.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.