From 5e48f7f77b60846183fc5cd78462ffb3130b828e Mon Sep 17 00:00:00 2001 From: Ken Kundert Date: Thu, 9 Jan 2025 12:03:39 -0800 Subject: [PATCH] pass # modifier to f & g formats --- clean | 2 +- doc/user.rst | 16 ++++++++++++---- quantiphy/quantiphy.py | 8 ++++---- tests/test_format.py | 5 +++++ 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/clean b/clean index afa7e06..9e31384 100755 --- a/clean +++ b/clean @@ -1,7 +1,7 @@ #!/usr/bin/env sh set nonomatch -rm -rf generated_settings +rm -rf generated_settings .tox rm -rf .cache # the rest is common to all python directories diff --git a/doc/user.rst b/doc/user.rst index 7948932..18546e5 100644 --- a/doc/user.rst +++ b/doc/user.rst @@ -148,8 +148,8 @@ provides many of the most common conversions for you: 93.206 Mmiles -Specifying Quantities -..................... +Creating Quantities +................... Normally, creating a :class:`Quantity` takes one or two arguments. The first is taken to be the value, and the second, if given, is taken to be the model, which @@ -683,14 +683,22 @@ If you are interested in the units of a quantity, you can use: >>> h_line.units 'Hz' -Or you can access both the value and the units, either as a tuple or in -a string: +Or you can access both the value and the units as a tuple: .. code-block:: python >>> h_line.as_tuple() (1420405751.786, 'Hz') + +Rendering Quantities +.................... + +Rendering a quantity convert it to a string. There are many ways to do so. +Using *str* renders using default settings: + +.. code-block:: python + >>> str(h_line) '1.4204 GHz' diff --git a/quantiphy/quantiphy.py b/quantiphy/quantiphy.py index f25b182..3e251d1 100644 --- a/quantiphy/quantiphy.py +++ b/quantiphy/quantiphy.py @@ -2587,11 +2587,11 @@ def format(self, template=''): # code {{{3 match = FORMAT_SPEC.match(template) if match: - align, alt_form, width, comma, prec, ftype, units = match.groups() + align, use_alt_form, width, comma, prec, ftype, units = match.groups() scale = units if units else None prec = int(prec) if prec else None ftype = ftype if ftype else '' - alt_form = dict(strip_zeros=False, strip_radix=False) if alt_form else {} + alt_form = dict(strip_zeros=False, strip_radix=False) if use_alt_form else {} if ftype and ftype in 'dnu': if ftype == 'u': value = scale if scale else self.units @@ -2644,12 +2644,12 @@ def format(self, template=''): )) else: value = float(self) - value = '{0:{1}.{2}{3}}'.format(value, comma, prec, ftype) + value = '{0:{4}{1}.{2}{3}}'.format(value, comma, prec, ftype, use_alt_form) value = self._map_leading_sign(value) value = self._map_sign(value) width = width.lstrip('0') # format function treats 0 as a padding rather than a width - if self.strip_zeros: + if alt_form.get("strip_zeros", self.strip_zeros): if 'e' in value: mantissa, exponent = value.split('e') if '.' in mantissa: diff --git a/tests/test_format.py b/tests/test_format.py index 8ef7711..1c39f2d 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -39,6 +39,11 @@ def test_format(): assert '{:.0p}'.format(q) == '1420405752 Hz' assert '{:#.0q}'.format(q) == '1 GHz' assert '{:#.0p}'.format(q) == '1420405752. Hz' + assert '{:#.0f}'.format(q) == '1420405752.' + assert '{:#.3f}'.format(q) == '1420405751.786' + assert '{:.0g}'.format(q) == '1e+09' + assert '{:#.0g}'.format(q) == '1.e+09' + assert '{:#.3g}'.format(q) == '1.420e+09' q = Quantity('2ns') assert float(q) == 2e-9