pprint

Disabling sorting mechanism in pprint output

不羁岁月 提交于 2019-12-10 12:55:22
问题 I have big dictionary which I`m printing for viewing with prettyprint, but how I can keep formatting but kill sorting mechanism in pprint? 回答1: You can monkey patch the pprint module. import pprint pprint.pprint({"def":2,"ghi":3,"abc":1,}) pprint._sorted = lambda x:x # Or, for Python 3.7: # pprint.sorted = lambda x, key=None: x pprint.pprint({"def":2,"ghi":3, "abc":1}) Since the 2nd output is essentiallly randomly sorted, your output may be different from mine: {'abc': 1, 'def': 2, 'ghi': 3}

Pretty print namedtuple

孤者浪人 提交于 2019-12-10 01:04:50
问题 I tried pprint from pprint , but its output is just one line, there is no multiline output and no indentation. 回答1: I use the builtin function vars to get the namedtuple as a dictionary. However, it returns an OrderedDict which pprint won't indent, so I convert it to a dict : >>> from collections import namedtuple >>> Busbar = namedtuple('Busbar', 'id name voltage') >>> busbar = Busbar(id=102, name='FACTORY', voltage=21.8) With pprint and dict : >>> from pprint import pprint >>> pprint(dict

How to override println behavior for reference types

跟風遠走 提交于 2019-12-07 05:34:23
问题 I have a cyclic graph I created using dosync and ref-set . When I pass this to println I get a java.lang.StackOverflowError as I would expect, because it's effectively trying to print an infinitely-nested structure. I found that if I do (str my-ref) it creates something that looks like vertex@23f7d873 and doesn't actually try to traverse the structure and print everything out, so this solves the problem in the immediate sense, but only helps when I'm very careful about what I'm printing to

How can I format a map over several lines with pprint?

こ雲淡風輕ζ 提交于 2019-12-05 11:10:42
问题 pprint 's docs are kind of a brick wall. If you pprint a map, it comes out on one line like so: {:a "b", :b "c", :d "e"} . Instead, I'd like to be pprinted like this, optionally with commas: {:a "b" :b "c" :d "e"} How would one do that with pprint? 回答1: You can set the *print-right-margin* binding: Clojure=> (binding [*print-right-margin* 7] (pprint {:a 1 :b 2 :c 3})) {:a 1, :b 2, :c 3} Not exactly what you're looking for, but it might be enough? BTW, the best way to figure this out —or at

How to override println behavior for reference types

佐手、 提交于 2019-12-05 09:38:19
I have a cyclic graph I created using dosync and ref-set . When I pass this to println I get a java.lang.StackOverflowError as I would expect, because it's effectively trying to print an infinitely-nested structure. I found that if I do (str my-ref) it creates something that looks like vertex@23f7d873 and doesn't actually try to traverse the structure and print everything out, so this solves the problem in the immediate sense, but only helps when I'm very careful about what I'm printing to the screen. I'd like to be able to call (println my-graph) have it print the ref as some type of custom

Pretty print namedtuple

穿精又带淫゛_ 提交于 2019-12-04 22:25:29
I tried pprint from pprint , but its output is just one line, there is no multiline output and no indentation. I use the builtin function vars to get the namedtuple as a dictionary. However, it returns an OrderedDict which pprint won't indent, so I convert it to a dict : >>> from collections import namedtuple >>> Busbar = namedtuple('Busbar', 'id name voltage') >>> busbar = Busbar(id=102, name='FACTORY', voltage=21.8) With pprint and dict : >>> from pprint import pprint >>> pprint(dict(vars(busbar))) {'id': 102, 'name': 'FACTORY', 'voltage': 21.8} The pprint PrettyPrinter in Python 3 is much

How to use pprint to print an object using the built-in __str__(self) method?

耗尽温柔 提交于 2019-12-03 11:05:43
I have a Python script which processes a .txt file which contains report usage information. I'd like to find a way to cleanly print the attributes of an object using pprint's pprint(vars(object)) function. The script reads the file and creates instances of a Report class. Here's the class. class Report(object): def __init__(self, line, headers): self.date_added=get_column_by_header(line,headers,"Date Added") self.user=get_column_by_header(line,headers,"Login ID") self.report=get_column_by_header(line,headers,"Search/Report Description") self.price=get_column_by_header(line,headers,"Price")

pprint sorting dicts but not sets?

与世无争的帅哥 提交于 2019-12-01 14:39:44
问题 I know that dicts and sets aren't ordered, so equal sets or dicts may print differently (all tests with Python 3.6.1): >>> for obj in {0, 8}, {8, 0}, {0:0, 8:8}, {8:8, 0:0}: print(obj) {0, 8} {8, 0} {0: 0, 8: 8} {8: 8, 0: 0} And I just realized that pprint (“pretty-print”) sorts dicts but not sets: >>> for obj in {0, 8}, {8, 0}, {0:0, 8:8}, {8:8, 0:0}: pprint.pprint(obj) {0, 8} {8, 0} {0: 0, 8: 8} {0: 0, 8: 8} It's documentation also says "Dictionaries are sorted by key before the display is

pprint(): how to use double quotes to display strings?

徘徊边缘 提交于 2019-11-30 08:06:33
问题 If I print a dictionary using pprint, it always wraps strings around single quotes ( ' ): >>> from pprint import pprint >>> pprint({'AAA': 1, 'BBB': 2, 'CCC': 3}) {'AAA': 1, 'BBB': 2, 'CCC': 3} Is there any way to tell pprint to use double quotes ( " ) instead? I would like to have the following behaviour: >>> from pprint import pprint >>> pprint({'AAA': 1, 'BBB': 2, 'CCC': 3}) {"AAA": 1, "BBB": 2, "CCC": 3} 回答1: It looks like you are trying to produce JSON; if so, use the json module: >>>

pretty-printing a record using a custom method in Clojure

自古美人都是妖i 提交于 2019-11-29 10:09:58
In Clojure 1.5.0, how can I provide a custom pretty-printer for my own record type, defined with defrecord. (defrecord MyRecord [a b]) (defmethod print-method MyRecord [x ^java.io.Writer writer] (print-method (:a x) writer)) (defmethod print-dup MyRecord [x ^java.io.Writer writer] (print-dup (:a x) writer)) (println (MyRecord. 'a 'b)) ;; a -- OK (clojure.pprint/pprint (MyRecord. 'a 'b)) ;; {:a a, :b b} -- not OK, I want a I would like clojure.pprint/pprint to also use my cutsom printer (which now, should just pretty-prints whatever is in the field a of the record for illustration purposes).