Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/libs/gap/context_managers.py
8815 views
1
"""
2
Context Managers for LibGAP
3
4
This module implements a context manager for global variables. This is
5
useful since the behavior of GAP is sometimes controlled by global
6
variables, which you might want to switch to a different value for a
7
computation. Here is an example how you are suppose to use it from
8
your code. First, let us set a dummy global variable for our example::
9
10
sage: libgap.set_global('FooBar', 123)
11
12
Then, if you want to switch the value momentarily you can write::
13
14
sage: with libgap.global_context('FooBar', 'test'):
15
....: print libgap.get_global('FooBar')
16
"test"
17
18
Afterward, the global variable reverts to the previous value::
19
20
sage: print libgap.get_global('FooBar')
21
123
22
23
The value is reset even if exceptions occur::
24
25
sage: with libgap.global_context('FooBar', 'test'):
26
....: print libgap.get_global('FooBar')
27
....: raise ValueError(libgap.get_global('FooBar'))
28
Traceback (most recent call last):
29
...
30
ValueError: "test"
31
sage: print libgap.get_global('FooBar')
32
123
33
"""
34
35
36
###############################################################################
37
# Copyright (C) 2012, Volker Braun <[email protected]>
38
#
39
# Distributed under the terms of the GNU General Public License (GPL)
40
# as published by the Free Software Foundation; either version 2 of
41
# the License, or (at your option) any later version.
42
# http://www.gnu.org/licenses/
43
###############################################################################
44
45
from sage.libs.gap.libgap import libgap
46
47
48
class GlobalVariableContext():
49
50
def __init__(self, variable, value):
51
"""
52
Context manager for GAP global variables.
53
54
It is recommended that you use the
55
:meth:`sage.libs.gap.libgap.Gap.global_context` method and not
56
construct objects of this class manually.
57
58
INPUT:
59
60
- ``variable`` -- string. The variable name.
61
62
- ``value`` -- anything that defines a GAP object.
63
64
EXAMPLES::
65
66
sage: libgap.set_global('FooBar', 1)
67
sage: with libgap.global_context('FooBar', 2):
68
....: print libgap.get_global('FooBar')
69
2
70
sage: libgap.get_global('FooBar')
71
1
72
"""
73
self._variable = variable
74
self._new_value = value
75
76
def __enter__(self):
77
"""
78
Called when entering the with-block
79
80
EXAMPLES::
81
82
sage: libgap.set_global('FooBar', 1)
83
sage: with libgap.global_context('FooBar', 2):
84
....: print libgap.get_global('FooBar')
85
2
86
sage: libgap.get_global('FooBar')
87
1
88
"""
89
self._old_value = libgap.get_global(self._variable)
90
libgap.set_global(self._variable, self._new_value)
91
92
def __exit__(self, exc_type, exc_val, exc_tb):
93
"""
94
Called when exiting the with-block
95
96
EXAMPLES::
97
98
sage: libgap.set_global('FooBar', 1)
99
sage: with libgap.global_context('FooBar', 2):
100
....: print libgap.get_global('FooBar')
101
2
102
sage: libgap.get_global('FooBar')
103
1
104
"""
105
libgap.set_global(self._variable, self._old_value)
106
return False
107
108