reloading all modules

2009-03-09 Thread Noam Aigerman
Hi,

Is there a way to use the reload() func or something else, to refresh
all of the modules you have imported? 

I don't care whether the module that will run this code will be reloaded
or not, so whichever is the easiest...

Thanks, Noam

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


RE: Kill a function while it's being executed

2009-02-17 Thread Noam Aigerman
Hi,
Sorry for resurrecting an old thread, but it just bothers me that this
is the best way that python has to deal with killing running
functions... it's quite an ugly hack, no?

Is it a feature that's needed bit missing from python, or is it left out
on purpose (same way like java has deprecated thread.stop and
thread.suspend because they were not safe)?

Thanks, Noam

 

-Original Message-
From: python-list-bounces+noama=answers@python.org
[mailto:python-list-bounces+noama=answers@python.org] On Behalf Of
Albert Hopkins
Sent: Wednesday, February 04, 2009 5:26 PM
To: python-list@python.org
Subject: Re: Kill a function while it's being executed

On Wed, 2009-02-04 at 13:40 +0200, Noam Aigerman wrote:
 Hi All,
 I have a script in which I receive a list of functions. I iterate over
 the list and run each function. This functions are created by some
other
 user who is using the lib I wrote. Now, there are some cases in which
 the function I receive will never finish (stuck in infinite loop).
 Suppose I use a thread which times the amount of time passed since the
 function has started, Is there some way I can kill the function after
a
 certain amount of time has passed (without asking the user who's
giving
 me the list of functions to make them all have some way of notifying
 them to finish)?
 Thanks, Noam

Noam, did you hijack a thread?

You could decorate the functions with a timeout function.  Here's one
that I either wrote or copied from a recipe (can't recall):

class FunctionTimeOut(Exception):
pass

def function_timeout(seconds):
Function decorator to raise a timeout on a function call
import signal

def decorate(f):
def timeout(signum, frame):
raise FunctionTimeOut()

def funct(*args, **kwargs):
old = signal.signal(signal.SIGALRM, timeout)
signal.alarm(seconds)

try:
result = f(*args, **kwargs)
finally:
signal.signal(signal.SIGALRM, old)
signal.alarm(0)
return result

return funct

return decorate


Then

func_dec = function_timeout(TIMEOUT_SECS)
for func in function_list:
timeout_function = func_dec(func)
try:
timeout_function(...)
except FunctionTimeout:
...


-a



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

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


Referencing resources from python

2009-02-16 Thread Noam Aigerman
Hi,

What is the best way to reference a non-python file's path from inside
python ?

Until now, I (stupidly) had such lines  as:

 

theFile=open('../somefile.txt')

 

in my  python files. Now I moved my python files to another dir, and all
those relative filenames broke.

On the other hand, the solution of writing the full path of each
resource isn't welcome either, as we're working from a SVN repository
and we sometimes checkout to different dir's. I am sure there is a third
way which didn't occur to me... 

What do you recommend?

Thanks, Noam

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


Propagating function calls

2009-02-10 Thread Noam Aigerman
Suppose I have a python object X, which holds inside it a python object
Y. How can I propagate each function call to X so the same function call
in Y will be called, i.e:

X.doThatFunkyFunk()

Would cause

Y.doThatFunkyFunk()

Thanks, Noam

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


Distributing simple tasks

2009-02-06 Thread Noam Aigerman
Hi,

Suppose I have an array of functions which I execute in threads (each
thread get a slice of the array, iterates over it and executes each
function in it's slice one after the other). Now I want to distribute
these tasks between two machines, i.e give each machine half of the
slices and let it run them in threads as described above. Is there an
easy way, or an article on this matter you can point me to?

Thanks, Noam

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


RE: Distributing simple tasks

2009-02-06 Thread Noam Aigerman
Hi,

The delta between the finishing times of each machine is insignificant
compared to the actual runtime, thus I don't feel it's necessary at the
moment. Anyway, I want to keep it simple until I understand how to
distribute tasks J

Thanks!

 

From: Thomas Raef [mailto:tr...@ebasedsecurity.com] 
Sent: Friday, February 06, 2009 4:01 PM
To: Noam Aigerman; python-list@python.org
Subject: RE: Distributing simple tasks

 

 

Hi,

Suppose I have an array of functions which I execute in threads (each
thread get a slice of the array, iterates over it and executes each
function in it's slice one after the other). Now I want to distribute
these tasks between two machines, i.e give each machine half of the
slices and let it run them in threads as described above. Is there an
easy way, or an article on this matter you can point me to?

Thanks, Noam

 

I would suggest maybe a separate queue machine that would hand out each
next function. That way if one machine takes a little longer, the
faster machine can keep picking off functions and running them, while
the slower machine can finish it's task.

 

Just a thought.

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


Kill a function while it's being executed

2009-02-04 Thread Noam Aigerman
Hi All,
I have a script in which I receive a list of functions. I iterate over
the list and run each function. This functions are created by some other
user who is using the lib I wrote. Now, there are some cases in which
the function I receive will never finish (stuck in infinite loop).
Suppose I use a thread which times the amount of time passed since the
function has started, Is there some way I can kill the function after a
certain amount of time has passed (without asking the user who's giving
me the list of functions to make them all have some way of notifying
them to finish)?
Thanks, Noam
--
http://mail.python.org/mailman/listinfo/python-list


RE: Kill a function while it's being executed

2009-02-04 Thread Noam Aigerman
About the hijacking - I *might* have done it without understanding what
I did (replied to a previous message and then changed the subject), if
that's what you mean...
Sorry

-Original Message-
From: python-list-bounces+noama=answers@python.org
[mailto:python-list-bounces+noama=answers@python.org] On Behalf Of
Albert Hopkins
Sent: Wednesday, February 04, 2009 5:26 PM
To: python-list@python.org
Subject: Re: Kill a function while it's being executed

On Wed, 2009-02-04 at 13:40 +0200, Noam Aigerman wrote:
 Hi All,
 I have a script in which I receive a list of functions. I iterate over
 the list and run each function. This functions are created by some
other
 user who is using the lib I wrote. Now, there are some cases in which
 the function I receive will never finish (stuck in infinite loop).
 Suppose I use a thread which times the amount of time passed since the
 function has started, Is there some way I can kill the function after
a
 certain amount of time has passed (without asking the user who's
giving
 me the list of functions to make them all have some way of notifying
 them to finish)?
 Thanks, Noam

Noam, did you hijack a thread?

You could decorate the functions with a timeout function.  Here's one
that I either wrote or copied from a recipe (can't recall):

class FunctionTimeOut(Exception):
pass

def function_timeout(seconds):
Function decorator to raise a timeout on a function call
import signal

def decorate(f):
def timeout(signum, frame):
raise FunctionTimeOut()

def funct(*args, **kwargs):
old = signal.signal(signal.SIGALRM, timeout)
signal.alarm(seconds)

try:
result = f(*args, **kwargs)
finally:
signal.signal(signal.SIGALRM, old)
signal.alarm(0)
return result

return funct

return decorate


Then

func_dec = function_timeout(TIMEOUT_SECS)
for func in function_list:
timeout_function = func_dec(func)
try:
timeout_function(...)
except FunctionTimeout:
...


-a



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

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


A replacement to closures in python?

2009-01-30 Thread Noam Aigerman
Hi,

I want to create an array of functions, each doing the same thing with a
change to the parameters it uses... something like:

arr=['john','terry','graham']

funcs=[]

for name in arr:

def func():

print 'hello, my name is '+name

funcs.append(func)

for f in funcs:

f()

 

And I would like that to print

hello, my name is john

hello, my name is terry

hello, my name is graham

of course... 

Now I understand why the above code doesn't work as I want it to, but is
there some simple workaround for it? 

Thanks, Noam 

 

 

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