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

""" 

Operations for LibGAP Elements 

 

GAP functions for which several methods can be available are called 

operations, so GAP ``Size`` is an example of an operation. This module 

is for inspecting GAP operations from Python. In particular, it can 

list the operations that take a particular LibGAP element as first 

argument. This is used in tab completion, where Python ``x.[TAB]`` 

lists all GAP operations for which ``Operation(x, ...)`` is defined. 

""" 

 

import re 

import string 

from sage.structure.sage_object import SageObject 

from sage.misc.cachefunc import cached_method 

from sage.libs.gap.libgap import libgap 

 

Length = libgap.function_factory('Length') 

FlagsType = libgap.function_factory('FlagsType') 

TypeObj = libgap.function_factory('TypeObj') 

IS_SUBSET_FLAGS = libgap.function_factory('IS_SUBSET_FLAGS') 

OPERATIONS = libgap.get_global('OPERATIONS') 

NameFunction = libgap.function_factory('NameFunction') 

 

 

NAME_RE = re.compile('(Setter|Getter|Tester)\((.*)\)') 

 

 

class OperationInspector(SageObject): 

 

def __init__(self, libgap_element): 

""" 

Information about operations that can act on a given LibGAP element 

 

INPUT: 

 

- ``libgap_element`` -- libgap element.  

 

EXAMPLES:: 

 

sage: from sage.libs.gap.operations import OperationInspector 

sage: OperationInspector(libgap(123)) 

Operations on 123 

""" 

self._obj = libgap_element 

self.flags = FlagsType(TypeObj(self.obj)) 

 

def _repr_(self): 

""" 

Return the string representation 

 

OUTPUT: 

 

String 

 

EXAMPLES: 

 

sage: from sage.libs.gap.operations import OperationInspector 

sage: opr = OperationInspector(libgap(123)) 

sage: opr._repr_() 

'Operations on 123' 

""" 

return 'Operations on {0}'.format(repr(self._obj)) 

 

@property 

def obj(self): 

""" 

The first argument for the operations 

 

OUTPUT: 

 

A Libgap object. 

 

EXAMPLES:: 

 

sage: from sage.libs.gap.operations import OperationInspector 

sage: x = OperationInspector(libgap(123)) 

sage: x.obj 

123 

""" 

return self._obj 

 

def operations(self): 

""" 

Return the GAP operations for :meth:`obj` 

 

OUTPUT: 

 

List of GAP operations 

 

EXAMPLES:: 

 

sage: from sage.libs.gap.operations import OperationInspector 

sage: x = OperationInspector(libgap(123)) 

sage: Unknown = libgap.function_factory('Unknown') 

sage: Unknown in x.operations() 

True 

""" 

result = [] 

for i in range(len(OPERATIONS) // 2): 

match = False 

for flag_list in OPERATIONS[2*i + 1]: 

if Length(flag_list) == 0: 

continue 

first_flag = flag_list[0] 

if IS_SUBSET_FLAGS(self.flags, first_flag): 

match = True 

break 

if match: 

op = OPERATIONS[2*i] 

result.append(op) 

return result 

 

def op_names(self): 

""" 

Return the names of the operations 

 

OUTPUT: 

 

List of strings 

 

EXAMPLES:: 

 

sage: from sage.libs.gap.operations import OperationInspector 

sage: x = OperationInspector(libgap(123)) 

sage: 'Sqrt' in x.op_names() 

True 

""" 

result = set() 

for f in self.operations(): 

name = NameFunction(f).sage() 

if name[0] not in string.letters: 

continue 

match = NAME_RE.match(name) 

if match: 

result.add(match.groups()[1]) 

else: 

result.add(name) 

return sorted(result)