def eval(self, x, newlines=False, strip=True, **kwds): r""" Send the code in the string s to the Kash interpreter and return the output as a string. INPUT: - ``s`` - string containing Kash code. - ``newlines`` - bool (default: True); if False, remove all backslash-newlines inserted by the Kash output formatter. - ``strip`` - ignored """ x = str(x) x = x.rstrip() if len(x) == 0 or x[len(x) - 1] != ';': x += ';' s = Expect.eval(self, x, **kwds) i = s.find('\r\n') if i != -1: s = s[i+2:] if newlines: return s else: return s.replace("\\\n","")
def help(self, s, pager=True): """ Print help on a given topic. EXAMPLES:: sage: print gap.help('SymmetricGroup', pager=False) Basic Groups _____________________________________________ Group Libraries ... """ tmp_to_use = self._local_tmpfile() if self.is_remote(): tmp_to_use = self._remote_tmpfile() else: tmp_to_use = self._local_tmpfile() self.eval('$SAGE.tempfile := "%s";'%tmp_to_use) line = Expect.eval(self, "? %s"%s) match = re.search("Page from (\d+)", line) if match == None: print line else: (sline,) = match.groups() if self.is_remote(): self._get_tmpfile() F = open(self._local_tmpfile(),"r") if pager: page(F.read(), start = int(sline)-1) else: return F.read()
def eval(self, code, strip=True, **kwds): """ EXAMPLES: sage: mupad.eval('2+2') # optional - mupad 4 """ s = Expect.eval(self, code, **kwds) return AsciiArtString(s)
def eval(self, code, strip=True, **kwds): """ EXAMPLES: sage: lie.eval('2+2') # optional - lie '4' """ s = Expect.eval(self, code, strip=True, **kwds) # return s.strip() if len(s) > 0 and s.find("\n") != -1: return s else: return s.strip()
def eval(self, command, *args, **kwds): """ Evaluates commands. EXAMPLES: sage: scilab.eval("5") # optional - scilab 'ans =\n \n 5.' sage: scilab.eval("d=44") # optional - scilab 'd =\n \n 44.' """ s = Expect.eval(self, command, **kwds).replace("\x1b[?1l\x1b>","").strip() return s
def set(self, var, value): """ Set the variable var to the given value. EXAMPLES: sage: macaulay2.set("a", "2") #optional sage: macaulay2.get("a") #optional 2 """ cmd = '%s=%s;'%(var,value) ans = Expect.eval(self, cmd) if ans.find("stdio:") != -1: raise RuntimeError("Error evaluating Macaulay2 code.\nIN:%s\nOUT:%s"%(cmd, ans))
def eval(self, x, newlines=False, strip=True, **kwds): r""" Send the code in the string s to the GAP interpreter and return the output as a string. INPUT: - ``s`` - string containing GAP code. - ``newlines`` - bool (default: True); if False, remove all backslash-newlines inserted by the GAP output formatter. - ``strip`` - ignored EXAMPLES:: sage: gap.eval('2+2') '4' sage: gap.eval('Print(4); #test\n Print(6);') '46' sage: gap.eval('Print("#"); Print(6);') '#6' sage: gap.eval('4; \n 6;') '4\n6' """ #We remove all of the comments: On each line, we try #to find a pound sign. If we find it, we check to see if #it is occuring in a string. If it is not in a string, we #strip off the comment. input_line = "" for line in str(x).rstrip().split('\n'): pound_position = line.rfind('#') if pound_position != -1 and not is_in_string(line, pound_position): line = line[:pound_position] input_line += line if not input_line.endswith(';'): input_line += ';' result = Expect.eval(self, input_line, **kwds) if not newlines: result = result.replace("\\\n","") return result.strip()
def eval(self, s, **kwds): """ Return mwrank's output for the given input. INPUT: - ``s`` (str) - a Sage object which when converted to a string gives valid input to ``mwrank``. The conversion is done by :meth:`validate_mwrank_input`. Possible formats are: - a string representing exactly five integers separated by whitespace, for example '1 2 3 4 5' - a string representing exactly five integers separated by commas, preceded by '[' and followed by ']' (with arbitrary whitespace), for example '[1 2 3 4 5]' - a list or tuple of exactly 5 integers. .. note:: If a RuntimeError exception is raised, then the mwrank interface is restarted and the command is retried once. EXAMPLES:: sage: mwrank.eval('12 3 4 5 6') 'Curve [12,3,4,5,6] :...' sage: mwrank.eval('[12, 3, 4, 5, 6]') 'Curve [12,3,4,5,6] :...' sage: mwrank.eval([12, 3, 4, 5, 6]) 'Curve [12,3,4,5,6] :...' sage: mwrank.eval((12, 3, 4, 5, 6)) 'Curve [12,3,4,5,6] :...' """ if self._expect is not None and not self._expect.isalive(): # if mwrank is interrupted twice in rapid succession, # then it doesn't restart correctly, and we're left with: # "RuntimeError: [Errno 9] Bad file descriptor" # Doing _start again fixes that always. See trac #5157. self._start() try: ss = validate_mwrank_input(s) return Expect.eval(self, ss, **kwds) except ValueError as err: raise ValueError('Invalid input: %s' % err) except RuntimeError: raise ValueError('Invalid input (%s) to mwrank (singular curve)' % s)
def eval(self, code, strip=True, **kwds): """ Send the code x to the Macaulay2 interpreter and return the output as a string suitable for input back into Macaulay2, if possible. INPUT: code -- str strip -- ignored EXAMPLES: sage: macaulay2.eval("2+2") #optional 4 """ code = code.strip() # TODO: in some cases change toExternalString to toString?? ans = Expect.eval(self, code, strip=strip, **kwds).strip('\n') if strip: ans = remove_output_labels(ans) return AsciiArtString(ans)
def eval(self, *args, **kwds): """ Send a line of input to mwrank, then when it finishes return everything that mwrank output. NOTE: If a RuntimeError exception is raised, then the mwrank interface is restarted and the command is retried once. EXAMPLES: sage: mwrank.eval('12 3 4 5 6') 'Curve [12,3,4,5,6] :...' """ if self._expect is not None and not self._expect.isalive(): # if mwrank is interrupted twice in rapid succession, # then it doesn't restart correctly, and we're left with: # "RuntimeError: [Errno 9] Bad file descriptor" # Doing _start again fixes that always. See trac #5157. self._start() return Expect.eval(self, *args, **kwds)
def eval(self, line, strip=True, **kwds): """ Send the code x to a second instance of the Sage interpreter and return the output as a string. This allows you to run two completely independent copies of Sage at the same time in a unified way. INPUT: - ``line`` - input line of code - ``strip`` - ignored EXAMPLES:: sage: sage0.eval('2+2') '4' """ if self._preparse: line = self.preparse(line) return Expect.eval(self, line, **kwds).strip()
def eval(self, code, strip=True, **kwds): s = Expect.eval(self, code, **kwds) if strip: return AsciiArtString(clean_output(s)) else: return AsciiArtString(s)
def eval(self, code, strip=True, **kwds): code += ';\n' return Expect.eval(self, code, strip, **kwds)[1:]
def eval(self, x, newlines=False, strip=True, split_lines=True, **kwds): r""" Send the code in the string s to the GAP interpreter and return the output as a string. INPUT: - ``s`` - string containing GAP code. - ``newlines`` - bool (default: True); if False, remove all backslash-newlines inserted by the GAP output formatter. - ``strip`` - ignored - ``split_lines`` -- bool (default: True); if True then each line is evaluated separately. If False, then the whole block of code is evaluated all at once. EXAMPLES:: sage: gap.eval('2+2') '4' sage: gap.eval('Print(4); #test\n Print(6);') '46' sage: gap.eval('Print("#"); Print(6);') '#6' sage: gap.eval('4; \n 6;') '4\n6' sage: gap.eval('if 3>2 then\nPrint("hi");\nfi;') 'hi' sage: gap.eval('## this is a test\nPrint("OK")') 'OK' sage: gap.eval('Print("This is a test. Oh no, a #");# but this is a comment\nPrint("OK")') 'This is a test. Oh no, a #OK' sage: gap.eval('if 4>3 then') '' sage: gap.eval('Print("Hi how are you?")') 'Hi how are you?' sage: gap.eval('fi') '' """ # '" #We remove all of the comments: On each line, we try #to find a pound sign. If we find it, we check to see if #it is occurring in a string. If it is not in a string, we #strip off the comment. if not split_lines: input_line = str(x) else: input_line = "" for line in str(x).rstrip().split('\n'): pound_position = line.find('#') while pound_position != -1: if not is_in_string(line, pound_position): line = line[:pound_position] pound_position = line.find('#', pound_position + 1) input_line += " " + line if not input_line.endswith(';'): input_line += ';' result = Expect.eval(self, input_line, **kwds) if not newlines: result = result.replace("\\\n", "") return result.strip()
def eval(self, x, newlines=False, strip=True, split_lines=True, **kwds): r""" Send the code in the string s to the GAP interpreter and return the output as a string. INPUT: - ``s`` - string containing GAP code. - ``newlines`` - bool (default: True); if False, remove all backslash-newlines inserted by the GAP output formatter. - ``strip`` - ignored - ``split_lines`` -- bool (default: True); if True then each line is evaluated separately. If False, then the whole block of code is evaluated all at once. EXAMPLES:: sage: gap.eval('2+2') '4' sage: gap.eval('Print(4); #test\n Print(6);') '46' sage: gap.eval('Print("#"); Print(6);') '#6' sage: gap.eval('4; \n 6;') '4\n6' sage: gap.eval('if 3>2 then\nPrint("hi");\nfi;') 'hi' sage: gap.eval('## this is a test\nPrint("OK")') 'OK' sage: gap.eval('Print("This is a test. Oh no, a #");# but this is a comment\nPrint("OK")') 'This is a test. Oh no, a #OK' sage: gap.eval('if 4>3 then') '' sage: gap.eval('Print("Hi how are you?")') 'Hi how are you?' sage: gap.eval('fi') '' """ # '" #We remove all of the comments: On each line, we try #to find a pound sign. If we find it, we check to see if #it is occurring in a string. If it is not in a string, we #strip off the comment. if not split_lines: input_line=str(x) else: input_line = "" for line in str(x).rstrip().split('\n'): pound_position = line.find('#') while pound_position != -1: if not is_in_string(line, pound_position): line = line[:pound_position] pound_position = line.find('#',pound_position+1) input_line += " "+line if not input_line.endswith(';'): input_line += ';' result = Expect.eval(self, input_line, **kwds) if not newlines: result = result.replace("\\\n","") return result.strip()