Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

r""" 

User-interface globals 

 

These represent "globals" in the top-level user interface (command 

line, notebook). When starting up Sage, ``user_globals`` becomes 

identical to the ``globals()`` of the top-level frame. Later on, 

functions can inject globals. 

 

.. NOTE:: 

 

Injecting globals is a feature which has some legitimate uses, but 

beware of over-using it. A natural use case for injecting globals is 

for "compile-and-import" functions, such as :func:`cython`. 

 

EXAMPLES: 

 

This is how a typical user interface initializes the globals:: 

 

sage: ui_globals = globals() # or wherever the user interface stores its globals 

sage: from sage import all_cmdline # or all_notebook 

sage: from sage.repl.user_globals import initialize_globals 

sage: _ = initialize_globals(all_cmdline, ui_globals) 

 

Now everything which was imported in ``all_cmdline`` is available as a 

global:: 

 

sage: from sage.repl.user_globals import get_global, set_global 

sage: get_global("Matrix") 

<sage.matrix.constructor.MatrixFactory object at ...> 

 

This is exactly the same:: 

 

sage: ui_globals["Matrix"] 

<sage.matrix.constructor.MatrixFactory object at ...> 

 

We inject a global:: 

 

sage: set_global("myvar", "Hello World!") 

sage: get_global("myvar") 

'Hello World!' 

 

If we set up things correctly, this new variable is now actually 

available as global:: 

 

sage: myvar 

'Hello World!' 

 

AUTHORS: 

 

- Jeroen Demeyer (2015-03-30): initial version (:trac:`12446`) 

""" 

 

#***************************************************************************** 

# Copyright (C) 2015 Jeroen Demeyer <jdemeyer@cage.ugent.be> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

 

 

user_globals = None 

 

 

def _check(): 

""" 

Raise ``RuntimeError`` if ``user_globals`` has not been initialized. 

 

EXAMPLES:: 

 

sage: import sage.repl.user_globals 

sage: sage.repl.user_globals._check() 

sage: sage.repl.user_globals.user_globals = None 

sage: sage.repl.user_globals._check() 

Traceback (most recent call last): 

... 

RuntimeError: the user-space globals dictionary has not been initialized... 

""" 

if user_globals is None: 

raise RuntimeError( 

"the user-space globals dictionary has not been initialized. " 

"Use initialize_globals() or set_globals() or use a different " 

"function which doesn't need these globals") 

 

 

def get_globals(): 

""" 

Return the dictionary of all user globals. 

 

EXAMPLES:: 

 

sage: from sage.repl.user_globals import get_globals, initialize_globals 

sage: initialize_globals(sage.all) 

sage: get_globals()["Matrix"] 

<sage.matrix.constructor.MatrixFactory object at ...> 

""" 

_check() 

return user_globals 

 

 

def set_globals(g): 

""" 

Set the dictionary of all user globals to ``g``. 

 

INPUT: 

 

- ``g`` -- a dictionary. Typically, this will be some dictionary 

given by the user interface or just ``globals()``. 

 

EXAMPLES:: 

 

sage: from sage.repl.user_globals import get_globals, set_globals 

sage: my_dict = dict() 

sage: set_globals(my_dict) 

sage: my_dict is get_globals() 

True 

""" 

global user_globals 

user_globals = g 

 

 

def initialize_globals(all, g=None): 

""" 

Set the user globals dictionary to ``g`` and assign everything 

which was imported in module ``all`` as global. 

 

INPUT: 

 

- ``all`` -- a module whose globals will be injected 

 

- ``g`` -- a dictionary, see :func:`set_globals`. If this is 

``None``, keep the current globals dictionary. 

 

EXAMPLES:: 

 

sage: my_globs = {"foo": "bar"} 

sage: from sage.repl.user_globals import initialize_globals 

sage: initialize_globals(sage.all, my_globs) 

sage: my_globs["foo"] 

'bar' 

sage: my_globs["Matrix"] 

<sage.matrix.constructor.MatrixFactory object at ...> 

 

Remove ``Matrix`` from the globals and initialize again without 

changing the dictionary:: 

 

sage: del my_globs["Matrix"] 

sage: initialize_globals(sage.all) 

sage: my_globs["Matrix"] 

<sage.matrix.constructor.MatrixFactory object at ...> 

""" 

if g is not None: 

set_globals(g) 

for key in dir(all): 

if key[0] != '_': 

user_globals[key] = getattr(all, key) 

 

 

def get_global(name): 

""" 

Return the value of global variable ``name``. Raise ``NameError`` 

if there is no such global variable. 

 

INPUT: 

 

- ``name`` -- a string representing a variable name 

 

OUTPUT: the value of variable ``name`` 

 

EXAMPLES:: 

 

sage: from sage.repl.user_globals import get_global 

sage: the_answer = 42 

sage: get_global("the_answer") 

42 

sage: get_global("the_question") 

Traceback (most recent call last): 

... 

NameError: name 'the_question' is not defined 

""" 

_check() 

try: 

return user_globals[name] 

except KeyError: 

raise NameError("name {!r} is not defined".format(name)) 

 

 

def set_global(name, value): 

""" 

Assign ``value`` to global variable ``name``. This is equivalent 

to executing ``name = value`` in the global namespace. 

 

INPUT: 

 

- ``name`` -- a string representing a variable name 

 

- ``value`` -- a value to assign to the variable 

 

EXAMPLES:: 

 

sage: from sage.repl.user_globals import set_global 

sage: set_global("the_answer", 42) 

sage: the_answer 

42 

""" 

_check() 

user_globals[name] = value