python interpreter
I'm using python's interpreter's to run various commands (like a normal shell). However if sources are modified changes are not reflected so I have to restart interpreter. Is there any way to avoid restarting this? example: import blah blah.Blah() # ... blah.Blah() changed blah.Blah() # ... new behavior -- http://mail.python.org/mailman/listinfo/python-list
decorating base methods
I want to derive a base class, such that some methods are decorated. The only thing I have in mind is: class Base(object): def A(self, x): pass def B(self, y): pass class Derived(Base): @decorator def A(self, x): Base.A(self, x) Is this correct approach? How can avoid call to Base.A(...)? -- http://mail.python.org/mailman/listinfo/python-list
callbacks in python
does anyone know a nice implementation of callbacks in python? i have issues mixing named & unamed parameters. i want build a callback over a function such that some parameters are passed when callback is created and the rest are passed when the function is called. example: callback = Callback(function, x=1, y) callback(z, t=4, u) -- http://mail.python.org/mailman/listinfo/python-list
Re: callbacks in python
On Aug 14, 12:02 am, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > your use of the word "callback" is a bit unusual, and your example isn't > valid Python code, but it looks as if functools.partial might be what > you need: > > http://docs.python.org/lib/module-functools.html my current implementation is very similar to partial() :) (10x, i'll use partial from now on). however it seems that I don't understand very well positional and keyword arguments in python because I got the error described here: http://docs.python.org/ref/calls.html#calls (TypeError: f() got multiple values for keyword argument 'a') which confused me even more. so if you pass positional and keyword arguments to both partial() and function returned the order of passed arguments might be different than expected. i was looking for an implementation that somehow avoids this confusion. -- http://mail.python.org/mailman/listinfo/python-list
Re: python interpreter
On Aug 12, 7:46 pm, "Calvin Spealman" <[EMAIL PROTECTED]> wrote: > The best answer is: Don't do that! > > That isn't how you test things. Write test scripts, probably using the > unittest framework. You'll save yourself time and trouble having > easily reproducible tests. Many people suggested reload(), but you > should know it is dangerous. It can have results you don't expect and > are hard to track down. Unless you really, really know what you're > doing: dont use it. =)) I already have unittests for most of my code. I'm using python interpreter as a shell to start various jobs & stuff. from time to time it's useful. i came to conclusion that `printf "python code" | python` was more appropriate for what i needed. -- http://mail.python.org/mailman/listinfo/python-list
execute another python script
how do I execute another python script under a different process? I want the script to be run using the same interpretoer as the one running current script. I tried using os.execlp but I don't know how to get the name/path of the interpreter. -- http://mail.python.org/mailman/listinfo/python-list
Re: execute another python script
On Aug 18, 11:34 am, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > >>> import sys, subprocess > >>> subprocess.call([sys.executable, "-c", "print 'hello'"]) > hello > 0 10x :). exactly what I was looking for. -- http://mail.python.org/mailman/listinfo/python-list
cathing uncaught exceptions
how can I catch (globally) exception that were not caught in a try/ catch block in any running thread? i had this weird case that an exception was raised in one thread, but nothing was displayed/logged. -- http://mail.python.org/mailman/listinfo/python-list
Re: cathing uncaught exceptions
On Aug 18, 6:18 pm, Paul McGuire <[EMAIL PROTECTED]> wrote: > On Aug 18, 10:02 am, Alexandru Mosoi <[EMAIL PROTECTED]> wrote: > > > how can I catch (globally) exception that were not caught in a try/ > > catch block in any running thread? i had this weird case that an > > exception was raised in one thread, but nothing was displayed/logged. > > I suspect that your weird case was *not* because your exception was > uncaught, but because it *was* caught and just discarded. Look > through your code for something like this: > > except ExceptionIDontExpectToEverGet: > pass > > or even worse: > > except: > pass > > I'll bet one of these was your "exception non-logger" culprits. > > -- Paul sorry, no... i used except: print 'something' to validate that there is an exception that was raised :). otherwise, i avoid your second construct in my code (just because I don't want to miss strange cases like this one). -- http://mail.python.org/mailman/listinfo/python-list
Re: cathing uncaught exceptions
On Aug 18, 6:02 pm, Alexandru Mosoi <[EMAIL PROTECTED]> wrote: > how can I catch (globally) exception that were not caught in a try/ > catch block in any running thread? i had this weird case that an > exception was raised in one thread, but nothing was displayed/logged. I found that normally sys.excepthook is invoked for uncaught exceptions. however in my case the function is not invoked (as it should normaly do). i printed the stackframe and checked that there is no try/catch block that handles the exception. any other idea? -- http://mail.python.org/mailman/listinfo/python-list
passing arguments to exec
i want to execute a python script using exec open('script.py'). how do I pass arguments? -- http://mail.python.org/mailman/listinfo/python-list
logging exceptions
why doesn't logging throw any exception when it should? how do I configure logging to throw exceptions? >>> try: ... logging.fatal('asdf %d', '123') ... except: ... print 'this line is never printed' ... Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/logging/__init__.py", line 744, in emit msg = self.format(record) File "/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/logging/__init__.py", line 630, in format return fmt.format(record) File "/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/logging/__init__.py", line 418, in format record.message = record.getMessage() File "/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/logging/__init__.py", line 288, in getMessage msg = msg % self.args TypeError: int argument required -- http://mail.python.org/mailman/listinfo/python-list
atomic increment
how can i do an atomic read+increment? something like with lock: old = atomic_int atomic_int += 1 but in one operation -- http://mail.python.org/mailman/listinfo/python-list
use of Queue
how is Queue intended to be used? I found the following code in python manual, but I don't understand how to stop consumers after all items have been produced. I tried different approaches but all of them seemed incorrect (race, deadlock or duplicating queue functionality) def worker(): while True: item = q.get() do_work(item) q.task_done() q = Queue() for i in range(num_worker_threads): t = Thread(target=worker) t.setDaemon(True) t.start() for item in source(): q.put(item) q.join() # block until all tasks are done -- http://mail.python.org/mailman/listinfo/python-list
Re: use of Queue
On Aug 27, 1:06 pm, Gerhard Häring <[EMAIL PROTECTED]> wrote: > Alexandru Mosoi wrote: > > how is Queue intended to be used? I found the following code in python > > manual, but I don't understand how to stop consumers after all items > > have been produced. I tried different approaches but all of them > > seemed incorrect (race, deadlock or duplicating queue functionality) > > > def worker(): > > while True: > > item = q.get() > > if item is None: > break > > > do_work(item) > > q.task_done() > > > q = Queue() > > for i in range(num_worker_threads): > > t = Thread(target=worker) > > t.setDaemon(True) > > t.start() > > > for item in source(): > > q.put(item) > > # stop all consumers > for i in range(num_worker_threads): > q.put(None) > > > > > q.join() # block until all tasks are done > > This is how I do it. > > -- Gerhard Your solution works assuming that you know how many consumer threads you have :). I don't :). More than that, it's not correct if you have more than one producer :). Having a sentinel was my very first idea, but as you see... it's a race condition (there are cases in which not all items are processed). -- http://mail.python.org/mailman/listinfo/python-list
Re: use of Queue
On Aug 27, 2:54 pm, Jeff <[EMAIL PROTECTED]> wrote: > Queue raises an Empty exception when there are no items left in the > queue. Put the q.get() call in a try block and exit in the except > block. Wrong. What if producer takes a long time to produce an item? Consumers will find the queue empty and exit instead of waiting. > You can also use a condition variable to signal threads to terminate. This is the solution I want to avoid because it duplicates Queue's functionality. I prefer having a clean solution with nice design to hacking Queue class. -- http://mail.python.org/mailman/listinfo/python-list
Re: use of Queue
On Aug 27, 12:45 pm, Alexandru Mosoi <[EMAIL PROTECTED]> wrote: > how is Queue intended to be used? I found the following code in python > manual, but I don't understand how to stop consumers after all items > have been produced. I tried different approaches but all of them > seemed incorrect (race, deadlock or duplicating queue functionality) > > def worker(): > while True: > item = q.get() > do_work(item) > q.task_done() > > q = Queue() > for i in range(num_worker_threads): > t = Thread(target=worker) > t.setDaemon(True) > t.start() > > for item in source(): > q.put(item) > > q.join() # block until all tasks are done ok. I think I figured it out :). let me know what you think global num_tasks, num_done, queue num_tasks = 0 num_done = 0 queue = Queue() # producer num_tasks += 1 for i in items: num_tasks += 1 queue.put(i) num_tasks -= 1 if num_tasks == num_done: queue.put(None) # consumer while True: i = queue.get() if i is None: queue.put(None) break # do stuff num_done += 1 if num_done == num_tasks: queue.put(None) break -- http://mail.python.org/mailman/listinfo/python-list
how do I stop SocketServer()?
supposing that I have a server (an instance of SocketServer()) that waits for a connection (ie is blocked in accept()) and in another thread i want to stop the server, how do I do that? -- http://mail.python.org/mailman/listinfo/python-list
Re: derived classes and __getattr__
On Sep 5, 11:47 am, Peter Otten <[EMAIL PROTECTED]> wrote: > Alexandru Moșoi wrote: > > i'm facing the following problem: > > > class Base(object): > > def __getattr__(self, attr): return lambda x: attr + '_' + x > > > def dec(callable): > > return lambda *args: 'dec_' + callable(*args) > > > class Derived(Base): > > what_so_ever = dec(Base.what_so_ever) # wrong, base doesn't have > > what_so_ever > > mumu = dec(Base.mumu) # wrong, base > > doesn't have mumu > > > any idea how to do this? > > __getattr__() is defined in the class to create instance attributes on the > fly. If you want class attributes you have to put the __getattr__() method > into the class of the class, or "metaclass": > > class Base(object): > class __metaclass__(type): > def __getattr__(self, attr): > return lambda self, x: attr + '_' + x > > def dec(callable): > return lambda *args: 'dec_' + callable(*args) > > class Derived(Base): > what_so_ever = dec(Base.what_so_ever) > > d = Derived() > print d.what_so_ever("42") > > I don't see how you can turn this into something useful... > > Peter 10x. it works. however I have another small problem. now, d.third('blah') doesn't work because instance d doesn't have 'third' attribute. I was expecting derived class to inherit the metaclass as well, but it didn't. -- http://mail.python.org/mailman/listinfo/python-list
Re: derived classes and __getattr__
On Sep 5, 1:13 pm, Peter Otten <[EMAIL PROTECTED]> wrote: > Alexandru Mosoi wrote: > > On Sep 5, 11:47 am, Peter Otten <[EMAIL PROTECTED]> wrote: > >> Alexandru Moșoi wrote: > >> > i'm facing the following problem: > > >> > class Base(object): > >> > def __getattr__(self, attr): return lambda x: attr + '_' + x > > >> > def dec(callable): > >> > return lambda *args: 'dec_' + callable(*args) > > >> > class Derived(Base): > >> > what_so_ever = dec(Base.what_so_ever) # wrong, base doesn't have > >> > what_so_ever > >> > mumu = dec(Base.mumu) # wrong, base > >> > doesn't have mumu > > >> > any idea how to do this? > > >> __getattr__() is defined in the class to create instance attributes on > >> the fly. If you want class attributes you have to put the __getattr__() > >> method into the class of the class, or "metaclass": > > >> class Base(object): > >> class __metaclass__(type): > >> def __getattr__(self, attr): > >> return lambda self, x: attr + '_' + x > > >> def dec(callable): > >> return lambda *args: 'dec_' + callable(*args) > > >> class Derived(Base): > >> what_so_ever = dec(Base.what_so_ever) > > >> d = Derived() > >> print d.what_so_ever("42") > > >> I don't see how you can turn this into something useful... > > >> Peter > > > 10x. it works. however I have another small problem. now, > > d.third('blah') doesn't work because instance d doesn't have 'third' > > attribute. I was expecting derived class to inherit the metaclass as > > well, but it didn't. > > That has nothing to do with inheritance: > > >>> type(Derived) > > > > If Python doesn't find an attribute in the instance it looks it up in the > class but not in the metaclass: > > >>> Base.third > > at 0x2b5f8028aa28>>>> Base().third > > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'Base' object has no attribute 'third' > > I think you should go back to the drawing board now and look for a simpler > approach to solve your actual problem... > > Peter i somehow fixed the problem using: def __getattr__(self, attr): return functools.partial(Base.__metaclass__.__getattr__(self.__class__, attr), self) however this looks ugly enough to look for another solution. i still have one more question: why do I have to bind self? (without which functions fail expecting an instance) -- http://mail.python.org/mailman/listinfo/python-list
generator exceptions
i have a generator that raises an exception when calling next(), however if I try to catch the exception and print the traceback i get only the line where next() was called while True: try: iterator.next() except StopIteration: break except Exception, e: traceback.print_exc() how do I get the traceback starting from where exception was raised in first place? -- http://mail.python.org/mailman/listinfo/python-list
blocking all threads
how can I block all threads for a specific amount of time? (i need to sleep whole process for testing purposes). i thought of accessing GIL and sleep for some amount of time, but I don't know how to do this and whether GIL is recursive. -- http://mail.python.org/mailman/listinfo/python-list