Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which pretty print library? [closed]

So from a glance at hackage I can see 5 pretty printing libraries:

  • good old HughesPJ in pretty
  • wl-pprint-extras
  • wl-pprint-terminfo
  • wl-pprint
  • ansi-wl-pprint
  • wl-pprint-text

Oh wait, was that 6? 6 pretty printing libraries... no wait, we'll come in again.

Anyway, they're all Wadler-Leijen except of course HughesPJ. My understanding is that WL is simpler and faster, so is probably preferred for new code.

wl-pprint and wl-pprint-extras seem to be the same... I can't tell what's "extra" about the latter, or what "Free" means here (the module is Text.PrettyPrint.Free).

wl-pprint-terminfo and ansi-wl-pprint both seem to be variants with ANSI terminal colors and whatnot, and seem equivalent except that wl-pprint-terminfo doesn't have any docs.

wl-pprint-text, of course, uses Text. I don't know how much difference that actually makes wrt speed.

The thing that worries me about these is that many of them have many releases. This implies they've had features added, bugs fixed, etc. But have they all had the same bugs fixed? I'm inclined to favor ansi-wl-pprint because it has documentation and its last upload was in 2012, and has a bunch of releases which implies the author still works on it.

But I don't know for sure. Anyone have any advice? And I'm sure others agree that 5 almost-but-not-quite copy-paste modules could do with some consolidation...

like image 530
Evan Laforge Avatar asked Mar 18 '12 19:03

Evan Laforge


People also ask

Which module is required in pretty printing?

The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter.

How do I enable Pprint?

To use pprint, begin by importing the library at the top of your Python file. From here you can either use the . pprint() method or instantiate your own pprint object with PrettyPrinter() .

What is prettify in Python?

The pprint module in Python is a utility module that you can use to print data structures in a readable, pretty way. It's a part of the standard library that's especially useful for debugging code dealing with API requests, large JSON files, and data in general.

What is the difference between print and Pprint in Python?

The purpose is very simple, it is for printing anything in python. pprint() function also has similar functionality. But the only difference is in the way it prints complex data structures. The normal print() function prints the entire content in a single line.


1 Answers

In no particular order:

  • The "Free" in Text.PrettyPrint.Free means free monad, as per the package description: "A free monad based on the Wadler/Leijen pretty printer"; its Doc type is parametrised on another type, and it has a Monad instance, allowing you to embed "effects" into Doc values. This is used by wl-pprint-terminfo to add formatting functionality; it's not not a competing package, but rather an extension library by the same author. See the list of additions in wl-pprint-extras' documentation compared to wl-pprint's list for more detailed information on what it adds.

  • wl-pprint-terminfo uses the terminfo package to do formatting, so it'll only work on POSIX-y systems, whereas ansi-wl-pprint uses the ansi-terminal package, so it'll work on Windows.

  • wl-pprint-text might be useful if you're working with Text already, but it's unlikely to have a major performance impact unless you're using the pretty printer really heavily; it's not exactly a massively computationally-intensive task.

Unless I had specific requirements, I'd probably just use the pretty package, since it's one of the boot packages, and thus available everywhere. I'd go for ansi-wl-pprint if I wanted formatting, and wl-pprint-text if I was working with Text, but otherwise I don't really see a particularly compelling reason to use a third-party library.

like image 86
ehird Avatar answered Oct 11 '22 10:10

ehird