Beispiel #1
0
 def eval(self, code, strip=True, **kwds):
     """
     EXAMPLES:
         sage: lisp.eval('(+ 2 2)')
         '4'
     """
     with gc_disabled():
         self._synchronize()
         code = str(code)
         code = code.strip()
         code = code.replace('\n', ' ')
         x = []
         for L in code.split('\n'):
             if L != '':
                 try:
                     s = self.__in_seq + 1
                     pr = '\[%s\]>' % s
                     M = self._eval_line(L, wait_for_prompt=self._prompt)
                     phrase = '[C\x1b[C\n'
                     phrase = phrase if phrase in M else L
                     i = M.rfind(phrase)
                     if i > 1:
                         M = M[i + len(phrase):]
                     x.append(M.strip())
                     self.__in_seq = s
                 except KeyboardInterrupt:
                     # DO NOT CATCH KeyboardInterrupt, as it is being caught
                     # by _eval_line
                     # In particular, do NOT call self._keyboard_interrupt()
                     raise
                 except TypeError, s:
                     return 'error evaluating "%s":\n%s' % (code, s)
         return '\n'.join(x)
Beispiel #2
0
    def eval(self, code, strip=True, **kwds):
        """
        EXAMPLES::

            sage: lisp.eval('(+ 2 2)')
            '4'

        TEST:

        Verify that it works when input == output::

            sage: lisp.eval('2')
            '2'
        """
        with gc_disabled():
            self._synchronize()
            code = str(code)
            code = code.strip()
            code = code.replace('\n', ' ')
            x = []
            for L in code.split('\n'):
                if L != '':
                    try:
                        s = self.__in_seq + 1
                        M = self._eval_line(L, wait_for_prompt=self._prompt)
                        if M.startswith(L + "\n"):
                            M = M[len(L):]  # skip L in case it was echoed
                        x.append(M.strip())
                        self.__in_seq = s
                    except TypeError as s:
                        return 'error evaluating "%s":\n%s' % (code, s)
            return '\n'.join(x)
Beispiel #3
0
 def _eval_line(self, line, allow_use_file=True, wait_for_prompt=True):
     """
     EXAMPLES::
     
         sage: maple._eval_line('2+2')  # optional - maple
         '4'
     """
     line += ';'
     with gc_disabled():
         z = Expect._eval_line(self, line, allow_use_file=allow_use_file,
                 wait_for_prompt=wait_for_prompt).replace('\\\n','').strip()
         if z.lower().find("error") != -1:
             # The following was very tricky to figure out.
             # When an error occurs using Maple, unfortunately,
             # Maple also dumps one into the line where the
             # error occurred with that line copied in.  This
             # totally messes up the pexpect interface.  However,
             # I think the following few lines successfully
             # "clear things out", i.e., delete the text from
             # the edit buffer and get a clean prompt. 
             e = self.expect()
             e.sendline('%s__sage__;'%(chr(8)*len(line)))
             e.expect('__sage__;')
             e.expect(self._prompt)            
             raise RuntimeError, "An error occurred running a Maple command:\nINPUT:\n%s\nOUTPUT:\n%s"%(line, z)
     return z
Beispiel #4
0
 def _eval_line(self,
                line,
                allow_use_file=True,
                wait_for_prompt=True,
                restart_if_needed=False):
     """
     EXAMPLES::
     
         sage: maple._eval_line('2+2')  # optional - maple
         '4'
     """
     line += ';'
     with gc_disabled():
         z = Expect._eval_line(self,
                               line,
                               allow_use_file=allow_use_file,
                               wait_for_prompt=wait_for_prompt).replace(
                                   '\\\n', '').strip()
         if z.lower().find("error") != -1:
             # The following was very tricky to figure out.
             # When an error occurs using Maple, unfortunately,
             # Maple also dumps one into the line where the
             # error occurred with that line copied in.  This
             # totally messes up the pexpect interface.  However,
             # I think the following few lines successfully
             # "clear things out", i.e., delete the text from
             # the edit buffer and get a clean prompt.
             e = self.expect()
             e.sendline('%s__sage__;' % (chr(8) * len(line)))
             e.expect('__sage__;')
             e.expect(self._prompt)
             raise RuntimeError, "An error occurred running a Maple command:\nINPUT:\n%s\nOUTPUT:\n%s" % (
                 line, z)
     return z
Beispiel #5
0
 def eval(self, code, strip=True, **kwds):
     """
     EXAMPLES:
         sage: lisp.eval('(+ 2 2)')
         '4'
     """
     with gc_disabled():
         self._synchronize()
         code = str(code)
         code = code.strip()
         code = code.replace('\n',' ')
         x = []
         for L in code.split('\n'):
             if L != '':
                 try:
                     s = self.__in_seq + 1
                     pr = '\[%s\]>'%s
                     M = self._eval_line(L, wait_for_prompt=self._prompt)
                     phrase = '[C\x1b[C\n'
                     phrase = phrase if phrase in M else L
                     i = M.rfind(phrase)
                     if i > 1:
                         M = M[i+len(phrase):]
                     x.append(M.strip())
                     self.__in_seq = s
                 except KeyboardInterrupt:
                     # DO NOT CATCH KeyboardInterrupt, as it is being caught
                     # by _eval_line
                     # In particular, do NOT call self._keyboard_interrupt()
                     raise
                 except TypeError, s:
                     return 'error evaluating "%s":\n%s'%(code,s)
         return '\n'.join(x)
Beispiel #6
0
    def display2d(self, onscreen=True):
        """
        Return the 2d string representation of this Maxima object.

        EXAMPLES::

            sage: F = maxima('x^5 - y^5').factor()
            sage: F.display2d()
                                   4      3    2  2    3      4
                       - (y - x) (y  + x y  + x  y  + x  y + x )
        """
        self._check_valid()
        P = self.parent()
        with gc_disabled():
            P._eval_line('display2d : true$')
            s = P._eval_line('disp(%s)$'%self.name(), reformat=False)
            P._eval_line('display2d : false$')
        s = s.strip('\r\n')

        # if ever want to dedent, see
        # http://mail.python.org/pipermail/python-list/2006-December/420033.html
        if onscreen:
            print s
        else:
            return s
Beispiel #7
0
    def display2d(self, onscreen=True):
        """
        Return the 2d string representation of this Maxima object.

        EXAMPLES::

            sage: F = maxima('x^5 - y^5').factor()
            sage: F.display2d()
                                   4      3    2  2    3      4
                       - (y - x) (y  + x y  + x  y  + x  y + x )
        """
        self._check_valid()
        P = self.parent()
        with gc_disabled():
            P._eval_line('display2d : true$')
            s = P._eval_line('disp(%s)$'%self.name(), reformat=False)
            P._eval_line('display2d : false$')
        s = s.strip('\r\n')

        # if ever want to dedent, see
        # http://mail.python.org/pipermail/python-list/2006-December/420033.html
        if onscreen:
            print s
        else:
            return s
Beispiel #8
0
    def eval(self, code, strip=True, **kwds):
        """
        EXAMPLES::

            sage: lisp.eval('(+ 2 2)')
            '4'

        TEST:

        Verify that it works when input == output::

            sage: lisp.eval('2')
            '2'
        """
        with gc_disabled():
            self._synchronize()
            code = str(code)
            code = code.strip()
            code = code.replace('\n',' ')
            x = []
            for L in code.split('\n'):
                if L != '':
                    try:
                        s = self.__in_seq + 1
                        M = self._eval_line(L, wait_for_prompt=self._prompt)
                        if M.startswith(L + "\n"):
                            M = M[len(L):]      # skip L in case it was echoed
                        x.append(M.strip())
                        self.__in_seq = s
                    except TypeError, s:
                        return 'error evaluating "%s":\n%s'%(code,s)
            return '\n'.join(x)
Beispiel #9
0
    def eval(self, code, strip=True, **kwds):
        """
        EXAMPLES:
            sage: lisp.eval('(+ 2 2)')
            '4'

        TEST:
            # Verify that it works when input == output
            sage: lisp.eval('2')
            '2'
        """
        with gc_disabled():
            self._synchronize()
            code = str(code)
            code = code.strip()
            code = code.replace('\n',' ')
            x = []
            for L in code.split('\n'):
                if L != '':
                    try:
                        s = self.__in_seq + 1
                        M = self._eval_line(L, wait_for_prompt=self._prompt)
                        if M.startswith(L + "\n"):
                            M = M[len(L):]      # skip L in case it was echoed
                        x.append(M.strip())
                        self.__in_seq = s
                    except KeyboardInterrupt:
                        # DO NOT CATCH KeyboardInterrupt, as it is being caught
                        # by _eval_line
                        # In particular, do NOT call self._keyboard_interrupt()
                        raise
                    except TypeError, s:
                        return 'error evaluating "%s":\n%s'%(code,s)
            return '\n'.join(x)
Beispiel #10
0
    def _eval_line(self, line, allow_use_file=True, wait_for_prompt=True, restart_if_needed=False):
        """
        EXAMPLES::

            sage: maple._eval_line('2+2')  # optional - maple
            '4'
        """
        line += ';'
        with gc_disabled():
            z = Expect._eval_line(self, line, allow_use_file=allow_use_file,
                    wait_for_prompt=wait_for_prompt).replace('\\\n','').strip()
            if z.lower().find("error") != -1:
                raise RuntimeError("An error occurred running a Maple command:\nINPUT:\n%s\nOUTPUT:\n%s" % (line, z))
        return z
Beispiel #11
0
    def _eval_line(self,
                   line,
                   allow_use_file=True,
                   wait_for_prompt=True,
                   restart_if_needed=False):
        """
        EXAMPLES::

            sage: maple._eval_line('2+2')  # optional - maple
            '4'
        """
        line += ';'
        with gc_disabled():
            z = Expect._eval_line(self,
                                  line,
                                  allow_use_file=allow_use_file,
                                  wait_for_prompt=wait_for_prompt).replace(
                                      '\\\n', '').strip()
            if z.lower().find("error") != -1:
                raise RuntimeError(
                    "An error occurred running a Maple command:\nINPUT:\n%s\nOUTPUT:\n%s"
                    % (line, z))
        return z