[Tutor] constructing semi-arbitrary functions

eryksun eryksun at gmail.com
Wed Feb 19 16:17:46 CET 2014


On Wed, Feb 19, 2014 at 7:33 AM, Oscar Benjamin
<oscar.j.benjamin at gmail.com> wrote:
> I don't really understand why it works that way though.
> Looking here
>
>    http://iminuit.github.io/iminuit/api.html#function-sig-label

This API is unusual, but co_argcount and co_varnames should be
available and defined as per the spec:

http://docs.python.org/2/reference/datamodel#index-59

    CPython/PyPy      Jython
    =========================
    co_name           *
    co_argcount       *
    co_nlocals        *
    co_varnames       *
    co_cellvars       *
    co_freevars       *
    co_filename       *
    co_firstlineno    *
    co_flags          *
    co_code
    co_consts
    co_names
    co_lnotab
    co_stacksize

The last few attributes aren't relevant to Jython since it's using the JVM.

To its credit, util.better_arg_spec does fall back on
inspect.getargspec. But then, its ultimate fallback is regex magic:
util.arguments_from_docstring. Wow. I guess it's convenient to specify
initial values, step size, limits, etc, as keyword arguments for named
parameters.

I haven't read this whole thread, but here are some source links if
the OP is using this iminuit package. The `describe` function is in
util.py.

https://github.com/iminuit/iminuit/blob/master/iminuit/util.py
https://github.com/iminuit/iminuit/blob/master/iminuit/_libiminuit.pyx

Some snippets from Minuit.__init__:

    args = describe(fcn) if forced_parameters is None\
           else forced_parameters

    # ...

    self.initialvalue = {x:maplookup(kwds,x,0.) for x in args}
    self.initialerror = \
        {x:maplookup(kwds,'error_'+x,1.) for x in args}
    self.initiallimit = \
        {x:maplookup(kwds,'limit_'+x,None) for x in args}
    self.initialfix = \
        {x:maplookup(kwds,'fix_'+x,False) for x in args}

    # ...

    self.parameters = args
    self.args = tuple(self.initialvalue[k] for k in args)
    self.values = {k:self.initialvalue[k] for k in args}
    self.errors = {k:self.initialerror[k] for k in args}


More information about the Tutor mailing list