Since I'm pressed for time tonight, this will be quick. While coding at home, I like to enjoy a good cup of tea. However, nearly without fail, I leave the tea bag in too long and get a cup that's brewed too strong. I'm sure there are other solutions out there, but the fact of the matter is that I could code up a tea timer faster than a Google search and testing various apps would take. Plus, it's good exercise and the kind of code snippet I would've liked to have seen when I was learning Python.
#!/usr/bin/env python import time def tea_timer(minutes): time.sleep(minutes * 60) return "Tea's done!" if __name__ == "__main__": import sys if len(sys.argv) != 2: print "Usage: %s
The code itself is straightforward but what's interesting are the modules used and the way the code is written. We're only using
time module (which is very useful for things beyond
sleep) and the
sys module for checking command line arguments and exiting.
The Python bits are shebang (portable to other computers which may store their Python in a different spot), the
__name__ check (see if the script is being imported or run from the command line) and the
__file__ variable, which provides the filename of the current file (no hardcoding the script name).
In addition to being a standalone script, it's built so it could be integrated into anything (a small Tk application, a Django view) via an import. The code within the
__name__ check is simply ignored and the
tea_timer function (simple as it is) can be called.
There will be a better post tomorrow, after I've recharged a bit. I've got a couple big posts in the works that should be a touch juicier.