Ejemplo n.º 1
0
def prefilter_PQ(self,line,continuation):
    """Alternate prefilter for input of PhysicalQuantityInteractive objects.

    This assumes that the function PhysicalQuantityInteractive() has been
    imported."""

    from re import match
    from IPython.iplib import InteractiveShell

    # This regexp is what does the real work
    unit_split = match(r'\s*(\w+)\s*=\s*(-?\d*\.?\d*[eE]?-?\d*)\s+([a-zA-Z].*)',
                       line)

    # If special input was ecnountered, process it:
    if unit_split:
        var,val,units = unit_split.groups()
        if var and val and units:
            units = units.replace('^','**')
            # Now a valid line needs to be constructed for IPython to process:
            line = var +" = PhysicalQuantityInteractive(" + val + ", '" + \
                   units + "')"
            #print 'New line:',line   # dbg
            
    # In the end, always call the default IPython _prefilter() function.  Note
    # that self must be passed explicitly, b/c we're calling the unbound class
    # method (since this method will overwrite the instance prefilter())
    return InteractiveShell._prefilter(self,line,continuation)
Ejemplo n.º 2
0
def prefilter_PQ(self, line, continuation):
    """Alternate prefilter for input of PhysicalQuantityInteractive objects.

    This assumes that the function PhysicalQuantityInteractive() has been
    imported."""

    from re import match
    from IPython.iplib import InteractiveShell

    # This regexp is what does the real work
    unit_split = match(
        r'\s*(\w+)\s*=\s*(-?\d*\.?\d*[eE]?-?\d*)\s+([a-zA-Z].*)', line)

    # If special input was ecnountered, process it:
    if unit_split:
        var, val, units = unit_split.groups()
        if var and val and units:
            units = units.replace('^', '**')
            # Now a valid line needs to be constructed for IPython to process:
            line = var +" = PhysicalQuantityInteractive(" + val + ", '" + \
                   units + "')"
            #print 'New line:',line   # dbg

    # In the end, always call the default IPython _prefilter() function.  Note
    # that self must be passed explicitly, b/c we're calling the unbound class
    # method (since this method will overwrite the instance prefilter())
    return InteractiveShell._prefilter(self, line, continuation)
def prefilter_paste(self, line, continuation):
    """Alternate prefilter for input of pasted code from an interpreter.
    """

    from re import match
    from IPython.iplib import InteractiveShell

    if match(r'^>>> |^\.\.\. ', line):
        # In the end, always call the default IPython _prefilter() function.
        # Note that self must be passed explicitly, b/c we're calling the
        # unbound class method (since this method will overwrite the instance
        # prefilter())
        return InteractiveShell._prefilter(self, line[4:], continuation)
    elif line.strip() == '...':
        return InteractiveShell._prefilter(self, '', continuation)
    else:
        return InteractiveShell._prefilter(self, line, continuation)
Ejemplo n.º 4
0
def sage_prefilter(self, block, continuation):
    """
    Sage's prefilter for input.  Given a string block (usually a
    line), return the preparsed version of it.  

    INPUT:

    - block -- string (usually a single line, but not always)
    - continuation -- whether or not this line is a continuation.
    """
    try:
        block2 = ''
        first = True
        B = block.split('\n')
        for i in range(len(B)):
            L = B[i]
            M = do_prefilter_paste(L, continuation or (not first))
            first = False
            # The L[:len(L)-len(L.lstrip())]  business here preserves
            # the whitespace at the beginning of L.
            if block2 != '':
                block2 += '\n'
            lstrip = L.lstrip()
            if lstrip[:5] == 'sage:' or lstrip[:3] == '>>>' or i==0:
                block2 += M
            else:
                block2 += L[:len(L)-len(lstrip)] + M 

    except None:
        
        print "WARNING: An error occurred in the Sage parser while"
        print "parsing the following block:"
        print block
        print "Please report this as a bug (include the output of typing '%hist')."
        block2 = block
        
    return InteractiveShell._prefilter(self, block2, continuation)
Ejemplo n.º 5
0
def sage_prefilter(self, block, continuation):
    """
    Sage's prefilter for input.  Given a string block (usually a
    line), return the preparsed version of it.  

    INPUT:

    - block -- string (usually a single line, but not always)
    - continuation -- whether or not this line is a continuation.
    """
    try:
        block2 = ''
        first = True
        B = block.split('\n')
        for i in range(len(B)):
            L = B[i]
            M = do_prefilter_paste(L, continuation or (not first))
            first = False
            # The L[:len(L)-len(L.lstrip())]  business here preserves
            # the whitespace at the beginning of L.
            if block2 != '':
                block2 += '\n'
            lstrip = L.lstrip()
            if lstrip[:5] == 'sage:' or lstrip[:3] == '>>>' or i == 0:
                block2 += M
            else:
                block2 += L[:len(L) - len(lstrip)] + M

    except None:

        print "WARNING: An error occurred in the Sage parser while"
        print "parsing the following block:"
        print block
        print "Please report this as a bug (include the output of typing '%hist')."
        block2 = block

    return InteractiveShell._prefilter(self, block2, continuation)