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

Implements sage.misc.misc.inject_variable(name, value) #7776

Closed
nthiery opened this issue Dec 27, 2009 · 10 comments
Closed

Implements sage.misc.misc.inject_variable(name, value) #7776

nthiery opened this issue Dec 27, 2009 · 10 comments

Comments

@nthiery
Copy link
Contributor

nthiery commented Dec 27, 2009

From the doc:

    inject a variable into the main global namespace

    INPUT:
     - name  - a string
     - value - anything

    EXAMPLES::

        sage: from sage.misc.misc import inject_variable
        sage: inject_variable("a", 314)
        sage: a
        314

This will be used in the upcoming "inject_shorthands" patch for symmetric functions #7777, and could be used in the various inject_variable code instead of manipulating directly globals() (which could be incorrect if not called directly from the interpreter/notebook.

CC: @sagetrac-sage-combinat

Component: user interface

Author: Nicolas M. Thiéry

Reviewer: Mike Hansen

Merged: sage-4.3.1.rc0

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

@nthiery

This comment has been minimized.

@nthiery
Copy link
Contributor Author

nthiery commented Dec 27, 2009

comment:2

(note: patch prepared and tested on 4.2 not 4.3)

@robertwb
Copy link
Contributor

comment:3

I'd rather it looked for __name__ == '__main__' than wiki_create_instance.

@nthiery
Copy link
Contributor Author

nthiery commented Jan 3, 2010

Attachment: trac_7776-inject_variable-nt.patch.gz

@nthiery
Copy link
Contributor Author

nthiery commented Jan 3, 2010

comment:4

Replying to @robertwb:

I'd rather it looked for __name__ == '__main__' than wiki_create_instance.

Ah, excellent, that sure is the right way for doing this. I had missed this __name__ thing.

Thanks for the suggestion! Patch updated.

@mwhansen
Copy link
Contributor

comment:5

This looks good to me.

@mwhansen
Copy link
Contributor

Reviewer: Mike Hansen

@jhpalmieri
Copy link
Member

comment:6

If I do

sage: inject_variable(3, 34)  # pass a non-string to inject_variable, which I probably shouldn't do

then tab-completion is broken. This is odd, and a little alarming. Since this function isn't meant for casual users, maybe this isn't a big deal, but otherwise, perhaps we should check that the first argument is a string.

Here's another question:

sage: from sage.misc.misc import inject_variable
sage: inject_variable('a', 23)
sage: inject_variable('a', 26)
/Applications/sage/local/bin/sage-ipython:1: RuntimeWarning: redefining global value `a`
  #!/usr/bin/env python
sage: inject_variable('a', 29)
sage: inject_variable('a', 33)

Why is the warning only printed the first time? Is that just the nature of these warnings?

@rlmill
Copy link
Mannequin

rlmill mannequin commented Jan 14, 2010

Merged: sage-4.3.1.rc0

@rlmill rlmill mannequin removed the s: positive review label Jan 14, 2010
@rlmill rlmill mannequin closed this as completed Jan 14, 2010
@nthiery
Copy link
Contributor Author

nthiery commented Jan 14, 2010

comment:8

Replying to @jhpalmieri:

If I do

sage: inject_variable(3, 34)  # pass a non-string to inject_variable, which I probably shouldn't do

then tab-completion is broken. This is odd, and a little alarming. Since this function isn't meant for casual users, maybe this isn't a big deal, but otherwise, perhaps we should check that the first argument is a string.

Thanks for catching this. Please review the trivial #7928 follow up!

Here's another question:

sage: from sage.misc.misc import inject_variable
sage: inject_variable('a', 23)
sage: inject_variable('a', 26)
/Applications/sage/local/bin/sage-ipython:1: RuntimeWarning: redefining global value `a`
  #!/usr/bin/env python
sage: inject_variable('a', 29)
sage: inject_variable('a', 33)

Why is the warning only printed the first time? Is that just the nature of these warnings?

Ah, I had not noticed this. It seems to be a feature of warn. I added a comment in #7928.

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

5 participants