コード例 #1
0
ファイル: builtins.py プロジェクト: FHe/tdl
 def more(self,name,pagesize=24):
     """
     List file contents
     """
     try:
         f = open(name)
         l = f.readlines()
         f.close()
         show_more(l,filename=name,pagesize=pagesize)
     except IOError:
         print "cannot open file: %s." % name
         return
コード例 #2
0
ファイル: image_menu.py プロジェクト: jackey-qiu/tdl
def bgr_menu(bgr_params=IMG_BGR_PARAMS):
    """
    Get background options
    """
    prompt   = 'Select option >'

    # make menu
    m   = Menu(labels=BGR_LABELS,descr=BGR_DESCR,sort=False,matchidx=True)
    ret = ''
    
    while ret != 'done':
        header = BGR_HEADER % (bgr_params['bgrflag'],
                               bgr_params['cnbgr'],bgr_params['cwidth'],
                               bgr_params['cpow'],str(bgr_params['ctan']),
                               bgr_params['rnbgr'],bgr_params['rwidth'],
                               bgr_params['rpow'],str(bgr_params['rtan']))
        m.header = header
        ret      = m.prompt(prompt)
        #
        if ret == 'bgrflag':
            bgr_params['bgrflag'] = get_int(prompt='Enter bgrflag',
                                            default=bgr_params['bgrflag'],
                                            valid=[0,1,2,3])
        #
        elif ret == 'cnbgr':
            bgr_params['cnbgr'] = get_int(prompt='Enter col nbgr',
                                          default=bgr_params['cnbgr'],
                                          min=0)
        elif ret == 'cwidth':
            # could use flt??
            bgr_params['cwidth'] = get_int(prompt='Enter col width',
                                           default=bgr_params['cwidth'],
                                           min=0)
        elif ret == 'cpow':
            bgr_params['cpow'] = get_flt(prompt='Enter col pow',
                                         default=bgr_params['cpow'],
                                         min=0.)
        elif ret == 'ctan':
            bgr_params['ctan'] = get_tf(prompt='Enter col tan flag',
                                        default=bgr_params['ctan'])
        #
        elif ret == 'rnbgr':
            bgr_params['rnbgr'] = get_int(prompt='Enter row nbgr',
                                          default=bgr_params['rnbgr'],
                                          min=0)
        elif ret == 'rwidth':
            # could use flt??
            bgr_params['rwidth'] = get_int(prompt='Enter row width',
                                           default=bgr_params['rwidth'],
                                           min=0)
        elif ret == 'rpow':
            bgr_params['rpow'] = get_flt(prompt='Enter row pow',
                                         default=bgr_params['rpow'],
                                         min=0.)
        elif ret == 'rtan':
            bgr_params['rtan'] = get_tf(prompt='Enter row tan flag',
                                        default=bgr_params['rtan'])
        elif ret == 'info':
            show_more(BGR_INFO)
            get_yn(prompt="Continue",default='y')
    return bgr_params
コード例 #3
0
ファイル: numshell.py プロジェクト: jackey-qiu/tdl
    def do_calc(self, line=''):
        """
        Calculator.  Help is available from the 'calc' command prompt
        """
        help_1 = "*** Calculator: 'q' = exit, 'h' for help, 'u' = use examples"
        help_2 = """*** Calculator Commands
        \r'c' = clear buffer
        \r'd' = debug (default off)
        \r'f' = convert results to float (default on)
        \r'h' = help
        \r'p' = pop last entry from buffer
        \r'q' = exit
        \r'r' = take product of all values in buffer
        \r's' = show buffer
        \r't' = total all values in buffer
        \r'u' = useage examples
        \r'v' = verbose listing of buffer values (default on)
        \r'w' = swap last two entries in buffer
        """
        use = """
        \r*** Useage
        \r In the below examples the result of a computation is
        \r appended to the buffer.  Use:
        \r   's' to inspect the buffer,
        \r   'w' to swap the top 2 buffer values,
        \r   'p' to pop (remove) the top buffer value
        \r Note, be cautious of integer arithmetic.  Even if the
        \r float flag is turned on ('f'), only the result
        \r of an evaluated expression will be converted to float. 
        \r Therefore, its best to explicitly use floating point numbers...
        
        \r*** Examples
        \r# Place values into the buffer (note b is the top of the buffer)
        \rcalc>3                # --> a
        \rcalc>6                # --> b  
        
        \r# A sole operate works on last two buffer entries
        \rcalc>-                # --> op
        \r  = -3                #  3 - 6 = a op b

        \r# A trailing operator works the same
        \rcalc>3                # --> a
        \rcalc>6-               # --> b op
        \r  = -3                #  3 - (6) = a op (b)

        \r# When an operator trails an expression the buffer
        \r# the expression is evaluated before the opertor
        \r# and buffer value are combined, i.e.
        \r# the expression is placed in parenthesis
        \rcalc>3                # --> a
        \rcalc>6+4-             # --> expr op
        \r  = -7                #  3 - (6 + 4) = a op (expr) 

        \r# The top value in the buffer can be used in an
        \r# expression with pop:
        \rcalc>10               # --> a
        \rcalc>num.log10(pop)   # --> expr(a)
        \r  = 1.0               #  num.log(3) = expr(a)

        \r# The pop occurs first, so when combined with the
        \r# operator syntax, the buffer value in the constructed
        \r# expression is the second in the list (note only one
        \r# pop is allowed in an expression)
        \rcalc>1                # --> a
        \rcalc>10               # --> b
        \rcalc>num.log10(pop)-  # --> expr(b) op
        \r  = 0.0               # 1 - (num.log10(10)) = a op (expr(b))
        \r
        """
        ################
        ex = self.interp.execute
        ev = self.interp.evaluate

        ################
        if len(line) > 0:
            print ev(line)
            return

        ################
        print help_1
        self.interp.symbol_table.del_symbol('__calc__')
        ex('__calc__ = group()')
        self.interp.set_data('__calc__.buff', [])
        self.interp.set_data('__calc__.val', 0.0)

        ################
        def buff_len():
            tmp = self.interp.get_data('__calc__.buff')
            if tmp == None: return 0
            return len(tmp)

        ################
        def get_line(line):
            # check for pop's
            idx = line.find('pop')
            if idx > -1:
                if buff_len() > 0:
                    ex("__calc__.p = __calc__.buff.pop()", print_err=False)
                    line = line[:idx] + "__calc__.p" + line[idx + 3:]
                else:
                    print "Buffer empty"
                    return None

            # look at operators...
            if line in ('+', '-', '/', '*'):
                """
                here do: former op later --> a op b
                """
                if buff_len() < 2:
                    print "Buffer has fewer than 2 values"
                    line = None
                else:
                    ex("__calc__.b = __calc__.buff.pop()", print_err=False)
                    ex("__calc__.a = __calc__.buff.pop()", print_err=False)
                    line = "__calc__.a  %s  __calc__.b" % (line)
            elif line[-1] in ('+', '-', '/', '*'):
                """
                here do: buff op expr 
                """
                op = line[-1]
                line = line[:-1]
                if buff_len() < 1:
                    print "Buffer empty"
                    line = None
                else:
                    ex("__calc__.a = __calc__.buff.pop()", print_err=False)
                    line = "__calc__.a %s (%s)" % (op, line)
            return line

        ################
        debug = False
        do_float = True
        do_verbose = True
        calc_types = [
            types.BooleanType, types.ComplexType, types.FloatType,
            types.IntType, types.LongType
        ]
        try:
            import numpy
            calc_types.append(numpy.ndarray)
            for xx in numpy.typeDict.values():
                calc_types.append(xx)
        except:
            numpy = None

        ################
        while 1:
            line = raw_input('calc>')
            line = line.strip()
            if len(line) > 0:
                if line in ('q', 'quit'):
                    return
                elif line in ('h', 'help'):
                    print help_2
                elif line in ('u', 'use'):
                    #print use
                    show_more(use)
                elif line in ('d', 'debug'):
                    debug = not debug
                    print "Debug = ", debug
                elif line in ('f', 'float'):
                    do_float = not do_float
                    print "Do float = ", do_float
                elif line in ('v', 'verbose'):
                    do_verbose = not do_verbose
                    print "Verbose = ", do_verbose
                elif line in ('c', 'clear'):
                    ex('__calc__.buff = []')
                elif line in ('s', 'show'):
                    buff = self.interp.get_data('__calc__.buff')
                    for v in buff:
                        print v
                elif line in ('w', 'swap'):
                    if buff_len() > 1:
                        ex('__calc__.a = __calc__.buff.pop()')
                        ex('__calc__.b = __calc__.buff.pop()')
                        ex('__calc__.buff.append(__calc__.a)')
                        ex('__calc__.buff.append(__calc__.b)')
                elif line in ('p', 'pop'):
                    if buff_len() > 0:
                        ex('print __calc__.buff.pop()')
                elif line in ('t', 'total'):
                    total = 0.0
                    buff = self.interp.get_data('__calc__.buff')
                    for v in buff:
                        total = total + v
                    self.interp.set_data('__calc__.buff', [total])
                    self.interp.set_data('__calc__.val', 0.0)
                    print "  = ", total
                elif line in ('r', 'product'):
                    total = 1.0
                    buff = self.interp.get_data('__calc__.buff')
                    for v in buff:
                        total = total * v
                    self.interp.set_data('__calc__.buff', [total])
                    self.interp.set_data('__calc__.val', 0.0)
                    print "  = ", total
                else:
                    # finally execute line
                    line = get_line(line)
                    if debug: print line
                    if line:
                        if debug: print_err = True
                        else: print_err = False
                        try:
                            val = ev(line, print_err=print_err)
                            if type(val) in calc_types:
                                if do_float:
                                    try:
                                        val = float(val)
                                    except:
                                        pass
                                self.interp.set_data('__calc__.val', val)
                                ex('__calc__.buff.append(__calc__.val)')
                                if do_verbose:
                                    buff = self.interp.get_data(
                                        '__calc__.buff')
                                    for v in buff:
                                        print v
                                else:
                                    print "  = ", val
                            else:
                                print "Error in type returned to calc: %s" % type(
                                    val)
                        except:
                            print "Error evaluating: %s" % line
                            self.interp.set_data('__calc__.val', 0.0)
                    else:
                        val = None
                        self.interp.set_data('__calc__.val', 0.0)
コード例 #4
0
ファイル: numshell.py プロジェクト: FHe/tdl
    def do_calc(self, line=''):
        """
        Calculator.  Help is available from the 'calc' command prompt
        """
        help_1 = "*** Calculator: 'q' = exit, 'h' for help, 'u' = use examples"
        help_2 = """*** Calculator Commands
        \r'c' = clear buffer
        \r'd' = debug (default off)
        \r'f' = convert results to float (default on)
        \r'h' = help
        \r'p' = pop last entry from buffer
        \r'q' = exit
        \r'r' = take product of all values in buffer
        \r's' = show buffer
        \r't' = total all values in buffer
        \r'u' = useage examples
        \r'v' = verbose listing of buffer values (default on)
        \r'w' = swap last two entries in buffer
        """
        use = """
        \r*** Useage
        \r In the below examples the result of a computation is
        \r appended to the buffer.  Use:
        \r   's' to inspect the buffer,
        \r   'w' to swap the top 2 buffer values,
        \r   'p' to pop (remove) the top buffer value
        \r Note, be cautious of integer arithmetic.  Even if the
        \r float flag is turned on ('f'), only the result
        \r of an evaluated expression will be converted to float. 
        \r Therefore, its best to explicitly use floating point numbers...
        
        \r*** Examples
        \r# Place values into the buffer (note b is the top of the buffer)
        \rcalc>3                # --> a
        \rcalc>6                # --> b  
        
        \r# A sole operate works on last two buffer entries
        \rcalc>-                # --> op
        \r  = -3                #  3 - 6 = a op b

        \r# A trailing operator works the same
        \rcalc>3                # --> a
        \rcalc>6-               # --> b op
        \r  = -3                #  3 - (6) = a op (b)

        \r# When an operator trails an expression the buffer
        \r# the expression is evaluated before the opertor
        \r# and buffer value are combined, i.e.
        \r# the expression is placed in parenthesis
        \rcalc>3                # --> a
        \rcalc>6+4-             # --> expr op
        \r  = -7                #  3 - (6 + 4) = a op (expr) 

        \r# The top value in the buffer can be used in an
        \r# expression with pop:
        \rcalc>10               # --> a
        \rcalc>num.log10(pop)   # --> expr(a)
        \r  = 1.0               #  num.log(3) = expr(a)

        \r# The pop occurs first, so when combined with the
        \r# operator syntax, the buffer value in the constructed
        \r# expression is the second in the list (note only one
        \r# pop is allowed in an expression)
        \rcalc>1                # --> a
        \rcalc>10               # --> b
        \rcalc>num.log10(pop)-  # --> expr(b) op
        \r  = 0.0               # 1 - (num.log10(10)) = a op (expr(b))
        \r
        """
        ################
        ex = self.interp.execute
        ev = self.interp.evaluate
        
        ################
        if len(line) > 0:
            print ev(line)
            return

        ################
        print help_1
        self.interp.symbol_table.del_symbol('__calc__')
        ex('__calc__ = group()')
        self.interp.set_data('__calc__.buff',[])
        self.interp.set_data('__calc__.val',0.0)

        ################
        def buff_len():
            tmp = self.interp.get_data('__calc__.buff')
            if tmp == None: return 0
            return len(tmp)
        
        ################
        def get_line(line):
            # check for pop's
            idx = line.find('pop')
            if idx > -1:
                if buff_len() > 0:
                    ex("__calc__.p = __calc__.buff.pop()",print_err=False)
                    line = line[:idx] + "__calc__.p" + line[idx+3:]
                else:
                    print "Buffer empty"
                    return None

            # look at operators...
            if line in ('+','-','/','*'):
                """
                here do: former op later --> a op b
                """
                if buff_len() < 2:
                    print "Buffer has fewer than 2 values"
                    line = None
                else:
                    ex("__calc__.b = __calc__.buff.pop()",print_err=False)
                    ex("__calc__.a = __calc__.buff.pop()",print_err=False)
                    line = "__calc__.a  %s  __calc__.b" % (line)
            elif line[-1] in ('+','-','/','*'):
                """
                here do: buff op expr 
                """
                op = line[-1]
                line = line[:-1]
                if buff_len() < 1:
                    print "Buffer empty"
                    line = None
                else:
                    ex("__calc__.a = __calc__.buff.pop()",print_err=False)
                    line = "__calc__.a %s (%s)" % (op,line)
            return line
        
        ################
        debug      = False
        do_float   = True
        do_verbose = True
        calc_types = [types.BooleanType, types.ComplexType,
                      types.FloatType, types.IntType,
                      types.LongType]
        try:
            import numpy
            calc_types.append(numpy.ndarray)
            for xx in numpy.typeDict.values():
                calc_types.append(xx)
        except:
            numpy = None
            
        ################
        while 1:
            line = raw_input('calc>')
            line = line.strip()
            if len(line) > 0:
                if line in ('q','quit'):
                    return
                elif line in ('h','help'):
                    print help_2
                elif line in ('u','use'):
                    #print use
                    show_more(use)
                elif line in ('d','debug'):
                    debug = not debug
                    print "Debug = ", debug
                elif line in ('f','float'):
                    do_float = not do_float
                    print "Do float = ", do_float
                elif line in ('v','verbose'):
                    do_verbose = not do_verbose
                    print "Verbose = ", do_verbose
                elif line in ('c','clear'):
                    ex('__calc__.buff = []')
                elif line in ('s','show'):
                    buff = self.interp.get_data('__calc__.buff')
                    for v in buff: print v
                elif line in ('w','swap'):
                    if buff_len() > 1:
                        ex('__calc__.a = __calc__.buff.pop()')
                        ex('__calc__.b = __calc__.buff.pop()')
                        ex('__calc__.buff.append(__calc__.a)')
                        ex('__calc__.buff.append(__calc__.b)')
                elif line in ('p','pop'):
                    if buff_len() > 0:
                        ex('print __calc__.buff.pop()')
                elif line in ('t','total'):
                    total = 0.0
                    buff = self.interp.get_data('__calc__.buff')
                    for v in buff:
                        total = total + v
                    self.interp.set_data('__calc__.buff',[total])
                    self.interp.set_data('__calc__.val',0.0)
                    print "  = " ,  total
                elif line in ('r','product'):
                    total = 1.0
                    buff = self.interp.get_data('__calc__.buff')
                    for v in buff:
                        total = total * v
                    self.interp.set_data('__calc__.buff',[total])
                    self.interp.set_data('__calc__.val',0.0)
                    print "  = " ,  total
                else:
                    # finally execute line
                    line = get_line(line)
                    if debug: print line
                    if line:
                        if debug: print_err = True
                        else: print_err = False
                        try:
                            val = ev(line,print_err=print_err)
                            if type(val) in calc_types:
                                if do_float:
                                    try: val = float(val)
                                    except: pass
                                self.interp.set_data('__calc__.val',val)
                                ex('__calc__.buff.append(__calc__.val)')
                                if do_verbose:
                                    buff = self.interp.get_data('__calc__.buff')
                                    for v in buff: print v
                                else:
                                    print "  = " ,  val
                            else:
                                print "Error in type returned to calc: %s" % type(val)
                        except:
                            print "Error evaluating: %s" % line
                            self.interp.set_data('__calc__.val',0.0)
                    else:
                        val = None
                        self.interp.set_data('__calc__.val',0.0)