-
Notifications
You must be signed in to change notification settings - Fork 468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
string formatting guide #1375
Merged
Merged
string formatting guide #1375
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
6175d5f
first draft of the format docs
keewis ba75cb9
fix the table
keewis b8dbc4e
add the ipython dependency
keewis f73d9c7
enable napoleon type preprocessing
keewis ede160a
document the quantity modifier
keewis 4b3bd49
add quantity format spec examples
keewis b273758
move the unit format spec description into its own section
keewis 2ed6ef6
describe the structure of quantity formats
keewis 3f6771f
extend the intersphinx settings
keewis 61a4976
wording
keewis e623e7e
update CHANGES
keewis ab73bab
Merge branch 'master' into format-docs
keewis 3eb1bd8
rewrite the unit format docs
keewis db2edea
rewrite the quantity format docs
keewis 8e460e6
typo
keewis 74113be
add more details
keewis ae92396
don't specify the language
keewis bd3abff
upgrade sphinx
keewis 151a7e2
convert back to a literal block
keewis 7a7d729
another attempt
keewis 0d3e0ed
move to an extra block
keewis 00c5350
try code-block
keewis 1e86f0f
try to set the language to none
keewis 85be4e3
Merge branch 'master' into format-docs
keewis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
.. _formatting: | ||
.. currentmodule:: pint | ||
|
||
|
||
.. ipython:: python | ||
:suppress: | ||
|
||
import pint | ||
|
||
|
||
String formatting | ||
================= | ||
The conversion of :py:class:`Unit` and :py:class:`Quantity` objects to strings (e.g. | ||
through the :py:class:`str` builtin or f-strings) can be customized using :ref:`format | ||
specifications <formatspec>`. The basic format is: | ||
|
||
.. code-block:: none | ||
|
||
[magnitude format][modifier][unit format] | ||
|
||
where each part is optional and the order of these is arbitrary. | ||
|
||
In case any part (except the modifier) is omitted, the corresponding value in | ||
:py:attr:`Quantity.default_format` or :py:attr:`Unit.default_format` is filled in. If | ||
that is not set (it evaluates to ``False``), :py:attr:`UnitRegistry.default_format` is | ||
used. If both are not set, the global default of ``"D"`` and the magnitude's default | ||
format are used instead. | ||
|
||
.. note:: | ||
|
||
Modifiers may be used without specifying any format: ``"~"`` is a valid format | ||
specification. | ||
|
||
|
||
Unit Format Specifications | ||
-------------------------- | ||
The :py:class:`Unit` class ignores the magnitude format part, and the unit format | ||
consists of just the format type. | ||
|
||
Let's look at some examples: | ||
|
||
.. ipython:: python | ||
|
||
ureg = pint.UnitRegistry() | ||
u = ureg.kg * ureg.m / ureg.s ** 2 | ||
|
||
f"{u:P}" # using the pretty format | ||
f"{u:~P}" # short pretty | ||
f"{u:P~}" # also short pretty | ||
|
||
# default format | ||
u.default_format | ||
ureg.default_format | ||
str(u) # default: default | ||
f"{u:~}" # default: short default | ||
ureg.default_format = "C" # registry default to compact | ||
str(u) # default: compact | ||
f"{u}" # default: compact | ||
u.default_format = "P" | ||
f"{u}" # default: pretty | ||
u.default_format = "" # TODO: switch to None | ||
ureg.default_format = "" # TODO: switch to None | ||
Comment on lines
+61
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this PR is merged before #1371, I can do that there |
||
f"{u}" # default: default | ||
|
||
Unit Format Types | ||
----------------- | ||
``pint`` comes with a variety of unit formats: | ||
|
||
======= =============== ====================================================================== | ||
Spec Name Example | ||
======= =============== ====================================================================== | ||
``D`` default ``kilogram * meter / second ** 2`` | ||
``P`` pretty ``kilogram·meter/second²`` | ||
``H`` HTML ``kilogram meter/second<sup>2</sup>`` | ||
``L`` latex ``\frac{\mathrm{kilogram} \cdot \mathrm{meter}}{\mathrm{second}^{2}}`` | ||
``Lx`` latex siunitx ``\si[]{\kilo\gram\meter\per\second\squared}`` | ||
``C`` compact ``kilogram*meter/second**2`` | ||
======= =============== ====================================================================== | ||
|
||
Quantity Format Specifications | ||
------------------------------ | ||
The magnitude format is forwarded to the magnitude (for a unit-spec of ``H`` the | ||
magnitude's ``_repr_html_`` is called). | ||
|
||
Let's look at some more examples: | ||
|
||
.. ipython:: python | ||
|
||
q = 1e-6 * u | ||
|
||
# modifiers | ||
f"{q:~P}" # short pretty | ||
f"{q:~#P}" # compact short pretty | ||
f"{q:P#~}" # also compact short pretty | ||
|
||
# additional magnitude format | ||
f"{q:.2f~#P}" # short compact pretty with 2 float digits | ||
f"{q:#~}" # short compact default | ||
|
||
Quantity Format Types | ||
--------------------- | ||
There are no special quantity formats yet. | ||
|
||
Modifiers | ||
--------- | ||
======== =================================================== ================================ | ||
Modifier Meaning Example | ||
======== =================================================== ================================ | ||
``~`` Use the unit's symbol instead of its canonical name ``kg·m/s²`` (``f"{u:~P}"``) | ||
``#`` Call :py:meth:`Quantity.to_compact` first ``1.0 m·mg/s²`` (``f"{q:#~P}"``) | ||
======== =================================================== ================================ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,6 +120,7 @@ User Guide | |
getting | ||
tutorial | ||
defining-quantities | ||
formatting | ||
numpy | ||
nonmult | ||
log_units | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do like the
ipython
sphinx extension a lot (I think it's cleaner than thedoctest
directive). If desired, I can also open a new PR converting our current use of thedoctest
directive (and code blocks, where appropriate) to that, which might allow us to usepytest
to run the doctests in the docstrings.