File: README.rst

package info (click to toggle)
python-colored-traceback 0.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 148 kB
  • sloc: python: 96; makefile: 2
file content (117 lines) | stat: -rw-r--r-- 3,741 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
Colored Traceback
=================

Automatically color Python's uncaught exception tracebacks.

This one's for anybody who's ever struggled to read python's stacktraces on the
terminal. Something about the two-lines-per-frame approach really just makes
them tough to scan visually.

Compare this:

::

    Traceback (most recent call last):
      File "./workflowy.py", line 525, in <module>
        main()
      File "./workflowy.py", line 37, in main
        projects = cli.load_json(args, input_is_pipe)
      File "./workflowy.py", line 153, in load_json
        return json.load(sys.stdin)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 290, in load
        **kw)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
        return _default_decoder.decode(s)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 383, in raw_decode
        raise ValueError("No JSON object could be decoded")
    ValueError: No JSON object could be decoded

To this:

.. code-block:: python

    Traceback (most recent call last):
      File "./workflowy.py", line 525, in <module>
        main()
      File "./workflowy.py", line 37, in main
        projects = cli.load_json(args, input_is_pipe)
      File "./workflowy.py", line 153, in load_json
        return json.load(sys.stdin)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 290, in load
        **kw)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
        return _default_decoder.decode(s)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 383, in raw_decode
        raise ValueError("No JSON object could be decoded")
    ValueError: No JSON object could be decoded

Installation
------------

Through pip:

.. code-block:: bash

    pip install colored-traceback

Or directly:

.. code-block:: bash

    git clone http://www.github.com/staticshock/colored-traceback.py
    cd colored-traceback.py
    pip install .

On Windows, which has no real support for ANSI escape sequences, this will
also install `colorama`.

Usage
-----

Colored Traceback can be executed as a module:

.. code-block:: bash

    python -m colored_traceback somefile.py

Colored Traceback also works well within a script or even directly in the
interpreter REPL. Standard usage will color the output, unless it's being
redirected to a pipe:

.. code-block:: python

    import colored_traceback
    colored_traceback.add_hook()

If want to retain color even when stderr is being piped, tack on an
`always=True` argument:

.. code-block:: python

    import colored_traceback
    colored_traceback.add_hook(always=True)

There are also a couple of convenience imports, which get the footprint down to
one line:

.. code-block:: python

    # Same as add_hook()
    import colored_traceback.auto

    # Same as add_hook(always=True)
    import colored_traceback.always

It goes without saying that you might want to catch `ImportError`, making the
presence of the package optional:

.. code-block:: python

    try:
        import colored_traceback.auto
    except ImportError:
        pass