[TriPython] Two micro-projects that might be interesting
Calloway, Chris
cbc at unc.edu
Mon Oct 2 16:33:27 EDT 2017
We are having to move out meeting this month (at RENCI in Chapel Hill) to Thursday, October 19. Would you like to be the featured speaker and show this off there. I think there would be a lot of interest in seeing some bytecode hacking.
--
Sincerely,
Chris Calloway
Applications Analyst
University of North Carolina
Renaissance Computing Institute
(919) 599-3530
On 10/2/17, 10:53 AM, "TriZPUG on behalf of Christopher O'Brien" <trizpug-bounces+cbc=unc.edu at python.org on behalf of obriencj at gmail.com> wrote:
Hello!
I was originally going to try and show these off tomorrow, but since
Raleigh project night is going to be skipping October, I figured I'd just
hand them off via list and see what people thought.
A friend had approached me about an interesting idea -- an extension of the
syntax for structured assignment. He wanted a way to grab a bunch of values
from a mapping into the local scope of a function without having to
manually write out every single assignment, and ideally without having to
represent the same naming information multiple times. I had been messing
around with the Python bytecode anyway, and it occurred to me that this
was totally possible to do today with just a little bit of introspection! I
threw together the following tiny package:
https://github.com/obriencj/python-mapbind
mapbind in essence looks into the future of function by inspecting the
bytecode, and determines how the result of the call would be assigned. I've
actually been able to confirm this works across a fairly wide range of
versions without having to get into writing any native extensions (2.6,
2.7, 3.4, 3.5, 3.6, pypy, pypy3)
An alternative possibility would have been if the locals() call in Python
actually returned a more living view from a function scope. As you're all
probably well aware, the frame locals() impl in CPython is just a snapshot
(when not in the module scope, anyway). I wondered just how difficult it
would be to make a living version of that view, and it turns out it's
actually pretty easy, but it does require some native extensions.
https://github.com/obriencj/python-livelocals
I'm not sure how much any of the rest of you enjoy playing around with the
internals of the language, but maybe these will be an amusing exercise!
- Chris
More information about the TriZPUG
mailing list