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

How to avoid accidentally implicitly referring to properties on the global object?

There are some things you need to consider before trying to answer this question. For example, take the Object constructor. It is a “Standard built-in object”. window.status is part of the Window interface. Obviously, you don’t want status to refer to window.status, but do you want Object to refer to window.Object? The solution to your … 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

Understanding the Python with statement and context managers

with doesn’t really replace try/except, but, rather, try/finally. Still, you can make a context manager do something different in exception cases from non-exception ones: class Mgr(object): def __enter__(self): pass def __exit__(self, ext, exv, trb): if ext is not None: print “no not possible” print “OK I caught you” return True with Mgr(): name=”rubicon”/2 #to raise … Read more

Conditional with statement in Python

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 stuff, # involving gs … Read more

Count the number of rows in another sheet [duplicate]

Your original was not working because the parent of Cells(4, 1) and Cells(4, 1).End(xlDown) was not specified. Prefix any cell address with a period (aka . or full stop) when you are inside a With … End With block. Example: With Worksheets(“Verbs”) wordcount = .Range(.Cells(4, 1), .Cells(4, 1).End(xlDown)).Rows.Count End With Note the .Cells(4, 1) and … Read more