Re: REALLY need help with iterating a list.

2007-06-11 Thread alg
Reverse iteration should do the trick, if I understand your problem:

for server in reversed(serverlist):
...
else:
serverlist.remove(server)

On Jun 11, 11:30 am, Radamand <[EMAIL PROTECTED]> wrote:
> This has been driving me buggy for 2 days, i need to be able to
> iterate a list of items until none are left, without regard to which
> items are removed. I'll put the relevant portions of code below,
> please forgive my attrocious naming conventions.
> Basically i'm trying to spin up some subprocesses that will ping a
> group of servers and then wait until all of them have completed (good
> or bad), store the ping result and the return code, and move on.
> The problem comes in the second block, when i try to iterate the
> list of servers and remove the ones that are finished, for some reason
> Python appears to re-index the list when I remove an item and the next
> step through the loop it cant find the item its expecting because the
> indexes have changed.
> Any assistance would be appreciated...
>
> =
> for server in serverlist:
> ping[server] = subprocess.Popen("ping -c 1 " + str(server) + " 5",
> shell=True, stdout=subprocess.PIPE)
>
> while len(serverlist) > 0:
> for server in serverlist:
> if ping[server].returncode==None:
> ping[server].poll()
> else:
> pingresult[server] = ping[server].stdout.read()
> pingreturncode[server] = ping[server].returncode
> serverlist.remove(server)


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


Re: Retrieving a stacktrace from an exception object / forwarding an exception

2007-06-22 Thread alg
Use the traceback module:

def log_exception():
"""This function will log the current exception's traceback
   to logfile
"""
import traceback
f = open("logfile", 'a')
traceback.print_exc(32, f)
f.close()

def my_func():
raise Exception

def wrapper():
try:
my_func()
except Exception, e:
log_exception()
raise e


On Jun 21, 11:54 pm, Samuel <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I am trying to wrap a function that throws an exeption in such a way
> that the stacktrace is logged into a file and the exception is
> forwarded after that. For example:
>
> ---
> def my_func():
> raise Exception("hello")
>
> def wrapper():
> try:
> my_func()
> except Exception, e:
> f = open("logfile", 'a')
> f.write(e.stacktrace())
> raise e
>
> wrapper() # should throw the exception with a stacktrace showing
> my_func()
> ---
>
> Any idea if and how this can be done?
>
> -Samuel


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