pretty-print

Prettyprint to a file?

荒凉一梦 提交于 2019-12-03 16:03:19
问题 I'm using this gist's tree, and now I'm trying to figure out how to prettyprint to a file. Any tips? 回答1: What you need is Pretty Print pprint module: from pprint import pprint # Build the tree somehow with open('output.txt', 'wt') as out: pprint(myTree, stream=out) 回答2: If I understand correctly, you just need to provide the file to the stream keyword on pprint: with open(outputfilename,'w') as fout: pprint(tree,stream=fout,**other_kwargs) 来源: https://stackoverflow.com/questions/17280534

Pretty Printing Syntax Tree with Operator Precedence and Associativity in Haskell

青春壹個敷衍的年華 提交于 2019-12-03 14:47:05
问题 Is there any commonly used method or even a library for pretty printing (and parsing) a syntax tree with (binary) operators that have an associativity and a precedence level assigned, so that the result uses as few brackets as possible? Take the formulas of propositional calculus as an example: data Formula = Atom String | Not (Formula) | And (Formula) (Formula) | Or (Formula) (Formula) | Imp (Formula) (Formula) Assume that the precedence is Imp < Or < And < Not (so Not binds the most) and

format xml, pretty print

谁说胖子不能爱 提交于 2019-12-03 14:13:27
I know of two ways to "pretty print", or format, xml: shell tools Hack 38 Pretty-Print XML Using a Generic Identity Stylesheet and Xalan what other free (as in beer) formatters are there? (aside from using javascript) Well, the identity transform you linked to is portable to any XSLT processor (Saxon, msxml, etc). Additionally, you could look at xmllint which is part of the LibXML2 toolkit. The --format option allows you to pretty print the input. Similar functionality exists in XMLStarlet (which uses LibXML2 under the hood iirc). xmlstarlet fo is what I use for pretty printing. Xmlstarlet has

How do I pretty-print productions and line numbers, using ANTLR4?

纵饮孤独 提交于 2019-12-03 12:53:38
I'm trying to write a piece of code that will take an ANTLR4 parser and use it to generate ASTs for inputs similar to the ones given by the -tree option on grun ( misc.TestRig ). However, I'd additionally like for the output to include all the line number/offset information. For example, instead of printing (add (int 5) '+' (int 6)) I'd like to get (add (int 5 [line 3, offset 6:7]) '+' (int 6 [line 3, offset 8:9]) [line 3, offset 5:10]) Or something similar. There aren't a tremendous number of visitor examples for ANTLR4 yet, but I am pretty sure I can do most of this by copying the default

Does jQuery have a JSON/javascript object to HTML pretty print function similar to PHP's var_dump?

杀马特。学长 韩版系。学妹 提交于 2019-12-03 11:29:30
问题 Does jQuery have a JSON/Javascript object to HTML pretty print function similar to PHP's var_dump? If yes, what is it? 回答1: jQuery does not (out of the box). However, James Padolsey created this prettyPrint which I really like. Also, if you're using Firebug or Web Inspector (or similar), you can just type the object into the console, press return , and see a tree-dump of the object. To force a tree-view, call console.dir(obj) 回答2: Although the accepted answer is correct that jQuery does not

Getting a python traceback without an exception

好久不见. 提交于 2019-12-03 11:21:51
Suppose you have these modules: module1.py import module2 def a(): module1.b() def c(): print "Hi guys!" module2.py import module1 def b(): module1.c() I want a function func(a()) that produces a similar output to this: (=a traceback ?) /usr/local/lib/python2.7/dist-packages/test/module1.py 3 def a(): 4 module1.b() 1 import module1 /usr/local/lib/python2.7/dist-packages/test/module2.py 3 def b(): 4 module1.c() 1 import module2 /usr/local/lib/python2.7/dist-packages/test/module1.py 6 def c(): 7 print "Hi guys!" It might be possible with the standard modules traceback and/or cgitb and/or inspect

Pretty Printing AST with Minimal Parentheses

落花浮王杯 提交于 2019-12-03 09:34:44
问题 I'm implementing a pretty-printer for a JavaScript AST and I wanted to ask if someone is aware of a "proper" algorithm to automatically parenthesize expressions with minimal parentheses based on operator precedence and associativity. I haven't found any useful material on the google. What seems obvious is that an operator whose parent has a higher precedence should be parenthesized, e.g.: (x + y) * z // x + y has lower precedence However, there are also some operators which are not

Custom pretty printer using Jackson library

こ雲淡風輕ζ 提交于 2019-12-03 09:32:53
问题 I have data in a Map object and I want to print it in a json format. I tried using DefaultPrettyPrinter mapper.writerWithDefaultPrettyPrinter().writeValue(filePath, mapObject); but the format is not what I expected. I am getting output like this: { "arrVals" : ["value-1","value-2"] } I want output like this: { "arrVals" : [ "value-1", "value-2" ] } 回答1: You need indentation before Array Values. You can use indentArraysWith method to set the Lf2SpacesIdenter object which will basically add a

Java: Writing a DOM to an XML file (formatting issues)

时光总嘲笑我的痴心妄想 提交于 2019-12-03 06:42:30
问题 I'm using org.w3c XML API to open an existing XML file. I'm removing some nodes , and I'm adding others instead. The problem is that the new nodes that are added are written one after the other, with no newline and no indentation what so ever. While it's true that the XML file is valid , it is very hard for a human to examine it. Is there anyway to add indentation , or at least a newline after each node? 回答1: I'm assuming that you're using a Transformer to do the actual writing (to a

Freemarker print date in template

折月煮酒 提交于 2019-12-03 06:30:32
I am trying to print the current date when the template is activated. I have read that I have to pass a new Date() Java object to the template, but I don't know how to do that or where to put it in the code. Does someone know how to pass a Java object to the template in this case? Thank you !! Actually you don't have to pass a new Date() to your template, because placing a timestamp into a template's output is quite common and therefore FreeMarker provides a special variable called .now which returns the current date and time. You can use it in your template like this: Page generated: ${.now}