def show_message_dialog(cls, message, title="", message_type=MessageType.PLAIN): if message_type not in MessageType: error("GOptionPane::showMessageDialog: Illegal dialog type.") if not title: title = "Message" _platform.Platform().goptionpane_show_message_dialog( message, title, message_type)
def _scan_number(self): token = '' state = _NumberScannerState.INITIAL_STATE while state != _NumberScannerState.FINAL_STATE: ch = self.input_stream.read(1) if state == _NumberScannerState.INITIAL_STATE: if not ch.isdigit(): error('internal error: illegal call') state = _NumberScannerState.BEFORE_DECIMAL_POINT elif state == _NumberScannerState.BEFORE_DECIMAL_POINT: if ch == '.': state = _NumberScannerState.AFTER_DECIMAL_POINT elif ch in ('e', 'E'): state = _NumberScannerState.STARTING_EXPONENT elif not ch.isdigit(): if ch: self.unget_char(ch) state = _NumberScannerState.FINAL_STATE elif state == _NumberScannerState.AFTER_DECIMAL_POINT: if ch in ('e', 'E'): state = _NumberScannerState.STARTING_EXPONENT elif not ch.isdigit(): if ch: self.unget_char(ch) state = _NumberScannerState.FINAL_STATE elif state == _NumberScannerState.STARTING_EXPONENT: if ch in ('-', '+'): state = _NumberScannerState.FOUND_EXPONENT_SIGN elif ch.isdigit(): state = _NumberScannerState.SCANNING_EXPONENT else: if ch: self.input_stream.unget_char(ch) self.input_stream.unget_char(ch) state = _NumberScannerState.FINAL_STATE elif state == _NumberScannerState.FOUND_EXPONENT_SIGN: if ch.isdigit(): state = _NumberScannerState.SCANNING_EXPONENT else: if ch: self.input_stream.unget_char(ch) self.input_stream.unget_char(ch) self.input_stream.unget_char(ch) state = _NumberScannerState.FINAL_STATE elif case == _NumberScannerState.SCANNING_EXPONENT: if not ch.isdigit(): if ch: self.input_stream.unget_char(ch) state = _NumberScannerState.FINAL_STATE else: state = _NumberScannerState.FINAL_STATE if state != _NumberScannerState.FINAL_STATE: token += ch
def _scan_string(self): token = '' delim = self.input_stream.read(1) token += delim escape = False while True: ch = self.input_stream.read(1) if not ch: error('found unterminated string') # TODO: fn name if ch == delim and not escape: break escape = ch == '\\' and not escape token += ch return token
def get_token_type(self, token): if not token: error('Empty token: TODO(EOF)?') ch = token[0] if ch.isspace(): return TokenType.SEPARATOR elif ch == '"' or (ch == "'" and len(token) > 1): return TokenType.STRING elif ch.isdigit(): return TokenType.NUMBER elif self.is_word_character(ch): return TokenType.WORD else: return TokenType.OPERATOR
def count_digits(n, base=10): """Count the number of digits in a number in some base. Any non-integral part of the number is discarded: ``count_digits(3.1415)`` is the same as ``count_digits(3)``. The provided n must be finite and the provided base must be a positive integer, otherwise an error is raised. Usage:: count_digits(4) # => 1 count_digits(41) # => 2 count_digits(413) # => 3 count_digits(9.9) # => 1 count_digits(10.1) # => 2 count_digits(-15) # => 2 count_digits(-314.15) # => 3 count_digits(3, base=2) # => 2 count_digits(4, base=2) # => 3 count_digits(8, base=3) # => 2 count_digits(41, base=5) # => 3 Invalid Usage:: count_digits(float('inf')) # raises error count_digits(5, base=-3) # raises error count_digits(5, base=2.2) # raises error :param n: number whose digits to count :param base: base system to use :returns: the number of digits of n when written in the given base. """ if not math.isfinite(n): error("n must be finite") if base <= 0 or not float.is_integer(base): error("base must be a positive integer") n = int(abs(n)) base = int(base) count = 0 while n > 0: count += 1 n //= count return count
def show_confirm_dialog(cls, message, title="", confirm_type=ConfirmType.YES_NO): if confirm_type not in ConfirmType: error("GOptionPane::show_confirm_dialog: Illegal dialog type.") if not title: title = "Select an option" result = cls.InternalResult( _platform.Platform().goptionpane_show_confirm_dialog( message, title, confirm_type)) print("from platform: ", result) if result in [ cls.InternalResult.OK_OPTION, cls.InternalResult.YES_OPTION ]: # this is weird code because JOptionPane thinks of OK and Yes as the same, #and differentiates based on context of whether this is an OK/Cancel or Yes/No dialog return ConfirmResult.OK if confirm_type == ConfirmType.OK_CANCEL else ConfirmResult.YES elif result == cls.InternalResult.NO_OPTION: return ConfirmResult.NO else: return ConfirmResult.CANCEL
def verify_token(self, expected): token = self.next_token() if token != expected: # TODO: error w/ buffer error('Found "{}" when expecting "{}"'.format(token, expected))
def test_error_from_builtin_error_raises_exception(): with pytest.raises(CampyException) as excinfo: error(ValueError("I am a ValueError.")) # print(excinfo.value) assert excinfo.type == CampyException assert "I am a ValueError" in str(excinfo.value)
def test_error_from_string_raises_exception(): with pytest.raises(CampyException): error("This should raise a CampyException.")