[Python-ideas] start, test, init -- in lib

spir denis.spir at gmail.com
Mon Dec 2 12:42:12 CET 2013


Hello,

Below is a minimal simulation of the proposal's core principal. Using my best 
knowledge of Python to have things light for the client, the necessity for it to 
make use of globals() and sys (see below, end of mod.py) nevertheless removes 
much of the proposal's nicety.

Anyway. It establishes the control logic as expressed in the proposal:

* if imported module:
     ~ run 'init' if any
     ~ else, run nothing
* if executed module:
     ~ run 'test', if any (see below)
     ~ else, run 'start', if any
     ~ else, run nothing (error?)

If both test & start exist, start is run if the magic var __run_fn__ == start.
The implementation also forwards command-line args to either test or start.

Below code:
* I called the control logic module 'top_fn', with a func of same name.
* To test it, a module 'mod'.
* To test the case of import, a module 'main'.

Try it by:
* running mod as is (with some command-line args): should run test
* running mod after uncommenting __run_fun__: should run start
* running main, importing mod: should run init

Non-nice aspects:
* The obligation to include in client module:
     import sys
     from topfn import topfn
     topfn(globals(), sys.argv)
   This should be compiler magic, certainly (eg make C main be the right func).
* No further possible compiler magic about differential compilation or such
   (see original proposal).

To be clear, the most important feature of the proposal is to establish standard 
magic names for high-level software structure, which are correctly taken into 
account by the language (read: compiler, probably). If this feature exists, it 
will be used, then recommanded.

Thank you,
Denis

==============================================================================
# topfn.py

from types import FunctionType

def topfn(env, argv):
     ''' Control which top function is executed in importing module.
     '''
     # relevant vars:
     name    = env["__name__"]               # know if module was run as app
     init    = env.get("init", None)         # init func
     test    = env.get("test", None)         # test func
     start   = env.get("start", None)        # start func for app run
     run_fn  = env.get("__run_fn__", None)   # choice between test & start

     # control logic:
     if name == "__main__":
         if isinstance(start, FunctionType) and run_fn == start:
             start(argv)
         elif isinstance(test, FunctionType):
             test(argv)
     elif isinstance(init, FunctionType):
         init()

==============================================================================
# mod.py

print("in mod")

def init ():
     print("Executing init.")

def test1 ():
     print("Executing test1.")
def test2 ():
     print("Executing test1.")
def test3 ():
     print("Executing test1.")
def test (argv):
     print("Executing test with argv: %s." % argv)
     test1()
     test2()
     test3()

def start (argv):
     print("Executing start with argv: %s." % argv)
#~ __run_fn__ = start

import sys
from topfn import topfn
topfn(globals(), sys.argv)

==============================================================================
# main.py

print("in main")
import mod


More information about the Python-ideas mailing list