Re: Namespace context managers

You may be interested in this working example of a @namespace decorator (for functions though, not for classes). https://stackoverflow.com/a/52358426/791713 — @namespace def mynamespace(): eggs = 'spam' class Bar: def hello(self): print("Hello, World!") assert mynamespace.eggs == 'spam' mynamespace.Bar().hello() — It uses sys.settrace() to find the frame of the function before it is called to convert it into an actual module object. My (largely underdeveloped and not recommended to be used) “Node.py” project implements a preprocessor for Python code in order to implement such a namespace keyword. — namespace Example: def hello(name): print('Hello, {}!'.format(name)) Example.hello('World') — It does this by converting the code above to the below (and _NamespaceSyntax__namespace_decorator points to the same namespace decorator from above) — @require._NamespaceSyntax__namespace_decorator def Example(): def hello(name): print('Hello, {}!'.format(name)) — (Link to code here: https://github.com/nodepy/nodepy/blob/f9e572aa20159af7a1a57b503d0c57693c7a96... ) Personally I like the suggestion by Ricky Teachey to use “def” instead of introducing a new keyword. If we cannot settle on this kind of syntax addition, the namespace decorator may be a good addition to the standard library? The full code below for reference. — import sys import types def call_function_get_frame(func, *args, **kwargs): """ Calls the function *func* with the specified arguments and keyword arguments and snatches its local frame before it actually executes. """ frame = None trace = sys.gettrace() def snatch_locals(_frame, name, arg): nonlocal frame if frame is None and name == 'call': frame = _frame sys.settrace(trace) return trace sys.settrace(snatch_locals) try: result = func(*args, **kwargs) finally: sys.settrace(trace) return frame, result def namespace(func): frame, result = call_function_get_frame(func) try: module = types.ModuleType(func.__name__) module.__dict__.update(frame.f_locals) return module finally: del frame — Best, -Niklas

The Zen of python could potentially be interpreted to heartily endorse adding nested namespaces to modules: Namespaces are one honking great idea — let’s do more of those! One concern about the implementation, from the sys documentation: "*CPython implementation detail:* The settrace() <https://docs.python.org/3/library/sys.html#sys.settrace> function is intended only for implementing debuggers, profilers, coverage tools and the like. Its behavior is part of the implementation platform, rather than part of the language definition, and thus may not be available in all Python implementations." Since the implementation uses the settrace function to grab the function frame at execution time to turn the namespace into a module object, the approach would seem to be very much at odds with the statement above. It's definitely not a debugging or coverage tool we're talking about here. On Sun, Aug 18, 2019, 10:12 AM Niklas Rosenstein <rosensteinniklas@gmail.com> wrote:

The Zen of python could potentially be interpreted to heartily endorse adding nested namespaces to modules: Namespaces are one honking great idea — let’s do more of those! One concern about the implementation, from the sys documentation: "*CPython implementation detail:* The settrace() <https://docs.python.org/3/library/sys.html#sys.settrace> function is intended only for implementing debuggers, profilers, coverage tools and the like. Its behavior is part of the implementation platform, rather than part of the language definition, and thus may not be available in all Python implementations." Since the implementation uses the settrace function to grab the function frame at execution time to turn the namespace into a module object, the approach would seem to be very much at odds with the statement above. It's definitely not a debugging or coverage tool we're talking about here. On Sun, Aug 18, 2019, 10:12 AM Niklas Rosenstein <rosensteinniklas@gmail.com> wrote:
participants (2)
-
Niklas Rosenstein
-
Ricky Teachey