Which is faster?

2005-01-26 Thread Aggelos I. Orfanakos
Any idea which of the following is faster?

'a/b/c/'[:-1]

or

'a/b/c/'.rstrip('/')

Thanks in advance.

P.S. I could time it but I thought of trying my luck here first, in
case someone knows already, and of course the reason.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Which is faster?

2005-01-26 Thread Aggelos I. Orfanakos
Yes, I could do the timing myself. Sorry if this was impolite -- it was
not in my intentions. The main reason I asked was about the reason.
Thanks.

-- 
http://mail.python.org/mailman/listinfo/python-list


python: can't open file 'timeit.py'

2005-01-28 Thread Aggelos I. Orfanakos
Hello.
Under Gentoo Linux, I issue:

$ python timeit.py
python: can't open file 'timeit.py'
$ ls -al /usr/lib/python2.3/timeit.py
-rw-r--r--  1 root root 9833 Oct 19 02:17 /usr/lib/python2.3/timeit.py

But if I specify the full path, it works:

$ python /usr/lib/python2.3/timeit.py -n 1 "pass"
1 loops, best of 3: 3.1 usec per loop

Any ideas how can I fix this? I think it may have to do with where
Python looks for modules, but I am not sure.

Thanks in advance.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python: can't open file 'timeit.py'

2005-01-28 Thread Aggelos I. Orfanakos
OK, the symbolic link solved the "problem". I thought that there was
something wrong with my Python configuration; that's why I asked in the
first place.

Thanks.

-- 
http://mail.python.org/mailman/listinfo/python-list


Regarding exception handling

2005-01-30 Thread Aggelos I. Orfanakos
Hello.

In a program, I want to ensure that a socket closes (so I use try ...
finally), but I also want to catch/handle a socket exception. This is
what I have done:

try:
try:
s = ... # socket opens

# various code ...
except socket.error, x:
# exception handling
finally:
s.close() # socket closes

Is there a more "elegant" or "proper" way to do this? Or the above code
is OK?

Thanks in advance.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regarding exception handling

2005-01-30 Thread Aggelos I. Orfanakos
(I don't know why, but indentation was not preserved once I posted.)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regarding exception handling

2005-01-30 Thread Aggelos I. Orfanakos
Thanks. This should now be OK:

#try:
#try:
#s = ... # socket opens
#
## various code ...
#except socket.error, x:
## exception handling
#finally:
#s.close() # socket closes

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regarding exception handling

2005-01-30 Thread Aggelos I. Orfanakos
I need it because the "various code" may raise other exceptions (not
related to sockets). In such case, the "except socket.error, x:" won't
catch it, but thanks to the "finally:", it is sure that the socket will
close.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regarding exception handling

2005-01-30 Thread Aggelos I. Orfanakos
Good point, but with your way, if "s = ... # socket opens" fails, then
nothing will catch it. What I usually do is what I wrote above (place
it below the 2nd try), and when attempting to close it, first use an if
like: "if locals().has_key('s'):".

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regarding exception handling

2005-01-30 Thread Aggelos I. Orfanakos
Coming from C, I think that it's generally a good programming practice
to make sure everything you create, closes; whether it's about a socket
or a file. This may not be the case with Python though. To be honest,
leaving this task to the garbage collector doesn't sound like a good
idea to me (since the language gives you the means to do it yourself).

-- 
http://mail.python.org/mailman/listinfo/python-list