Exemple #1
0
 def do_del(self, line):
     '''Delete variables.'''
     if self.variables:
         patterns = line.split()
         if len(patterns) == 0:
             self.variables.clear()
             print('Deleted all variables.')
         else:
             names = sorted(self.variables.keys(), key=lambda s: s.lower())
             deleted = []
             try:
                 for pattern in patterns:
                     regex = compile('^' + pattern + '$')
                     for name in names:
                         if regex.match(name) and name in self.variables:
                             del self.variables[name]
                             deleted.append(name)
             except Exception:
                 # Catches re's "nothing to repeat" errors
                 # This happens, for example, for "del *"
                 # What is a more specific exception in this case?
                 print('Runtime error: Invalid pattern:', pattern)
             if deleted:
                 print_iterable(chain(['Delteted:'], deleted))
             else:
                 print('No variables matched the given pattern' +
                     int(bool(patterns[1:])) * 's' + '.')
     else:
         print('There are no variables to delete.')
Exemple #2
0
 def default(self, line):
     '''Evaluate the given expression.'''
     try:
         self.parser.parse(line)
         self.names = self.parser.names
         tree = self.parser.tree
         if tree.set_vars(self.variables) or tree.set_vars(constants):
             self.value = tree.evaluate()
             for name in self.names:
                 self.variables[name] = self.value
         else:
             raise Exception('Encountered unknown variable.')
         print_iterable(self.names, sep=', ', end=' =\n')
         print('    ' + str(self.value))
     except ParseException as ex:
         print('Runtime error:', str(ex))
         underline_substring(ex.expression, ex.start, ex.end)
     except KeyboardInterrupt:
         print('\nInterrupted.')
     except Exception as ex:
         print('Runtime error:', str(ex))