我们从Python开源项目中,提取了以下31个代码示例,用于说明如何使用locale.format_string()。
def science_time(time_in_seconds: float, decimals=2, append_seconds=True, remove_spaces=False) -> str: if time_in_seconds < 1e-6: suffix = "n" value = time_in_seconds * 1e9 elif time_in_seconds < 1e-3: suffix = "µ" value = time_in_seconds * 1e6 elif time_in_seconds < 1: suffix = "m" value = time_in_seconds * 1e3 else: suffix = "" value = time_in_seconds result = locale.format_string("%.{0}f ".format(decimals) + suffix, value) if append_seconds: result += "s" if remove_spaces: result = result.replace(" ", "") return result
def format_string(self, format, val, grouping=False): """ Format a string in the current numeric locale. See python's locale.format_string for details. ICU's message formatting codes are incompatible with locale's, so just use locale.format_string for now. """ return locale.format_string(format, val, grouping)
def _test_format_string(self, format, value, out, **format_opts): self._test_formatfunc(format, value, out, func=locale.format_string, **format_opts)
def test_percent_escape(self): self.assertEqual(locale.format_string('%f%%', 1.0), '%f%%' % 1.0) self.assertEqual(locale.format_string('%d %f%%d', (1, 1.0)), '%d %f%%d' % (1, 1.0)) self.assertEqual(locale.format_string('%(foo)s %%d', {'foo': 'bar'}), ('%(foo)s %%d' % {'foo': 'bar'}))
def test_mapping(self): self.assertEqual(locale.format_string('%(foo)s bing.', {'foo': 'bar'}), ('%(foo)s bing.' % {'foo': 'bar'})) self.assertEqual(locale.format_string('%(foo)s', {'foo': 'bar'}), ('%(foo)s' % {'foo': 'bar'}))
def big_value_with_suffix(value: float, decimals=3) -> str: fmt_str = "%.{0:d}f".format(decimals) if abs(value) >= 1e9: return locale.format_string(fmt_str+"G", value / 1e9) elif abs(value) >= 1e6: return locale.format_string(fmt_str+"M", value / 1e6) elif abs(value) >= 1e3: return locale.format_string(fmt_str+"k", value / 1e3) else: return locale.format_string(fmt_str, value)
def refresh_estimated_time(self): c = self.table_model.protocol if c.num_messages == 0: self.ui.lEstimatedTime.setText("Estimated Time: ") return avg_msg_len = numpy.mean([len(msg.encoded_bits) for msg in c.messages]) avg_bit_len = numpy.mean([m.samples_per_bit for m in self.modulators]) avg_sample_rate = numpy.mean([m.sample_rate for m in self.modulators]) pause_samples = sum(c.pauses) nsamples = c.num_messages * avg_msg_len * avg_bit_len + pause_samples self.ui.lEstimatedTime.setText( locale.format_string("Estimated Time: %.04f seconds", nsamples / avg_sample_rate))
def __init__(self, signal, parent=None): super().__init__(parent) self.signal = signal self.ui = Ui_SignalDetails() self.ui.setupUi(self) self.setAttribute(Qt.WA_DeleteOnClose) file = self.signal.filename self.ui.lblName.setText(self.signal.name) if os.path.isfile(file): self.ui.lblFile.setText(file) self.ui.lblFileSize.setText(locale.format_string("%.2fMB", os.path.getsize(file) / (1024 ** 2))) self.ui.lFileCreated.setText(time.ctime(os.path.getctime(file))) else: self.ui.lblFile.setText(self.tr("signal file not found")) self.ui.lblFileSize.setText("-") self.ui.lFileCreated.setText("-") self.ui.lblSamplesTotal.setText("{0:n}".format(self.signal.num_samples).replace(",", " ")) self.ui.dsb_sample_rate.setValue(self.signal.sample_rate) self.set_duration() self.ui.dsb_sample_rate.valueChanged.connect(self.on_dsb_sample_rate_value_changed) try: self.restoreGeometry(constants.SETTINGS.value("{}/geometry".format(self.__class__.__name__))) except TypeError: pass
def get_value_with_suffix(value, unit=""): decimal_point = locale.localeconv()["decimal_point"] if abs(value) >= 10 ** 9: target_val, suffix = value / 10 ** 9, "G" elif abs(value) >= 10 ** 6: target_val, suffix = value / 10 ** 6, "M" elif abs(value) >= 10 ** 3: target_val, suffix = value / 10 ** 3, "k" else: target_val, suffix = value, "" return locale.format_string("%.3f", target_val).rstrip("0").rstrip(decimal_point) + suffix + unit
def update_view(self): try: self.ui.sliderYscale.setValue(int(self.graphics_view.transform().m22())) except AttributeError: return txt = self.ui.txtEditErrors.toPlainText() new_messages = self.device.read_messages() if "No devices found for" in new_messages: self.device.stop_on_error("Could not establish connection to USRP") Errors.usrp_found() self.on_clear_clicked() elif any(e in new_messages for e in ("HACKRF_ERROR_NOT_FOUND", "HACKRF_ERROR_LIBUSB")): self.device.stop_on_error("Could not establish connection to HackRF") Errors.hackrf_not_found() self.on_clear_clicked() elif "No module named gnuradio" in new_messages: self.device.stop_on_error("Did not find gnuradio.") Errors.gnuradio_not_installed() self.on_clear_clicked() elif "RTLSDR-open: Error Code: -1" in new_messages: self.device.stop_on_error("Could not open a RTL-SDR device.") self.on_clear_clicked() elif "Address already in use" in new_messages: self._restart_device_thread() if len(new_messages) > 1: self.ui.txtEditErrors.setPlainText(txt + new_messages) self.ui.lSamplesCaptured.setText("{0:n}".format(self.device.current_index)) self.ui.lSignalSize.setText(locale.format_string("%.2f", (8 * self.device.current_index) / (1024 ** 2))) self.ui.lTime.setText(locale.format_string("%.2f", self.device.current_index / self.device.sample_rate)) if self.is_rx and self.device.data is not None and len(self.device.data) > 0: self.ui.labelReceiveBufferFull.setText("{0}%".format(int(100 * self.device.current_index / len(self.device.data)))) if self.device.current_index == 0: return False return True