www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - 'pp' for D?

reply "linkrope" <linkrope github.com> writes:
I want to pretty-print the representation of a value of a generic 
type T.
In Ruby, I would use 'pp':

     value = 'hello'
     pp value  # prints "hello" - with quotes!
     value = 42
     pp value  # prints 42

Now, value.to!string eliminates the quotes, should value be of 
type string.
As a workaround, I put the value into an array to make use of the 
"undocumented" function formatElement:

     "%(%s%)".format([value])

Ugly! Where does Phobos hide the function I'm looking for?
Sep 29 2013
next sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Sunday, 29 September 2013 at 14:31:15 UTC, linkrope wrote:
 As a workaround, I put the value into an array to make use of 
 the "undocumented" function formatElement:

     "%(%s%)".format([value])
That seems excessive. What happened to format(`"%s"`, s) or format("\"%s\"", s) or text('"', s, '"')?
 Where does Phobos hide the function I'm looking for?
I don't have a solution that's "convenient" enough to satisfy you.
Sep 29 2013
parent reply "linkrope" <linkrope github.com> writes:
On Sunday, 29 September 2013 at 18:14:03 UTC, monarch_dodra wrote:
 On Sunday, 29 September 2013 at 14:31:15 UTC, linkrope wrote:
 As a workaround, I put the value into an array to make use of 
 the "undocumented" function formatElement:

    "%(%s%)".format([value])
That seems excessive. What happened to format(`"%s"`, s) or format("\"%s\"", s) or text('"', s, '"')?
Of course, this works well when I know that the value is of type string. But I have a template and I want to print the representation of (T value): "hello" (with quotes) or just 42 (without) Phobos does this for array elements, but it seems that there's nothing like 'repr'?
Sep 29 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 09/29/2013 03:38 PM, linkrope wrote:
 On Sunday, 29 September 2013 at 18:14:03 UTC, monarch_dodra wrote:
 On Sunday, 29 September 2013 at 14:31:15 UTC, linkrope wrote:
 As a workaround, I put the value into an array to make use of the
 "undocumented" function formatElement:

    "%(%s%)".format([value])
That seems excessive. What happened to format(`"%s"`, s) or format("\"%s\"", s) or text('"', s, '"')?
Of course, this works well when I know that the value is of type string. But I have a template and I want to print the representation of (T value): "hello" (with quotes) or just 42 (without) Phobos does this for array elements, but it seems that there's nothing like 'repr'?
I don't know a Phobos function either but the following should work: import std.stdio; import std.traits; void pp(T)(File output, T value) { static if (isSomeString!T) { output.writef(`"%s"`, value); } else { output.write(value); } } void pp(T)(T value) { pp(stdout, value); } void main() { pp("hello"); pp(42); } Ali
Sep 29 2013
next sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Monday, 30 September 2013 at 04:20:32 UTC, Ali Çehreli wrote:
 On 09/29/2013 03:38 PM, linkrope wrote:
 On Sunday, 29 September 2013 at 18:14:03 UTC, monarch_dodra 
 wrote:
 On Sunday, 29 September 2013 at 14:31:15 UTC, linkrope wrote:
 As a workaround, I put the value into an array to make use 
 of the
 "undocumented" function formatElement:

   "%(%s%)".format([value])
That seems excessive. What happened to format(`"%s"`, s) or format("\"%s\"", s) or text('"', s, '"')?
Of course, this works well when I know that the value is of type string. But I have a template and I want to print the representation of (T value): "hello" (with quotes) or just 42 (without) Phobos does this for array elements, but it seems that there's nothing like 'repr'?
I don't know a Phobos function either but the following should work: [SNIP] static if (isSomeString!T) {
This should also take into acount enums, as they get their own printing. It should either use std.conv's private "isExactSomeString", or use: static if (isSomeString!T && !is(T == enum)
Sep 29 2013
prev sibling parent reply "linkrope" <linkrope github.com> writes:
On Monday, 30 September 2013 at 04:20:32 UTC, Ali Çehreli wrote:
 I don't know a Phobos function either but the following should 
 work:

 import std.stdio;
 import std.traits;

 void pp(T)(File output, T value)
 {
     static if (isSomeString!T) {
         output.writef(`"%s"`, value);

     } else {
         output.write(value);
     }
 }

 void pp(T)(T value)
 {
     pp(stdout, value);
 }

 void main()
 {
     pp("hello");
     pp(42);
 }

 Ali
OK. But putting quotes around a string value is obviously not enough. What if the string contains a quote? "hell\"o" would become `"hell"o"`! Seems, I have the choice between: string repr(T)(T value) { auto writer = appender!string(); auto fmt = FormatSpec!char("%s"); formatElement(writer, value, fmt); return writer.data; } (relying on the "undocumented" formatElement), or the aforementioned array detour: string repr(T)(T value) { return "%(%s%)".format([value]); }
Sep 30 2013
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Monday, 30 September 2013 at 21:24:28 UTC, linkrope wrote:
 But putting quotes around a string value is obviously not 
 enough.
 What if the string contains a quote? "hell\"o" would become 
 `"hell"o"`!
Would would you want it be become?
Oct 01 2013
next sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Tuesday, 1 October 2013 at 09:21:44 UTC, John Colvin wrote:
 On Monday, 30 September 2013 at 21:24:28 UTC, linkrope wrote:
 But putting quotes around a string value is obviously not 
 enough.
 What if the string contains a quote? "hell\"o" would become 
 `"hell"o"`!
Would would you want it be become?
Don't understand. Did you mean "Would would you want it be become"? I think he meant that he *wanted* it to become "hell\"o". I said: "why don't you just print `"%s"`, to which he retorted by saying it would choke on his input of `hell"o`, which would print: "hell"o" Which would be wrong.
Oct 01 2013
prev sibling parent "qznc" <qznc web.de> writes:
On Tuesday, 1 October 2013 at 09:21:44 UTC, John Colvin wrote:
 On Monday, 30 September 2013 at 21:24:28 UTC, linkrope wrote:
 But putting quotes around a string value is obviously not 
 enough.
 What if the string contains a quote? "hell\"o" would become 
 `"hell"o"`!
Would would you want it be become?
I believe the basic idea of repr and friends is to output language literals. You could copy&paste the output back into a source file and use it.
Oct 01 2013
prev sibling next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
linkrope:

 Where does Phobos hide the function I'm looking for?
Surely Phobos should add a prettyPrinting() function, like the function of Python standard library. Bye, bearophile
Sep 30 2013
parent reply Jacob Carlborg <doob me.com> writes:
On 2013-09-30 23:56, bearophile wrote:

 Surely Phobos should add a prettyPrinting() function, like the function
 of Python standard library.
I would rather have function that generates a pretty representation of a given value. Then it either can be used to print the representation or something else. -- /Jacob Carlborg
Oct 01 2013
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Jacob Carlborg:

 I would rather have function that generates a pretty 
 representation of a given value. Then it either can be used to 
 print the representation or something else.
That's the point of the Python pprint() (pretty print) function :-) Bye, bearophile
Oct 01 2013
prev sibling parent reply "linkrope" <linkrope github.com> writes:
On Tuesday, 1 October 2013 at 07:30:06 UTC, Jacob Carlborg wrote:
 On 2013-09-30 23:56, bearophile wrote:

 Surely Phobos should add a prettyPrinting() function, like the 
 function
 of Python standard library.
I would rather have function that generates a pretty representation of a given value. Then it either can be used to print the representation or something else.
How about "%r" in 'format'? Then for arrays, "%(%s, %)" would in fact be "%(%r, %)" while "%-(%s, %)" would in fact be "%(%s, %)" - breaking existing code; punishable by 'xxformat' for at least one year :-)
Oct 01 2013
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Tuesday, 1 October 2013 at 19:47:16 UTC, linkrope wrote:
 On Tuesday, 1 October 2013 at 07:30:06 UTC, Jacob Carlborg 
 wrote:
 On 2013-09-30 23:56, bearophile wrote:

 Surely Phobos should add a prettyPrinting() function, like 
 the function
 of Python standard library.
I would rather have function that generates a pretty representation of a given value. Then it either can be used to print the representation or something else.
How about "%r" in 'format'? Then for arrays, "%(%s, %)" would in fact be "%(%r, %)" while "%-(%s, %)" would in fact be "%(%s, %)" - breaking existing code; punishable by 'xxformat' for at least one year :-)
%r is already taken: It means "raw". It's used as a way to use formatting, even when writing in a binary file. You can even use "%+r" and "%-r" to specify the endian-ness you want to write in. It's fun. Makes writing file headers *real* easy.
Oct 01 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
monarch_dodra:

 %r is already taken: It means "raw". It's used as a way to use 
 formatting, even when writing in a binary file. You can even 
 use "%+r" and "%-r" to specify the endian-ness you want to 
 write in. It's fun. Makes writing file headers *real* easy.
I didn't know that -.- Bye, bearophile
Oct 01 2013
prev sibling next sibling parent "Daniel Davidson" <nospam spam.com> writes:
On Sunday, 29 September 2013 at 14:31:15 UTC, linkrope wrote:
 I want to pretty-print the representation of a value of a 
 generic type T.
 In Ruby, I would use 'pp':

     value = 'hello'
     pp value  # prints "hello" - with quotes!
     value = 42
     pp value  # prints 42

 Now, value.to!string eliminates the quotes, should value be of 
 type string.
 As a workaround, I put the value into an array to make use of 
 the "undocumented" function formatElement:

     "%(%s%)".format([value])

 Ugly! Where does Phobos hide the function I'm looking for?
I have one at: https://github.com/patefacio/d-help/blob/master/d-help/pprint/pp.d The following code outputs the text below: import std.stdio; import pprint.pp; enum Color { Red, White, Blue } struct R { int x = 22; string s = "foobar"; } struct S { int i; R r; } struct T { int []i; string []j; } void main() { auto s = S(3); auto t = T([1,2,3], ["a", "b", "c"]); writeln(pp(Color.Red)); writeln(pp(42)); writeln(pp("hello")); writeln(pp(s)); writeln(pp(t)); } Outputs Red 42 "hello" { (S).i = 3 (S).r = { (R).x = 22 (R).s = "foobar" } } { (T).i = [ [0]->1 [1]->2 [2]->3 ] (T).j = [ [0]->"a" [1]->"b" [2]->"c" ] }
Sep 30 2013
prev sibling parent Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
On Sun, 29 Sep 2013 16:31:14 +0200
"linkrope" <linkrope github.com> wrote:

 I want to pretty-print the representation of a value of a generic 
 type T.
 In Ruby, I would use 'pp':
 
      value = 'hello'
      pp value  # prints "hello" - with quotes!
      value = 42
      pp value  # prints 42
 
 Now, value.to!string eliminates the quotes, should value be of 
 type string.
 As a workaround, I put the value into an array to make use of the 
 "undocumented" function formatElement:
 
      "%(%s%)".format([value])
 
 Ugly! Where does Phobos hide the function I'm looking for?
How 'bout either of these?: void pp(T)(T value) { writefln("%(%s%)", [value]); } string prettyVal(T)(T value) { return "%(%s%)".format([value]); } pp(...whatever...); string s = prettyVal(...whatever...);
Oct 01 2013