gutow / Algebra_with_Sympy

Tools for doing stepwise algebra in an on paper like manner using SymPy.
https://gutow.github.io/Algebra_with_Sympy/
GNU General Public License v3.0
16 stars 3 forks source link

Consider having option to label sympy expressions with their python name #37

Open gutow opened 1 month ago

gutow commented 1 month ago

This would require adding another option to output formatting (e.g. sympy_expr_labels). When requested sympy expressions would be labeled with the python name for the expressions just as equations are by default.

The operation to extract equation labels could be used more generically and not be a part of the expression object. Rough code to get the necessary string for the label would be:

def _get_eqn_name(expr):
    """
    Tries to find the python string name that refers to a sympy object. In
    IPython environments (IPython, Jupyter, etc...) looks in the user_ns.
    If not in an IPython environment looks in __main__.
    :return: string value if found or empty string.
    """
    import __main__ as shell
    for k in dir(shell):
        item = getattr(shell, k)
        if isinstance(item, Basic):
            if item == expr and not k.startswith('_'):
                return k
    return ''

This could also take over for the similar function currently embedded in the equation type, further removing output tweaking from sympy proper.