Skip to content

Commit bcd4f81

Browse files
committed
adding more docs
1 parent 55d5b83 commit bcd4f81

6 files changed

Lines changed: 125 additions & 3 deletions

File tree

devtools/ansi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def __str__(self):
130130

131131
class StylePrint:
132132
"""
133-
Annoyingly enums do not allow inheritance, this is a lazy design mistake, this is an ugly work around
133+
Annoyingly enums do not allow inheritance, a lazy design mistake, this is an ugly work around
134134
for that mistake.
135135
"""
136136
def __call__(self, input, *styles, reset=True, flush=True, file=None, **print_kwargs):

docs/examples/2_output.png

-1.94 KB
Loading

docs/examples/ansi_colours.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from devtools import sprint, sformat
2+
3+
sprint('this is red', sprint.red)
4+
5+
sprint('this is bold underlined blue on a green background. yuck',
6+
sprint.blue, sprint.bg_yellow, sprint.bold, sprint.underline)
7+
8+
v = sformat('i am dim', sprint.dim)
9+
print(repr(v))
10+
# > '\x1b[2mi am dim\x1b[0m'

docs/examples/prettier.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from devtools import PrettyFormat, pprint, pformat
2+
3+
v = {
4+
'foo': {'whatever': [3, 2, 1]},
5+
'sentence': 'hello\nworld',
6+
'generator': (i * 2 for i in [1, 2, 3]),
7+
}
8+
9+
pprint(v)
10+
# > with colours:
11+
"""
12+
{
13+
'foo': {
14+
'whatever': [3, 2, 1],
15+
},
16+
'sentence': (
17+
'hello\n'
18+
'world'
19+
),
20+
'generator': (
21+
2,
22+
4,
23+
6,
24+
),
25+
}
26+
"""
27+
28+
s = pformat(v, highlight=False)
29+
print(s)
30+
# > as above without colours, the generator will also be empty as it's already been evaluated
31+
32+
pp = PrettyFormat(
33+
indent_step=2, # default: 4
34+
indent_char='_', # default: space
35+
repr_strings=True, # default: False
36+
simple_cutoff=2, # default: 10 (if repr is below this length it'll be shown on one line)
37+
width=80, # default: 120
38+
yield_from_generators=False # default: True (whether to evaluate generators)
39+
)
40+
41+
print(pp(v))
42+
# >
43+
"""
44+
{
45+
__'foo': {
46+
____'whatever': [
47+
______'apple',
48+
______123,
49+
______45.67,
50+
____],
51+
__},
52+
__'sentence': 'hello\nworld',
53+
__'generator': <generator object <genexpr> at 0x7f448b7cebf8>,
54+
}
55+
"""

docs/examples/sitecustomize.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
...
2+
3+
try:
4+
from devtools import debug
5+
except ImportError:
6+
pass
7+
else:
8+
__builtins__['debug'] = debug

docs/index.rst

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ If ``pygments`` is installed *devtools* will colourise output to make it even mo
2121
chances are you already have pygments installed if you're using ipython, otherwise it can be installed along
2222
with *devtools* via ``pip install devtools[pygments]``.
2323

24-
debug print
24+
*python-devtools* has no requirements beyond python and optionally pygments.
25+
26+
Debug print
2527
-----------
2628

2729
Example:
@@ -40,6 +42,9 @@ you found down the back of a chair on the tube:
4042
* each argument is printed "pretty" on a new line with
4143
* if ``pygments`` is installed the output will be highlighted
4244

45+
Complex usage
46+
.............
47+
4348
a more complex example of ``debug`` shows more of what it can do.
4449

4550
.. literalinclude:: examples/2_input.py
@@ -49,9 +54,53 @@ Will output:
4954
.. image:: examples/2_output.png
5055

5156

57+
Usage without import
58+
....................
5259

53-
.. include:: ../HISTORY.rst
60+
We all know the annoyance of running code only to discover a missing import, this can be particularly
61+
frustrating when the function you're using isn't used except during development.
62+
63+
You can setup your environment to make ``debug`` available at all times by editing ``sitecustomize.py``,
64+
with ubuntu and python3.6 this file can be found at ``/usr/lib/python3.6/sitecustomize.py`` but you might
65+
need to look elsewhere depending on your OS/python version.
66+
67+
Add the following to ``sitecustomize.py``
68+
69+
.. literalinclude:: examples/sitecustomize.py
70+
71+
The ``ImportError`` exception is important since you'll want python to run fine even if *devtools* isn't installed.
72+
73+
This approach has another advantage: if you forget to remove ``debug(...)`` calls from your code, CI
74+
(which won't have devtools installed) should fail both on execution and linting, meaning you don't end up with
75+
extraneous debug calls in production code.
76+
77+
Other Tools
78+
-----------
5479

80+
Prettier print
81+
..............
82+
83+
Python comes with `pretty print <https://docs.python.org/3/library/pprint.html>`_, problem is quite often
84+
it's not that pretty, it also doesn't cope well with non standard python objects (think numpy arrays or
85+
django querysets) which have their own pretty print functionality.
86+
87+
To get round this *devtools* comes with prettier print, my take on pretty printing. You can see it in use above
88+
in ``debug()``, but you can also call it directly:
89+
90+
.. literalinclude:: examples/prettier.py
91+
92+
For more details on prettier printing, see
93+
`prettier.py <https://github.com/samuelcolvin/python-devtools/blob/master/devtools/prettier.py>`_.
94+
95+
ANSI terminal colours
96+
.....................
97+
98+
.. literalinclude:: examples/ansi_colours.py
99+
100+
For more details on ansi colours, see
101+
`ansi.py <https://github.com/samuelcolvin/python-devtools/blob/master/devtools/ansi.py>`_.
102+
103+
.. include:: ../HISTORY.rst
55104

56105
.. |pypi| image:: https://img.shields.io/pypi/v/python-devtools.svg
57106
:target: https://pypi.python.org/pypi/python-devtools

0 commit comments

Comments
 (0)