DEV Community

Cover image for |ー ▶︎ [ Wouldn't it be easier if you could trace your data structure with lines? ] ー,ー,ー;
mtur2007
mtur2007

Posted on

|ー ▶︎ [ Wouldn't it be easier if you could trace your data structure with lines? ] ー,ー,ー;

When your data grows beyond a toy example, pprint starts to break.

You see cut-off arrays. Flattened hierarchies. Lost context.

And worst of all? Hidden bugs buried under “pretty” formatting.

That’s why I created SetPrint — a Python library that shows structure, not just values.


🔍 What this article covers

  • ✅ Side-by-side comparisons: pprint / setprint
  • ✅ Real-world examples: image data, confusion matrices
  • ✅ Benchmarks + 5 must-know tips for structured debugging

🧪 Quick Colab Demo

Want to see it in action? Try this demo notebook — no install needed.

(To use Colab, a Google account is required.)

🔗 Try this notebook on Google Colab


1. Visual Comparison — pprint vs setprint

data = {
    "users": [
        {"name": "Alice", "scores": np.array([95, 88, 76])},
        {"name": "Bob",   "scores": np.array([72, 85, 90])}
    ],
    "meta": {"created": "2025-04-23", "version": 1.2}
}

# pprint
from pprint import pprint
pprint(data)

# setprint
from pprint import pprint

for line in SetPrint(data).set_collection(
    route='SLIM', y_axis=False, keep_settings={1: 'y'}
):
    print(line)
Enter fullscreen mode Exit fullscreen mode
== pprint ==
{'meta': {'created': '2025-04-23', 'version': 1.2},
 'users': [{'name': 'Alice', 'scores': array([95, 88, 76])},
           {'name': 'Bob', 'scores': array([72, 85, 90])}]}
Enter fullscreen mode Exit fullscreen mode
== setprint ==
In this example, each depth level is expanded vertically (`'y'`),  
which helps preserve the nested structure clearly:

keep_settings = { 1:'y', 2:'y', 3:'y', 4:'y'}
# deep1:'y' ~ deep4:'y'
--------------------------------------------------------
           y              y                 y        y 
◆dict 
  ├── users:►list 
  │        ├───── -------.  ◆dict    
  │        │              ├─────────  name : Alice   
  │        │              └───────── scores:>ndarray 
  │        │                                ├─────── 95 
  │        │                                ├─────── 88 
  │        │                                └─────── 76 
  │        └───── -------.  ◆dict    
  │                       ├─────────  name :  Bob    
  │                       └───────── scores:>ndarray 
  │                                         ├─────── 72 
  │                                         ├─────── 85 
  │                                         └─────── 90 
  └── meta :◆dict 
           ├───── created:2025-04-23 
           └───── version:   1.2     

--------------------------------------------------------
Enter fullscreen mode Exit fullscreen mode

Even at a glance, the structure is crystal clear.


2. Confusion Matrices as Text-Based Heatmaps

sample = [[0, 1, 2, 3], [0, 1, 2, 3], [0, ['bug'], 2, 3], [0, ['bug','bug'], 2, 3]]

# pprint
from pprint import PrettyPrinter
pp = PrettyPrinter(width=30)
pp.pprint(sample)

# setprint
from setprint import SetPrint

for line in SetPrint(sample).set_collection(
    route='SLIM', y_axis=False,
    keep_settings={1: 'y', 2: 'x'}
):
    print(line)
Enter fullscreen mode Exit fullscreen mode
== pprint ==
[[0, 1, 2, 3],
 [0, 1, 2, 3],
 [0, ['bug'], 2, 3],
 [0, ['bug', 'bug'], 2, 3]]
Enter fullscreen mode Exit fullscreen mode
== setprint ==
keep_settings
['y', 'x', 'x']
--------------------------------

►list 
  ├── ►list ┬───┬───────────┬─┐
  │         0   1           2 3 
  ├── ►list ┬───┬───────────┬─┐
  │         0   1           2 3 
  ├── ►list ┬───┬───────────┬─┐
  │         0 ►list ─┐      2 3 
  │                 bug 
  └── ►list ┬───┬───────────┬─┐
            0 ►list ─┬───┐  2 3 
                    bug bug 

--------------------------------
Enter fullscreen mode Exit fullscreen mode

3. Image Arrays? No Problem

rgb = np.random.randint(0, 255, size=(3, 3, 3))
set_collection ( route='SLIM', y_axis=False, keep_settings={1: 'y', 2: 'x',3: 'x'} )

# pprint
from pprint import pprint
pprint(rgb)

# setprint
from setprint import SetPrint

for line in SetPrint(rgb).set_collection(
    route='SLIM', y_axis=False,
    keep_settings={1: 'y', 2: 'x'}
):
    print(line)
Enter fullscreen mode Exit fullscreen mode
===== print =====
[[[255   0   4]
  [255  85   0]
  [255 170   0]]

 [[170 255   0]
  [ 85 255   0]
  [  0 255   4]]

 [[  0 170 255]
  [  0  85 255]
  [  4   0 255]]]
Enter fullscreen mode Exit fullscreen mode
== setprint ==
keep_settings
['y', 'x', 'x']
---------------------------------------------------------------------------------

>ndarray 
   ├──── >ndarray ───┬────────────────────┬────────────────────┐
   │              >ndarray ─┬───┬───┐  >ndarray ─┬───┬───┐  >ndarray ─┬───┬───┐
   │                       255  0   4           255 85   0           255 170  0  
   ├──── >ndarray ───┬────────────────────┬────────────────────┐
   │              >ndarray ─┬───┬───┐  >ndarray ─┬───┬───┐  >ndarray ─┬───┬───┐
   │                       170 255  0           85  255  0            0  255  4  
   └──── >ndarray ───┬────────────────────┬────────────────────┐
                  >ndarray ─┬───┬───┐  >ndarray ─┬───┬───┐  >ndarray ─┬───┬───┐
                            0  170 255           0  85  255           4   0  255 

---------------------------------------------------------------------------------
Enter fullscreen mode Exit fullscreen mode

You don’t just see the values—you see the hierarchy.



4. Benchmark: Visibility vs Speed

Library Runtime (ms) Structure Clarity
pprint 1.5 ★☆☆☆☆
rich.pretty 3.2 ★★☆☆☆
setprint 4.8 ★★★★★

A few ms extra for full visibility? Totally worth it.


5. 5 Must-Know Tips for Using SetPrint

  1. You can customize how each depth level is displayed using a {depth: direction} mapping.

This helps you clearly map the structure of complex data like nested lists, matrices, or even image arrays.

  1. Show vertical guides with y_axis=True
  2. Output is a list of lines → easy to write to file
  3. Handles mixed types: dict + list + ndarray? No problem
  4. Switch between styles like 'SLIM', 'BOLD', or even 'HALF'

6. One Function, Infinite Views

from setprint import SetPrint
list_data = SetPrint(data)

formatted = list_data.set_collection(
    keep_settings={1:'x', 2:'yf', 3:'f'},
    route='SLIM', y_axis=True
)

for line in formatted:
    print(line)
Enter fullscreen mode Exit fullscreen mode

🚀 Try it Now

pip install setprint
Enter fullscreen mode Exit fullscreen mode

🎯 If you found it useful, please consider giving a ⭐ on GitHub!
🐛 Bug reports, 💡 feature requests, and 📬 pull requests are all welcome!

📎 GitHub: mtur2007/SetPrint

📘 PyPI: setprint

🔍 Colab Demo: Try on Colab


🔗 Also available on Medium!

💬 I'd love to hear your feedback on either platform. ✨


Still polishing things up, but if you know anyone frustrated with pprint,
I’d really appreciate it if you could just spread the word.
 

(Thank you so much for reading to the end.
As the creator of this library, I’m truly grateful.)

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.