Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#7776 closed enhancement (fixed)

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

Reported by: nthiery Owned by: was
Priority: major Milestone: sage-4.3.1
Component: user interface Keywords:
Cc: sage-combinat Merged in: sage-4.3.1.rc0
Authors: Nicolas M. Thiéry Reviewers: Mike Hansen
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description (last modified by nthiery)

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.

Attachments (1)

trac_7776-inject_variable-nt.patch (2.5 KB) - added by nthiery 8 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 8 years ago by nthiery

  • Description modified (diff)
  • Status changed from new to needs_review

comment:2 Changed 8 years ago by nthiery

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

comment:3 follow-up: Changed 8 years ago by robertwb

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

Changed 8 years ago by nthiery

comment:4 in reply to: ↑ 3 Changed 8 years ago by nthiery

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.

comment:5 Changed 8 years ago by mhansen

  • Reviewers set to Mike Hansen
  • Status changed from needs_review to positive_review

This looks good to me.

comment:6 follow-up: Changed 8 years ago by 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.

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?

comment:7 Changed 8 years ago by rlm

  • Merged in set to sage-4.3.1.rc0
  • Resolution set to fixed
  • Status changed from positive_review to closed

comment:8 in reply to: ↑ 6 Changed 8 years ago by nthiery

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.

Note: See TracTickets for help on using tickets.