@@ -452,18 +452,15 @@ conversion is :index:`not linear <dB>`:
452452
453453.. code-block :: python
454454
455- >> > def from_dB (value , units = ' ' ):
455+ >> > def from_dB (value ):
456456 ... return 10 ** (value/ 20 ), value.units[2 :]
457457
458458 >> > Quantity(' -100 dBV' , scale = from_dB)
459459 Quantity(' 10 uV' )
460460
461- .. note ::
462-
463- Since version 2.18 the first argument, in this case *value *, is guaranteed to
464- be a :class: `Quantity ` that contains both the units and any parameters needed
465- during the conversion. As such, the second argument, *units *, is not longer
466- needed and will eventually be removed.
461+ The argument to the conversion function, in this case *value *, is guaranteed to
462+ be a :class: `Quantity ` that contains both the units and any parameters needed
463+ during the conversion.
467464
468465The conversion can also often occur if you simply state the units you wish the
469466quantity to have:
@@ -833,8 +830,35 @@ in grams and wanting to present it in either kilograms or in pounds:
833830 >> > print (" mass (kg): {} " .format(m.render(show_units = False , scale = 0.001 )))
834831 mass (kg): 2.529
835832
836- >> > print (m.render(scale = (0.0022046 , ' lb' ), form = ' fixed' ))
837- 5.5754 lb
833+ Alternatively, you can use either the built-in converters or the converters you
834+ created to do the conversion simply based on the units:
835+
836+ .. code-block :: python
837+
838+ >> > print (m.render(scale = ' lb' ))
839+ 5.5755 lb
840+
841+ You can also give the desired scale factor as part of the units:
842+
843+ .. code-block :: python
844+
845+ >> > m = Quantity(' 2529 g' )
846+ >> > print (" mass (kg): {} " .format(m.render(show_units = False , scale = ' kg' )))
847+ mass (kg): 2.529
848+
849+ This also works with the binary scale factors:
850+
851+ .. code-block :: python
852+
853+ >> > rates = [155.52e6 , 622.08e6 , 2.48832e9 , 9.95328e9 , 39.81312e9 ]
854+ >> > rates = [Quantity(r, ' b' ) for r in rates]
855+ >> > for r in rates:
856+ ... print (f " { r:> 14 ,.2pMiB} " )
857+ 18.54 MiB
858+ 74.16 MiB
859+ 296.63 MiB
860+ 1 ,186.52 MiB
861+ 4 ,746.09 MiB
838862
839863 As before, functions can also be used to do the conversion. Here is an example
840864where that comes in handy: a logarithmic conversion to :index: `dBV <dB> ` is
@@ -843,27 +867,16 @@ performed.
843867.. code-block :: python
844868
845869 >> > import math
846- >> > def to_dB (value , units = ' ' ):
870+ >> > def to_dB (value ):
847871 ... return 20 * math.log10(value), ' dB' + value.units
848872
849873 >> > T = Quantity(' 100mV' )
850874 >> > print (T.render(scale = to_dB))
851875 - 20 dBV
852876
853- .. note ::
854-
855- Since version 2.18 the first argument, in this case *value *, is guaranteed to
856- be a :class: `Quantity ` that contains both the units and any parameters needed
857- during the conversion. As such, the second argument, *units *, is not longer
858- needed and will eventually be removed.
859-
860- Finally, you can also use either the built-in converters or the converters you
861- created to do the conversion simply based on the units:
862-
863- .. code-block :: python
864-
865- >> > print (m.render(scale = ' lb' ))
866- 5.5755 lb
877+ The argument to the conversion function, in this case *value *, is guaranteed to
878+ be a :class: `Quantity ` that contains both the units and any parameters needed
879+ during the conversion.
867880
868881In an earlier example the units of time and temperature data were converted to
869882normal SI units. Presumably this makes processing easier. Now, when producing
@@ -1208,11 +1221,11 @@ accordingly:
12081221
12091222 >> > for p in range (1 , 5 ):
12101223 ... bytes = Quantity(256 ** p, ' B' )
1211- ... print (f " An { 8 * p} bit bus addresses { bytes :,pkB} . " )
1212- An 8 bit bus addresses 0.256 kB.
1213- An 16 bit bus addresses 65.536 kB.
1214- An 24 bit bus addresses 16 ,777.216 kB.
1215- An 32 bit bus addresses 4 ,294 ,967.296 kB.
1224+ ... print (f " An { 8 * p} bit bus addresses { bytes :,pkB } ( { bytes :,pKiB } ) ." )
1225+ An 8 bit bus addresses 0.256 kB ( 0.25 KiB) .
1226+ An 16 bit bus addresses 65.536 kB ( 64 KiB) .
1227+ An 24 bit bus addresses 16 ,777.216 kB ( 16 , 384 KiB) .
1228+ An 32 bit bus addresses 4 ,294 ,967.296 kB ( 4 , 194 , 304 KiB) .
12161229
12171230 Generally you should only specify base units when using a format that renders
12181231with scale factors as otherwise you could see two scale factors on the same
@@ -1637,13 +1650,14 @@ One way around this is to add another conversion specifically for differences:
16371650 >> > print (Quantity(' 100 Δ°C' , scale = ' Δ°F' ))
16381651 180 Δ°F
16391652
1640- Notice that the scaling functions used here differ from those described
1641- previously in that they only take one argument and return one value. The units
1642- are not included in either then argument list or the return value.
1653+ Notice that the scaling functions used here differ slightly from those described
1654+ previously in that they only return one value. The units are not included in
1655+ the return value. You can use the previously described scaling functions that
1656+ do return units here, but the units will be ignored.
16431657
1644- Also notice that the return value of *UnitConversion * was not used in the
1645- examples above. It is enough to simply create the *UnitConversion * for it to be
1646- available to *Quantity *. So, it is normal to not capture the return value of
1658+ The return value of *UnitConversion * was not used in the examples above. It is
1659+ enough to simply create the *UnitConversion * for it to be available to
1660+ *Quantity *. So, it is normal to not capture the return value of
16471661*UnitConversion *. However, there are a few things you can do with the return
16481662value. First you can convert it to a string to get a description of the
16491663relationship. This is largely used as a sanity check:
@@ -1721,8 +1735,8 @@ conversion. For example:
17211735
17221736 For more information on defining unit converters, see :class: `UnitConversion `.
17231737For more information on parametrized unit converters, see
1724- :meth: `UnitConversion.fixture `. For example of real-time dynamic conversions,
1725- see :ref: `quantiphy bitcoin example `.
1738+ :meth: `UnitConversion.fixture `. For an example of real-time dynamic
1739+ conversions, see :ref: `quantiphy bitcoin example `.
17261740
17271741
17281742.. index ::
@@ -1739,10 +1753,10 @@ doing so the relationship between the units must be known, and
17391753:class: `UnitConversion ` is used to specify the relationship. However, it is
17401754also possible to perform simple scale factor conversions without changing the
17411755units. This case is specified in a manner similar to a unit conversion, but in
1742- this case both the from-units and the to-units are the same, and it is not
1743- necessary to define a :class: `UnitConversion `. For example, imagine printing
1744- a table of bit-rates where the rates are held in bps but are expected to be
1745- displayed in Mbps:
1756+ this case both the from-units and the to-units are the same or are equivalent,
1757+ and it is not necessary to define a :class: `UnitConversion `. For example,
1758+ imagine printing a table of bit-rates where the rates are held in bps but are
1759+ expected to be displayed in Mbps:
17461760
17471761.. code-block :: python
17481762
0 commit comments