On Tue, 31 Oct 2006 16:18:36 -0800, Martin Albrecht
<[EMAIL PROTECTED]> wrote:
>
>> because its potentially confusing. Thus here is my proposal.
>>
>> (1) The default behavior is exactly as in SAGE 1.4, i.e., no namespace
>> injection and one has the R.<x,y,z> = PolynomialRing(QQ,3)
>> constructor.
>> 'll rework the preparser to make sure this can be done with
>> violating
>> global uniqueness of parent structures (i.e., I'll not use
>> assign_names).
>>
>> (2) There is a mode that users can turn on if they want, which does the
>> namespace injection. They type inject_on() to turn it on. They
>> can
>> put this in their init.sage startup file. Doing inject_on replaces
>> certain constructors with wrappers that do namespace injection.
>>
>> (3) If R is any structure with generators, R.inject_names(dict) will
>> inject
>> the variable names of R into the given dictionary (which, e.g.,
>> could
>> be globals()).
>>
>> -- William
>
> I like that proposal.
Good since I mostly implemented it already. ;-)
I also added a cool new context manager for variable names, using the
new Python with statement. This is relevant since now one can't change
variables names once and for all for rings, etc., because we want those
to be globally immutable. However, being able to change them locally
in a specific block, so they are guaranteed to be changed at the end of
the block, seems useful. So I'm happy to have written my first context
manager... it's actually very very easy.
class localvars:
r"""
Context manager for safely temporarily changing the variables
names of an object with generators.
Objects with named generators are globally unique in SAGE.
Sometimes, though, it is very useful to be able to temporarily
display the generators differently. The new Python "with"
statement and the localvars context manager make this easy and
safe (and fun!)
Suppose X is any object with generators. Write
\begin{verbatim}
with localvars(X, names[, latex_names] [,normalize=False]):
some code
...
\end{verbatim}
and the indented code will be run as if the names in X are changed
to the new names. If you give normalize=True, then the names are
assumed to be a tuple of the correct number of strings.
If you're writing Python library code, you currently have
to put \code{from __future__ import with_statement} in your file
in order to use the \code{with} statement. This restriction will
disappear in Python 2.6.
EXAMPLES:
sage: R.[x,y] = PolynomialRing(QQ,2)
sage: with localvars(R, 'z,w'):
... print x^3 + y^3 - x*y
...
w^3 - z*w + z^3
NOTES: I wrote this because it was needed to print elements of the
quotient of a ring R by an ideal I using the print function for
elemnts of R. See the code in \code{quotient_ring_element.pyx}.
AUTHOR: William Stein (2006-10-31)
"""
def __init__(self, obj, names, latex_names=None, normalize=True):
self._obj = obj
if normalize:
self._names = normalize_names(obj.ngens(), names)
self._latex_names = latex_names
else:
self._names = normalize_names(obj.ngens(), names)
self._latex_names = latex_names
def __enter__(self):
self._orig = self._obj.__temporarily_change_names(self._names,
self._latex_names)
def __exit__(self, type, value, traceback):
self._obj.__temporarily_change_names(self._orig[0], self._orig[1])
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~----------~----~----~----~------~----~------~--~---