Skip to content

Commit 887d2a3

Browse files
committed
improve examples, fix tests
1 parent 0f9f375 commit 887d2a3

9 files changed

Lines changed: 48 additions & 31 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ install-dev:
1010
install-test:
1111
pip install -U setuptools pip
1212
pip install -r tests/requirements.txt
13+
pip install -r docs/requirements.txt
1314
pip install -U .
1415

1516
.PHONY: isort

devtools/debug.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
pformat = PrettyFormat(
1919
indent_step=int(os.getenv('PY_DEVTOOLS_INDENT', 4)),
2020
simple_cutoff=int(os.getenv('PY_DEVTOOLS_SIMPLE_CUTOFF', 10)),
21-
width=int(os.getenv('PY_DEVTOOLS_SIMPLE_TERM_WIDTH', 120)),
21+
width=int(os.getenv('PY_DEVTOOLS_WIDTH', 120)),
2222
yield_from_generators=env_true('PY_DEVTOOLS_YIELD_FROM_GEN', True),
2323
)
2424

devtools/prettier.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
(frozenset, 'frozenset({', '})'),
2222
]
2323
__all__ = ['PrettyFormat', 'pformat', 'pprint']
24+
DEFAULT_WIDTH = int(os.getenv('PY_DEVTOOLS_WIDTH', 120))
2425

2526

2627
def env_true(var_name, alt=None):

docs/examples/ex1.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

docs/examples/ex2.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

docs/examples/example1.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from devtools import debug
2+
3+
v1 = {
4+
'bar': ['apple', 'banana', 'carrot', 'grapefruit'],
5+
'foo': {1: 'nested', 2: 'dict'},
6+
}
7+
8+
debug(v1, sum(range(5)))

docs/examples/example2.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import numpy as np
2+
3+
foo = {
4+
'foo': np.array(range(20)),
5+
'bar': {'apple', 'banana', 'carrot', 'grapefruit'},
6+
'spam': [{'a': i, 'b': {j for j in range(1 + i * 2)}} for i in range(3)],
7+
'gen': (i for i in ['i', 'am', 'a', 'generator']),
8+
}
9+
bar = {1: 2, 11: 12}
10+
11+
debug(foo)
12+
13+
# kwargs can be used as keys for what you are printing
14+
debug(
15+
long_string='long strings get wrapped ' * 10,
16+
new_line_sring='wraps also on newline\n' * 3,
17+
)
18+
19+
# debug can also show the output of expressions
20+
debug(
21+
len(foo),
22+
bar[1],
23+
foo == bar
24+
)

docs/gen_html.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python3.6
1+
#!/usr/bin/env python
22
import os
33
import subprocess
44
import sys
@@ -7,17 +7,19 @@
77
from ansi2html import Ansi2HTMLConverter
88

99
EX_DIR = Path(__file__).parent / 'examples'
10-
os.environ['PY_DEVTOOLS_HIGHLIGHT'] = 'true'
10+
os.environ.update(
11+
PY_DEVTOOLS_HIGHLIGHT='true',
12+
)
1113
conv = Ansi2HTMLConverter()
1214

1315

1416
def gen_html(name):
15-
p = subprocess.run((sys.executable, str(EX_DIR / f'{name}.py')), stdout=subprocess.PIPE, check=True)
17+
p = subprocess.run((sys.executable, str(EX_DIR / '{}.py'.format(name))), stdout=subprocess.PIPE, check=True)
1618
html = conv.convert(p.stdout.decode(), full=False).strip('\r\n')
1719
html = '<pre class="ansi2html-content">\n{}\n</pre>'.format(html)
18-
(EX_DIR / f'{name}.html').write_text(html)
20+
(EX_DIR / '{}.html'.format(name)).write_text(html)
1921

2022

21-
gen_html('ex1')
22-
gen_html('ex2')
23+
gen_html('example1')
24+
gen_html('example2')
2325
gen_html('prettier')

docs/index.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,36 @@ and readable way to print stuff during development. (If you know why this is, I'
3131

3232
The wait is over:
3333

34-
.. literalinclude:: examples/ex1.py
34+
.. literalinclude:: examples/example1.py
3535

3636
.. rst-class:: divider
3737

3838
🡣
3939

4040
.. raw:: html
41-
:file: ./examples/ex1.html
41+
:file: ./examples/example1.html
4242

4343
``debug`` is like ``print`` on steroids, and coffee, and some orange pills
4444
you found down the back of a chair on the tube:
4545

4646
* each output is prefixed with the file, line number and function where ``debug`` was called
4747
* the variable name or expression being printed is shown
4848
* each argument is printed "pretty" on a new line
49-
* if ``pygments`` is installed the output will be highlighted
49+
* if ``pygments`` is installed the output is highlighted
5050

5151
Complex usage
5252
.............
5353

5454
a more complex example of ``debug`` shows more of what it can do.
5555

56-
.. literalinclude:: examples/ex2.py
56+
.. literalinclude:: examples/example2.py
5757

5858
.. rst-class:: divider
5959

6060
🡣
6161

6262
.. raw:: html
63-
:file: ./examples/ex2.html
63+
:file: ./examples/example2.html
6464

6565
Usage without import
6666
....................

0 commit comments

Comments
 (0)