You are not logged in.

#1 2008-09-24 08:40:54

BetterLeftUnsaid
Member
From: My Happy Place
Registered: 2007-11-04
Posts: 78

Python Error Handling

And in theory, this post also contains a script to tile windows in Openbox.  But that's beside the point.

I was just curious as to how often try/except clauses are used in Python.  I was writing this script (usually I'm too lazy to even start any sort of error handling), and realized that a lot of what I was doing was within a try/except clause.  I'm pretty sure I could remove at least some of them, but anybody have an idea on how to prevent errors without a try/except?

Also, it seems weird to me to keep using subprocess to call a bunch of nonPython commands.  As far as I can tell, there's no better way to get the information that I need, but I'm also not fully aware of Python's full capabilities.

I dunno, I guess I was just trying to overall improve my failscripting ability, and just wondered if some of you Archers had any suggestions.

0.4: http://pastebin.com/f1d65c591

EDITS: Yay updated script + vertical and grid tiling.  So now it's the topic is kind of odd because it's almost trying to fit in with the Community Contributions thread.

Last edited by BetterLeftUnsaid (2008-09-24 23:39:43)

Offline

#2 2008-09-24 15:08:31

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Python Error Handling

Generally its better to catch exceptions with classes:

eg:

try:
    x, y = int(x), int(y)
except ValueError:
    # exception case

that way if there's an error you don't expect it will fail with a message that makes sense instead of just getting lost in a jungle of caught exceptions. On that note, in python, I sometimes find its often better to let the default exceptions get raised instead of handling them, although this is really dependent on the context. What's the point of catching an exception if all you're going to do is print an error message and exit anyway?

In many cases you can avoid catching exceptions by using an if statement. For example, instead of catching the exception on openboxrc, you could use if os.path.isfile(filename): before opening the file. You could also replace this code:

margin_top = int(re.findall(r"<top>(\d*)</top>", openbox_rc)[0])

with a bit more readable:

match = re.findall(r"<top>(\d*)</top>", openbox_rc)

if match:
    margin_top = match[0]

On a related topic, that regex could be improved; use \d+ to ensure there is at least one digit inside the group, and it would fail on code with whitespace. for example, this is legitimate xml:

<top>
    50
</top>

You can probably allow for this case by putting optional whitespace around the group:

"<top>\w*(\d+)\w*</top>"

Although I can't remember off the top of my head if \w properly matches linebreaks; you might want to find \s or something instead.

In addition, since you're only looking for one entry in this case and you pick the first one anyway, you can use re.search instead of re.findall

As far as the subprocess calls, you can probably get the screen resolution using pygame, tkinter, wxpython, or other toolkits. You might also be able to use pyxlib directly. It is possible there is a python interface to wmctrl as well, but I have no knowledge of that.

You should probably replace sys.exit() in your fail method with sys.exit(1). In shell commands, 0 indicates a successful exit and any other command indicates an error condition. This could be useful if you or someone wants to use this script in a bash shell script.

Dusty

Offline

#3 2008-09-24 16:04:16

BetterLeftUnsaid
Member
From: My Happy Place
Registered: 2007-11-04
Posts: 78

Re: Python Error Handling

Awesome.  Thanks a lot Dusty.  New updated code on first post, if anybody is interested.

Last edited by BetterLeftUnsaid (2008-09-24 18:19:20)

Offline

#4 2008-09-24 22:09:06

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: Python Error Handling

\w matches word characters, as in [a-zA-Z0-9_]. Dusty may have meant \W, which matches the complement of \w. But \s is probably best for your purposes: it matches spaces, tabs, \r, \n, \f and \v.

Offline

Board footer

Powered by FluxBB