Oracle SQL insert into with With clause

You may use as many ‘helper_tables’ as you wish. create table t(helper1 varchar2(50) , helper2 varchar2(50) , dataElement varchar2(50) ); insert into t(helper1, helper2, dataelement) with de as(select level lvl from dual connect by level <10) ,h1 as (select lvl, lvl/1.5 hp from de) ,h2 as (select lvl, lvl/2 hp2 from de) select h1.hp , … Read more

Alternative to contextlib.nested with variable number of context managers

The new Python 3 contextlib.ExitStack class was added as a replacement for contextlib.nested() (see issue 13585). It is coded in such a way you can use it in Python 2 directly: import sys from collections import deque class ExitStack(object): “””Context manager for dynamic management of a stack of exit callbacks For example: with ExitStack() as … Read more

Catching exception in context manager __enter__()

Like this: import sys class Context(object): def __enter__(self): try: raise Exception(“Oops in __enter__”) except: # Swallow exception if __exit__ returns a True value if self.__exit__(*sys.exc_info()): pass else: raise def __exit__(self, e_typ, e_val, trcbak): print “Now it’s running” with Context(): pass To let the program continue on its merry way without executing the context block you … Read more

Python Conditional “With” Lock Design

Just use a threading.RLock which is re-entrant meaning it can be acquired multiple times by the same thread. http://docs.python.org/library/threading.html#rlock-objects For clarity, the RLock is used in the with statements, just like in your sample code: lock = threading.RLock() def func1(): with lock: func2() def func2(): with lock: # this does not block even though the … Read more

Encapsulating retries into `with` block

Is it possible to repeat the code within a with statement? No. As pointed out earlier in that mailing list thread, you can reduce a bit of duplication by making the decorator call the passed function: def do_work(): … # This is not ideal! @transaction(retries=3) def _perform_in_transaction(): # Atomic DB statements … # called implicitly … Read more

Python __enter__ / __exit__ vs __init__ (or __new__) / __del__

There are several differences you appear to have missed: Context manager get a chance to provide a new object just for the block you are executing. Some context managers just return self there (like file objects do), but, as an example, database connection objects could return a cursor object tied to the current transaction. Context … Read more

What are the python builtin __exit__ argument types?

exc_type is the exception’s class. exc_val is the exception instance. exc_tb is a traceback object, of which there is a reference in types.TracebackType. In general it should be the case that type(exc_val) is exc_type exc_val.__traceback__ is exc_tb Note that __exit__ is still invoked when there was no exception raised by the code under a context … Read more

Python Multiprocessing Lib Error (AttributeError: __exit__)

In Python 2.x and 3.0, 3.1 and 3.2, multiprocessing.Pool() objects are not context managers. You cannot use them in a with statement. Only in Python 3.3 and up can you use them as such. From the Python 3 multiprocessing.Pool() documentation: New in version 3.3: Pool objects now support the context management protocol – see Context … Read more

RAII in Python – automatic destruction when leaving a scope

tl;dr RAII is not possible, you mix it up with scoping in general and when you miss those extra scopes you’re probably writing bad code. Perhaps I don’t get your question(s), or you don’t get some very essential things about Python… First off, deterministic object destruction tied to scope is impossible in a garbage collected … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)