Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the block of commands that are to be executed in the with statement

In reading the specifications for the with statement (link), I have some things I'd like to play around with. This isn't for any production code or anything, I'm just exploring, so please don't be too harsh if this is a bad idea.

What I'd like to do is grab the piece called "BLOCK" in the linked docs above, and actually tinker around with it inside of the call to __enter__. (See the linked doc, just after the start of the motivation and summary section.)

The idea is to create my own sort of on-the-fly local namespace. Something like this:

with MyNameSpace(some_object):
    print a #Should print some_object.a
    x = 4 #Should set some_object.x=4

Basically, I want the statements inside of the with block to be subordinate to the local variables and assignment conventions of some_object.

In my specific case, some_object might be a special data array that has my own column-wise operations or something. In which case saying something like x = y + 5 if y > 4 else y - 2 might be some fancy NumPy vectorized operation under the hood, but I don't need to explicitly call some_object's interface to those methods. In the namespace, the expressions should "just work" (however I define them to be inferred in the MyNameSpace class.

My first idea is to somehow interrupt the with process and get a hold of the code that goes in the try block. Then interpret that code when __enter__ gets called, and replace the code in the try block with something else (perhaps pass if that would work, but possibly something that restores some_object back to the original variable scope with its new changed variables preserved).

A simple test case would be something like this:

my_dict = {'a':3, 'b':2}
with MyNameSpace(my_dict):
    print a # Should print 3
    x = 5 # When the block finishes, my_dict['x'] should now be 5

I'm interested if this idea exists somewhere already.

I am aware of best practices things for assigning variables. This is a pet project, so please assume that, just for the sake of this idea, we can ignore best practices. Even if you wouldn't like assigning variables this way, it could be useful in my current project.

Edit

To clarify the kinds of tricky stuff I might want to do, and to address the answer below claiming that it can't be done, consider the example file testLocals.py below:

my_dict = {'a':1, 'b':2}
m = locals()
print m["my_dict"]['a']
m["my_dict"]['c'] = 3
print my_dict

class some_other_scope(object):
    def __init__(self, some_scope):
        x = 5
        g = locals()
        some_scope.update(g)
        some_scope["my_dict"]["d"] = 4

sos = some_other_scope(m)
print my_dict
print x

which gives the following when I run it non-interactively:

ely@AMDESK:~/Desktop/Programming/Python$ python testLocals.py
1
{'a': 1, 'c': 3, 'b': 2}
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
5
like image 241
ely Avatar asked Sep 18 '12 22:09

ely


People also ask

What is the with statement in Python?

In Python, the with statement replaces a try-catch block with a concise shorthand. More importantly, it ensures closing resources right after processing them. A common example of using the with statement is reading or writing to a file. A function or class that supports the with statement is known as a context manager.

Which methods are invoked on entering into and exiting from the block of code written in with statement?

__enter__ and [__exit__] both are methods that are invoked on entry to and exit from the body of "the with statement" (PEP 343) and implementation of both is called context manager. the with statement is intend to hiding flow control of try finally clause and make the code inscrutable.

How can the compiler identify the inner block of a for loop Python?

Python identifies blocks using indentation. All the statements in a particular block need to be indented at the same level, though you can use any amount of indentation, but all the statements in a particular block need to have the same indentation level.

What is context managers in Python?

A context manager usually takes care of setting up some resource, e.g. opening a connection, and automatically handles the clean up when we are done with it. Probably, the most common use case is opening a file. with open('/path/to/file.txt', 'r') as f: for line in f: print(line)


2 Answers

Try this.

import sys 
class MyNameSpace(object):
    def __init__(self,ns):
        self.ns = ns
    def __enter__(self):
        globals().update(self.ns)
    def __exit__(self, exc_type,exc_value,traceback):
        self.ns.update(sys._getframe(1).f_locals)

my_dict = {'a':3, 'b':2} 
with MyNameSpace(my_dict) as ns:
    print(a) # Should print 3
    x = 5 # When the block finishes, my_dict['x'] should now be 5 

print(my_dict['x'])
like image 146
Odomontois Avatar answered Oct 12 '22 22:10

Odomontois


Here is something similar I tried a few months ago:

import sys
import inspect
import collections

iscallable = lambda x: isinstance(x, collections.Callable)

class Namespace(object):
    def __enter__(self):
        """store the pre-contextmanager scope"""
        f = inspect.currentframe(1)
        self.scope_before = dict(f.f_locals)
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        """determine the locally declared objects"""
        f = inspect.currentframe(1)
        scope_after = dict(f.f_locals)
        scope_context = set(scope_after) - set(self.scope_before)

        # capture the local scope, ignoring the context manager itself
        self.locals = dict(
            (k, scope_after[k]) for k in scope_context if not isinstance(scope_after[k], self.__class__)
        )

        for name in self.locals:
            obj = scope_after[name]
            if iscallable(obj):
                # closure around the func_code with the appropriate locals
                _wrapper = type(lambda: 0)(obj.func_code, self.locals)
                self.__dict__[name] = _wrapper
                # update locals so the calling functions refer to the wrappers too
                self.locals[name] = _wrapper
            else:
                self.__dict__[name] = obj

            # remove from module scope
            del sys.modules[__name__].__dict__[name]

        return self

with Namespace() as Spam:
    x = 1
    def ham(a):
        return x + a      
    def cheese(a):
        return ham(a) * 10

It uses inspect to modify locals while within the context manager and then to re-assign back to the original values when done.

It's not perfect - I can't remember where it hits issues, but I'm sure it does - but it might help you get started.

like image 41
Matthew Trevor Avatar answered Oct 12 '22 22:10

Matthew Trevor