Skip to content

Commit f83c539

Browse files
Ken KundertKen Kundert
authored andcommitted
document bar class
1 parent 71dcfaa commit f83c539

File tree

3 files changed

+98
-59
lines changed

3 files changed

+98
-59
lines changed

doc/user.rst

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,52 @@ Several utility functions are provided for your convenience. They are often
10941094
helpful when creating messages.
10951095

10961096

1097+
.. _bar desc:
1098+
1099+
bar
1100+
"""
1101+
1102+
.. py:class:: bar(normalized_value, width=72)
1103+
:noindex:
1104+
1105+
:class:`bar` produces a graphic representation of a normalized value in the form
1106+
of a bar. *normalized_value* is the value to render; it is expected to be
1107+
a value between 0 and 1. *width* specifies the maximum width of the bar in
1108+
characters.
1109+
1110+
.. code-block:: python
1111+
1112+
>>> from inform import bar, display
1113+
1114+
>>> for i in range(10):
1115+
... value = 1 - i/9.02
1116+
... display(f'{value:0.3f}: {bar(value, 70)}')
1117+
1.000: ██████████████████████████████████████████████████████████████████████
1118+
0.889: ██████████████████████████████████████████████████████████████▏
1119+
0.778: ██████████████████████████████████████████████████████▍
1120+
0.667: ██████████████████████████████████████████████▋
1121+
0.557: ██████████████████████████████████████▉
1122+
0.446: ███████████████████████████████▏
1123+
0.335: ███████████████████████▍
1124+
0.224: ███████████████▋
1125+
0.113: ███████▉
1126+
0.002: ▏
1127+
1128+
You can specify a format string when interpolating the bar into a string to
1129+
control its width, whether the bar should be padded with terminal spaces, and
1130+
how to handle overflows. For example:
1131+
1132+
.. code-block:: python
1133+
1134+
>>> assets = {'property': 13_194, 'cash': 2846, 'equities': 19_301}
1135+
>>> total = sum(assets.values())
1136+
>>> for key, value in assets.items():
1137+
... display(f"{key:>8}: ❭{bar(value/total):60P}")
1138+
property: ❭██████████████████████▍ ❬
1139+
cash: ❭████▊ ❬
1140+
equities: ❭████████████████████████████████▊ ❬
1141+
1142+
10971143
.. _color desc:
10981144

10991145
Color Class
@@ -2060,40 +2106,11 @@ render_bar
20602106
.. py:function:: render_bar(normalized_value, width=72)
20612107
:noindex:
20622108
2063-
:func:`render_bar()` produces a graphic representation of a normalized value in
2064-
the form of a bar. *normalized_value* is the value to render; it is expected to
2065-
be a value between 0 and 1. *width* specifies the maximum width of the line in
2066-
characters.
2109+
This function is deprecated. You should instead use:
20672110
20682111
.. code-block:: python
20692112
2070-
>>> from inform import render_bar, display
2071-
>>> for i in range(10):
2072-
... value = 1 - i/9.02
2073-
... display('{:0.3f}: {}'.format(value, render_bar(value, 70)))
2074-
1.000: ██████████████████████████████████████████████████████████████████████
2075-
0.889: ██████████████████████████████████████████████████████████████▏
2076-
0.778: ██████████████████████████████████████████████████████▍
2077-
0.667: ██████████████████████████████████████████████▋
2078-
0.557: ██████████████████████████████████████▉
2079-
0.446: ███████████████████████████████▏
2080-
0.335: ███████████████████████▍
2081-
0.224: ███████████████▋
2082-
0.113: ███████▉
2083-
0.002: ▏
2084-
2085-
If you would like to add delimiters to the bar, you can make each bar fixed
2086-
width by adding ``fullwidth=True``:
2087-
2088-
.. code-block:: python
2089-
2090-
>>> assets = {'property': 13_194, 'cash': 2846, 'equities': 19_301}
2091-
>>> total = sum(assets.values())
2092-
>>> for key, value in assets.items():
2093-
... display(f"{key:>8}: ❭{render_bar(value/total, full_width=True)}❬")
2094-
property: ❭██████████████████████████▉ ❬
2095-
cash: ❭█████▊ ❬
2096-
equities: ❭███████████████████████████████████████▎ ❬
2113+
bar(normalized_value, width).render()
20972114
20982115
20992116
.. _title_case desc:

inform/inform.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,7 @@ class bar:
17201720
17211721
width (int): The width of the bar in characters when value is 1.
17221722
1723-
full_width (bool):
1723+
pad (bool):
17241724
Whether bar should be rendered to fill the whole width using
17251725
trailing spaces. This is useful if anything follows the bar on its
17261726
line, such as if you wish to mark the end of the bar.
@@ -1736,15 +1736,15 @@ class bar:
17361736
Common values are '➔', '∎', '►', '▞', '▋▍▎▏' or '>>>'.
17371737
17381738
When rendered within a string you can specify a format that overrides the
1739-
above arguments. The format strings take the form *WFCO* where:
1739+
above arguments. The format strings take the form *WPCO* where:
17401740
17411741
- *W* is an integer that overrides *width*.
1742-
- *F* is either 'f' or 'F' overrides *full_width*; is true if capitalized.
1742+
- *P* is either 'p' or 'P' overrides *pad*; is true if capitalized.
17431743
- *C* is a simple real number, 1 or greater.
17441744
- *O* is an arbitrary string that becomes the overflow marker.
17451745
1746-
The format fields are optional, but if one is given, the one listed before
1747-
it must also be given.
1746+
The format fields are optional, but if you want to specify *W* and *C* you
1747+
also need to also specify *P* to separate them.
17481748
17491749
**Examples**::
17501750
@@ -1753,7 +1753,7 @@ class bar:
17531753
>>> assets = {'property': 13_194, 'cash': 2846, 'equities': 19_301}
17541754
>>> total = sum(assets.values())
17551755
>>> for key, value in assets.items():
1756-
... display(f"{key:>8}: ❭{bar(value/total):60F}❬")
1756+
... display(f"{key:>8}: ❭{bar(value/total):60P}❬")
17571757
property: ❭██████████████████████▍ ❬
17581758
cash: ❭████▊ ❬
17591759
equities: ❭████████████████████████████████▊ ❬
@@ -1763,32 +1763,32 @@ class bar:
17631763
maximum life the overflow marker is added to the end of the bar, which adds
17641764
a few vertical bars, a newline, and 20 spaced of indent.
17651765
1766-
>>> hours = dict(drill01=6, drill02=34, drill03=89, drill04=57)
1766+
>>> hours = dict(unit_1=6, unit_2=34, unit_3=89, unit_4=57)
17671767
>>> max_life = 40
17681768
>>> for name, life in hours.items():
1769-
... print(f"{bar(life/max_life):20F2▋▍▎▏\n }", name)
1770-
███ drill01
1771-
█████████████████ drill02
1769+
... print(f"{bar(life/max_life):20P2▋▍▎▏\n }", name)
1770+
███ unit_1
1771+
█████████████████ unit_2
17721772
████████████████████████████████████████▋▍▎▏
1773-
drill03
1774-
████████████████████████████▌ drill04
1773+
unit_3
1774+
████████████████████████████▌ unit_4
17751775
17761776
"""
1777-
def __init__(self, value, width=72, full_width=False, clip=1, overflow=False):
1777+
def __init__(self, value, width=72, pad=False, clip=1, overflow=False):
17781778
self.value = value
17791779
self.width = width
1780-
self.full_width = full_width
1780+
self.pad = pad
17811781
self.clip = clip
17821782
self.overflow = overflow or ''
17831783

1784-
def render(self, value=None, width=None, full_width=None, clip=None, overflow=None):
1784+
def render(self, value=None, width=None, pad=None, clip=None, overflow=None):
17851785
"""Render bar to string
17861786
17871787
Arguments given override those specified when class was instantiated.
17881788
"""
17891789
value = self.value if value is None else value
17901790
width = self.width if width is None else width
1791-
full_width = self.full_width if full_width is None else full_width
1791+
pad = self.pad if pad is None else pad
17921792
clip = self.clip if clip is None else clip
17931793
overflow = self.overflow if overflow is None else overflow
17941794

@@ -1800,31 +1800,31 @@ def render(self, value=None, width=None, full_width=None, clip=None, overflow=No
18001800
else:
18011801
last = BAR_CHARS[frac-1:frac]
18021802
bar = buckets*BAR_CHARS[-1] + last
1803-
if full_width:
1803+
if pad:
18041804
bar += (width - len(bar))*' '
18051805
return bar
18061806

18071807
def __format__(self, formatter):
1808-
# format strings take the form WFCO where:
1808+
# format strings take the form WPCO where:
18091809
# W is an integer indicating desired width
1810-
# F is either 'f' or 'F' for full_width, cap is true
1810+
# P is either 'p' or 'P' for pad, cap is true
18111811
# C is a simple real number, 1 or greater.
18121812
# O is an arbitrary string that will be used as overflow marker
1813-
value = width = full_width = clip = overflow = None
1813+
value = width = pad = clip = overflow = None
18141814
if formatter:
1815-
match = re.match(r'(\d*)([fF]?)(\d\.?\d*)?(.*)\Z', formatter, re.S)
1815+
match = re.match(r'(\d*)([pP]?)(\d\.?\d*)?(.*)\Z', formatter, re.S)
18161816
try:
18171817
if match[1]:
18181818
width = int(match[1])
1819-
if 'f' in match[2]:
1820-
full_width = False
1821-
if 'F' in match[2]:
1822-
full_width = True
1819+
if 'p' in match[2]:
1820+
pad = False
1821+
if 'P' in match[2]:
1822+
pad = True
18231823
clip = float(match[3]) if match[3] else None
18241824
overflow = match[4] or None
18251825
except (TypeError, ValueError):
18261826
warn('invalid format string')
1827-
return self.render(value, width, full_width, clip, overflow)
1827+
return self.render(value, width, pad, clip, overflow)
18281828

18291829
def __str__(self):
18301830
return self.render()

inform/inform.pyi

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Callable, Iterable, Mapping, Sequence, TextIO
1+
from typing import Any, Callable, Iterable, Literal, Mapping, Sequence, TextIO
22
from pathlib import Path
33

44
__released__: str
@@ -25,6 +25,27 @@ def is_collection(obj: Any) -> bool: ...
2525
def is_mapping(obj: Any) -> bool: ...
2626
def is_array(obj: Any) -> bool: ...
2727

28+
class bar:
29+
def __init__(
30+
self,
31+
value: float,
32+
width: int,
33+
pad: bool,
34+
clip: float,
35+
overflow: Literal[False] | str,
36+
) -> None:
37+
...
38+
39+
def render(
40+
self,
41+
value: float,
42+
width: int,
43+
pad: bool,
44+
clip: float,
45+
overflow: Literal[False] | str,
46+
) -> str:
47+
...
48+
2849
class Color:
2950
enable: bool
3051

@@ -169,7 +190,8 @@ def columns(
169190
def render_bar(
170191
value: float,
171192
width: int = ...,
172-
full_width: bool = ...
193+
full_width: bool = ...,
194+
clip: float = ...
173195
):
174196
...
175197

0 commit comments

Comments
 (0)