#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 )
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)
Change History (9)
comment:1 Changed 10 years ago by
- Description modified (diff)
- Status changed from new to needs_review
comment:2 Changed 10 years ago by
comment:3 follow-up: ↓ 4 Changed 10 years ago by
I'd rather it looked for __name__ == '__main__'
than wiki_create_instance
.
Changed 10 years ago by
comment:4 in reply to: ↑ 3 Changed 10 years ago by
Replying to robertwb:
I'd rather it looked for
__name__ == '__main__'
thanwiki_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 10 years ago by
- Reviewers set to Mike Hansen
- Status changed from needs_review to positive_review
This looks good to me.
comment:6 follow-up: ↓ 8 Changed 10 years ago by
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 10 years ago by
- 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 10 years ago by
Replying to jhpalmieri:
If I do
sage: inject_variable(3, 34) # pass a non-string to inject_variable, which I probably shouldn't dothen 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: patch prepared and tested on 4.2 not 4.3)