Break or exit out of “with” statement?

with giving you trouble? Throw more with-able objects at the problem! class fragile(object): class Break(Exception): “””Break out of the with statement””” def __init__(self, value): self.value = value def __enter__(self): return self.value.__enter__() def __exit__(self, etype, value, traceback): error = self.value.__exit__(etype, value, traceback) if etype == self.Break: return True return error Just wrap the expression you’re going … Read more

The VB.NET ‘With’ Statement – embrace or avoid?

If you have long variablenames and would end up with: UserHandler.GetUser.First.User.FirstName=”Stefan” UserHandler.GetUser.First.User.LastName=”Karlsson” UserHandler.GetUser.First.User.Age=”39″ UserHandler.GetUser.First.User.Sex=”Male” UserHandler.GetUser.First.User.Occupation=”Programmer” UserHandler.GetUser.First.User.UserID=”0″ ….and so on then I would use WITH to make it more readable: With UserHandler.GetUser.First.User .FirstName=”Stefan” .LastName=”Karlsson” .Age=”39″ .Sex=”Male” .Occupation=”Programmer” .UserID=”0″ end with In the later example there are even performance benefit over the first example because in the … Read more

Conditional with statement in Python

Python 3.3 and above Python 3.3 introduced contextlib.ExitStack for just this kind of situation. It gives you a “stack”, to which you add context managers as necessary. In your case, you would do this: from contextlib import ExitStack with ExitStack() as stack: if needs_with(): gs = stack.enter_context(get_stuff()) # do nearly the same large block of … Read more

How do I mock an open used in a with statement (using the Mock framework in Python)?

Python 3 Patch builtins.open and use mock_open, which is part of the mock framework. patch used as a context manager returns the object used to replace the patched one: from unittest.mock import patch, mock_open with patch(“builtins.open”, mock_open(read_data=”data”)) as mock_file: assert open(“path/to/open”).read() == “data” mock_file.assert_called_with(“path/to/open”) If you want to use patch as a decorator, using mock_open()‘s … Read more

In Python, if I return inside a “with” block, will the file still close?

Yes, it acts like the finally block after a try block, i.e. it always executes (unless the python process terminates in an unusual way of course). It is also mentioned in one of the examples of PEP-343 which is the specification for the with statement: with locked(myLock): # Code here executes with myLock held. The … Read more

Are there legitimate uses for JavaScript’s “with” statement?

Another use occurred to me today, so I searched the web excitedly and found an existing mention of it: Defining Variables inside Block Scope. Background JavaScript, in spite of its superficial resemblance to C and C++, does not scope variables to the block they are defined in: var name = “Joe”; if ( true ) … Read more

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