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

# -*- coding: utf-8 -*- 

""" 

@file 

@brief Defines magic commands to interact with C# in a :epkg:`Python` notebook. 

""" 

from IPython.core.magic import magics_class, cell_magic 

from pyquickhelper.ipythonhelper import MagicCommandParser, MagicClassWithHelpers 

from csharpy.notebook.csmagics import CsMagics 

from ..binaries import maml, mlnet 

 

 

@magics_class 

class CsMLMagics(MagicClassWithHelpers): 

""" 

Defines magic commands for notebooks. 

""" 

 

@staticmethod 

def maml_parser(): 

""" 

Defines the way to parse the magic command ``%%maml``. 

""" 

parser = MagicCommandParser(prog="maml", 

description='Runs a maml script.') 

parser.add_argument('-q', '--quiet', action='store_true', default=False, 

help='hide output') 

return parser 

 

@cell_magic 

def maml(self, line, cell): 

""" 

Defines magic command ``%%maml``. 

 

.. nbref:: 

:title: %%maml 

:lid: cmagic-maml 

 

The magic command wraps the function @see fn maml. 

 

:: 

 

%%maml 

 

data=iris.txt 

loader=text{col=Label:U4[0-2]:0 col=Slength:R4:1 col=Swidth:R4:2 

col=Plength:R4:3 col=Pwidth:R4:4 sep=, header=+} 

xf=Concat{col=Features:Slength,Swidth} 

tr=ova{p=lr} 

out=model.zip 

""" 

parser = self.get_parser(CsMLMagics.maml_parser, "maml") 

args = self.get_args(line, parser) 

 

if args is not None: 

quiet = args.quiet 

out, err = maml(cell, not quiet) 

if out: 

print(out) 

if err: 

print('-----') 

print(err) 

 

@staticmethod 

def mlnet_parser(): 

""" 

Defines the way to parse the magic command ``%%mlnet``. 

""" 

parser = MagicCommandParser(prog="mlnet", 

description='Compiles and wrap a C# function into a Python function.\n' 

'Automatically adds ML.net dependencies.') 

parser.add_argument('name', type=str, help='function name') 

parser.add_argument('-i', '--idep', nargs='*', action='append', 

help='internal dependencies (like System, System.Linq)') 

parser.add_argument('-d', '--dep', nargs='*', action='append', 

help='dependencies (assembly name without extension)') 

parser.add_argument('-c', '--catch', action='store', default=False, 

help='catch exception') 

return parser 

 

@cell_magic 

def mlnet(self, line, cell): 

""" 

Defines magic command ``%%mlnet``. 

Relies on magic command :epkg:`%%CS`. 

 

.. nbref:: 

:title: %%mlnet 

:lid: cmagic-mlnet 

 

The magic command wraps the function @see fn mlnet. 

 

:: 

 

%%mlnet 

 

""" 

line, cell = CsMagics._preprocess_line_cell_maml( # pylint: disable=W0212 

line, cell) 

 

parser = self.get_parser(CsMagics.CS_parser, "CS") 

args = self.get_args(line, parser) 

 

if args is not None: 

name = args.name 

dep = CsMagics._linearise_args(args.dep) # pylint: disable=W0212 

idep = CsMagics._linearise_args(args.idep) # pylint: disable=W0212 

 

if args.catch: 

try: 

f = mlnet(name, cell, idep, dep) 

except Exception as e: # pylint: disable=W0703 

print(str(e).replace('\r', '')) 

return None 

else: 

f = mlnet(name, cell, idep, dep) 

if self.shell is not None: 

self.shell.user_ns[name] = f 

return f 

return None 

 

 

def register_magics(ip): 

""" 

Registers magics commands. 

""" 

ip.register_magics(CsMLMagics)