modelx icon indicating copy to clipboard operation
modelx copied to clipboard

Setting Doc String

Open mhumpher opened this issue 4 years ago • 4 comments

Is it possible to set the doc string for a cell outside of a module import? After creating a cell with new_cell, I want to add a doc string, but using the doc property or set_property method return an error.

mhumpher avatar Apr 26 '21 20:04 mhumpher

The doc property of a Cells is linked to the docstring in its Formula.

>>> foo.formula
def foo(x):
    """this is foo's doc"""
    return x

>>> foo.doc
"this is foo's doc"

So to change doc, you can redefine the Formula

>>> def temp(x):
...     """foo's doc is updated"""
...     return x

>>> foo.formula = temp

>>> foo.formula
def foo(x):
    """foo's doc is updated"""
    return x

>>> foo.doc
"foo's doc is updated"

The name temp can be anything. It gets replaced with foo.

If Formula is defined by a lambda function, doc is None because lambda functions don't have docstrings. I'm planning to enhance modelx so that Cells with lambda functions can have docs. I also want to allow such operation as foo.doc = "foo's doc is updated", which updates the docstring in its Formula.

fumitoh avatar Apr 27 '21 13:04 fumitoh

Thanks. Lambda functions are indeed my use case.

mhumpher avatar Apr 27 '21 14:04 mhumpher

Let me see if I can make it work by the next release.

fumitoh avatar Apr 27 '21 14:04 fumitoh

Just released v0.14.0 and this feature is now implemented.

fumitoh avatar May 02 '21 16:05 fumitoh