[Distutils] who needs keyword params? (was: sample setup.py ...)

Greg Stein gstein@lyra.org
Thu, 3 Feb 2000 04:32:31 -0800 (PST)


On Wed, 2 Feb 2000, Joe Van Andel wrote:
>... example setup ...
>
> setup (name = "Perp",
>        version = "0.2",
>        maintainer = "Joseph VanAndel",
>        maintainer_email = "vanandel@ucar.edu",
>        description = "Python Environment for Radar Processing(PERP)",
> #       cmdclass = {'install': Install},
>        packages = ['','a1pp','rec','util'],
>        install_path = 'Perp',
>        include_dirs = ['./include',DDU_INC,SPOL_INC],
> #       ext_package = 'Perp',
>        ext_modules = [
>                       ('a1pp.pulsepairc',
>                        {'sources': ['a1pp/pulsepair.cc',
>                                     'a1pp/pulsepair_wrap.cc'],
>                        'libraries': ['gcc', 'stdc++','pthread']
>                         },
>                         ),
>                       ('a1pp.IIR_Filterc',
>                        {'sources': ['a1pp/IIR_Filter.cc',
>                                     'a1pp/IIR_Filter_wrap.cc'],
>                        'libraries': ['gcc', 'stdc++','pthread']
>                         },
>                         ),
>                       ('rec.RadarFeaturec',
>                        {'sources': ['rec/RadarFeature.cc',
>                                     'rec/RadarFeature_wrap.cc'],
>                        'libraries': ['gcc', 'stdc++','pthread']
>                         },
>                         ),
>                       ('util.du_SweepUtilc',
>                        {'sources': ['util/du_SweepUtil.cc',
>                                     'util/du_SweepUtil_wrap.cc'],
>                        'library_dirs': [DDM_LIB],
>                        'libraries': ['ddm','gcc', 'stdc++','pthread'],
> #                       'extra_preargs': [DDM_LIB]
>                         },
>                         ),
>                 ]
> )

I just had a reasonably evil thought. Looking at the above code, I
thought, "man. look at all those parameters. gets kind of ugly. it would
be nice to not have to pass all those, but just define them at the top
level." Of course, that's when the anti-Pythonic demon possessed me, and I
figured that we could do something like the following (untested) code:

def _internal_setup(...):
  # rename the old setup function to this
  ...

def _get_caller_globals():
  try:
    raise 'hi'
  except:
    return tb.tb_frame.f_back.f_back

def setup(**kw):
  new_kw = { }
  frame = _get_caller_frame()
  code = _internal_setup.func_code
  for argname in code.co_varnames[:code.co_argcount]:
    new_kw[argname] = kw.get(argname) or \
                      frame.f_locals.get(argname) or \
                      frame.f_globals.get(argname)
  return apply(_internal_setup, (), new_kw)


Now that is fun! :-)

The only caveat is that any arguments passed to setup() must be keyword
arguments -- it does not define any positional arguments (unlike the
current setup function). Of course, this could be its "specification"
and/or an extension of the above code could fix that.

While this is generally Badness for most people, it could very well
simplify things for people wanting to whip up a new setup.py.

Cheers,
-g

-- 
Greg Stein, http://www.lyra.org/