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

"Evaluating Python code without any preparsing" 

 

class Python: 

""" 

Allows for evaluating a chunk of code without any preparsing. 

""" 

 

def eval(self, x, globals, locals=None): 

r""" 

Evaluate x with given globals; locals is completely ignored. 

This is specifically meant for evaluating code blocks with 

``%python`` in the notebook. 

 

INPUT: 

 

x -- a string 

globals -- a dictionary 

locals -- completely IGNORED 

 

EXAMPLES:: 

 

sage: from sage.misc.python import Python 

sage: python = Python() 

sage: python.eval('2+2', globals()) 

4 

'' 

 

Any variables that are set during evaluation of the block 

will propagate to the globals dictionary. :: 

 

sage: python.eval('a=5\nb=7\na+b', globals()) 

12 

'' 

sage: b 

7 

 

The locals variable is ignored -- it is there only for 

completeness. It is ignored since otherwise the following 

will not work:: 

 

sage: python.eval("def foo():\n return 'foo'\nprint(foo())\ndef mumble():\n print('mumble {}'.format(foo()))\nmumble()", globals()) 

foo 

mumble foo 

'' 

sage: mumble 

<function mumble at ...> 

""" 

x = x.strip() 

y = x.split('\n') 

if len(y) == 0: 

return '' 

s = '\n'.join(y[:-1]) + '\n' 

t = y[-1] 

try: 

z = compile(t + '\n', '', 'single') 

except SyntaxError: 

s += '\n' + t 

z = None 

 

eval(compile(s, '', 'exec'), globals, globals) 

 

if not z is None: 

eval(z, globals) 

return '' 

 

python = Python()