[Python-ideas] Function-like Modules (better control/introspection)

C Anthony Risinger anthony at xtfx.me
Fri Mar 14 22:03:17 CET 2014


hello,

could we make modules work more like this...

https://gist.github.com/xtfxme/9556806

tl;dr: __code__ object available (potentially a __self__ object), reload by
calling, makes working with modules more natural/safe!

ENCLOSED: modules-next.py

-- 

C Anthony

#----------------------------------------------------------------------------------------(
modules-next.py )
# encoding: utf-8
#
# modules-next
#
# C Anthony Risinger


import sys


m_name = 'modules_next'
m_source = r"""\
if __name__ == '{0}':
    from . import __call__ as reexec
    from . import __code__ as code
    from . import __dict__ as ns
    print('>>> {{0}}: {{1}}\n{{2}}\n'.format(__name__, code, ns.keys()))
    __name__ = 'modules_next_reloaded'
    reexec()
    print('>>> {{0}}: {{1}}\n{{2}}\n'.format(__name__, code, ns.keys()))
else:
    print('!!! __name__ == {{0}}\n'.format(__name__))

def throw():
    raise Exception(__name__)
""".format(m_name)
m_code = compile(m_source, m_name, 'exec')
m_dict = {
    '__file__': __file__ + '.next',
    '__name__': m_name,
    '__path__': list(),
    '__doc__': None,
    }
#...bind namespace/functions
module = eval('lambda: None', m_dict)
#...automatically expose as attributes
module.__dict__ = module.__globals__
#...must be set twice, __code__ sees __dict__, but we see descriptor
module.__name__ = m_name
#...redefine the function body!
module.__code__ = m_code
#...yay! we haz legitimate module!
sys.modules[m_name] = module


print('--* importing...')
import modules_next
print('>>> {0}: {1}\n{2}\n'.format(
    __name__,
    modules_next.__code__,
    modules_next.__dict__.keys(),
    ))

print('--* importing (for real this time)...')
#...import/reload by calling our func-y-module
module()
print('>>> {0}: {1}\n{2}\n'.format(
    __name__,
    modules_next.__code__,
    modules_next.__dict__.keys(),
    ))


print('--* throwing...')
modules_next.throw()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140314/c8c8bb6b/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: modules-next.py
Type: text/x-python
Size: 1521 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140314/c8c8bb6b/attachment-0001.py>


More information about the Python-ideas mailing list