Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement automatic definition of undefined variables in the Sage command line (ipython) #7486

Open
williamstein opened this issue Nov 18, 2009 · 2 comments

Comments

@williamstein
Copy link
Contributor

See #7482, which is about doing this in the notebook. For the command line, see the remarks below by the author of IPython.

CC: @sagetrac-olazo

Component: user interface

Issue created by migration from https://trac.sagemath.org/ticket/7486

@williamstein williamstein added this to the sage-5.11 milestone Nov 18, 2009
@williamstein williamstein self-assigned this Nov 18, 2009
@williamstein
Copy link
Contributor Author

comment:1

From Fernando Perez:

Actually no, not that hard: ipython executes all  user  code inside a
dict created at initialization time.  One could replace this (today a
plain python dict) with another dict that would implement the
requested behavior.  This would spring variables into existence when a
KeyError was about to occur.

The command-line doesn't have the notion of a cell (yet, that will
hopefully change soon) but it could be toggled with a function call.

It's basically a matter of instantiating the initial ipython with a
custom user_ns dict that does the above, that should be all.  And
providing a magic (or even plain) function in the global namespace to
toggle this behavior on/off at runtime.

The attached file (run it as 'ipauto.py') shows how it could work, I
didn't implement the toggling and it's a quick hack, but the basic
idea is  there.  Here it is in action:

In [1]: %autocall 0
Automatic calling is: OFF

In [2]: x+y
Injecting x
Injecting y
Out[2]: x + y

In [3]: type(x)
Out[3]: <class 'sympy.core.symbol.Symbol'>

In [4]: x+3
Out[4]: 3 + x

In [5]: x**2+x*x
Out[5]: 2*x**2

In [6]: x+z**3
Injecting z
Out[6]: x + z**3


A couple of notes:

- It's key to disable autocalling for this to work, otherwise too many
false positives are triggered.  If this idea proves to have  wings, it
will be one more argument in favor of disabling autocalling (it just
has too many uncontrollable side effects).

- Unfortunately IPython as shipped needs a tiny patch for this to
work, due to a really silly omission.  The patch is a trivial 2-liner,
in case anyone wants to take this further for Sage:

(ipython-0.10)maqroll[0.10]> bzr diff
=== modified file 'IPython/Shell.py'
--- IPython/Shell.py    2009-04-14 20:12:02 +0000
+++ IPython/Shell.py    2009-11-18 09:03:01 +0000
@@ -1230,7 +1230,7 @@


 # This is the one which should be called by external code.
-def start(user_ns = None):
+def start(user_ns = None, user_global_ns = None):
    """Return a running shell instance, dealing with threading options.

    This is a factory function which will instantiate the proper IPython shell
@@ -1238,7 +1238,7 @@
    different GUI toolkits require different thread handling details."""

    shell = _select_shell(sys.argv)
-    return shell(user_ns = user_ns)
+    return shell(user_ns = user_ns, user_global_ns = user_global_ns)

 # Some aliases for backwards compatibility
 IPythonShell = IPShell



Cheers,

f

@williamstein
Copy link
Contributor Author

Attachment: ipauto.py.gz

@jdemeyer jdemeyer modified the milestones: sage-5.11, sage-5.12 Aug 13, 2013
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.1, sage-6.2 Jan 30, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.2, sage-6.3 May 6, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.3, sage-6.4 Aug 10, 2014
@mkoeppe mkoeppe removed this from the sage-6.4 milestone Dec 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants