如何使用pprint格式化几行地图? [英] How can I format a map over several lines with pprint?

查看:142
本文介绍了如何使用pprint格式化几行地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pprint 的文档是一种砖墙。如果你打印一个地图,它会出现在一行像这样: {:ab,:bc,:de} 。相反,我想这样打印,可选择使用逗号:

  {:ab
:bc
:de}

pprint?

解决方案

您可以设置 * print-right-margin * binding:

  Clojure => (绑定[* print-right-margin * 7](pprint {:a 1:b 2:c 3}))
{:a 1,
:b 2,
:c 3}

不是你想要的,但这可能够吗?



BTW,最好的办法,至少我采取的方法是使用

  Clojure => (使用'clojure.contrib.repl-utils)
Clojure => (source pprint)
(defn pprint
漂亮的打印对象到可选输出writer。如果没有提供writer,
将对象打印到* out *的当前绑定值。
([object](pprint object * out *))
([object writer]
(with-pretty-writer writer
(binding [* print-pretty * true]
(写出对象))
(if(not(= 0(.getColumn#^ PrettyWriter * out *)))
))))
nil

Hmmrmmm .. with-pretty-writer do to * out *

 code> Clojure => (source clojure.contrib.pprint / with-pretty-writer)
(defmacro#^ {:private true} with-pretty-writer [base-writer& body]
`作者#(不是(pretty-writer?〜base-writer))]
(binding [* out *(如果new-writer#
(make-pretty-writer〜base-writer * print-right -margin * * print-miser-width *)
〜base-writer)]
〜@ body
(如果new-writer#(.flush * out *)))))
nil

好吧, * print-right-margin * / code>听起来很有前途...

  Clojure => (source clojure.contrib.pprint / make-pretty-writer)
(defn- make-pretty-writer
在具有指定右边距和缩放宽度的PrettyWriter中包装基本作者
[base-writer right-margin miser-width]
(PrettyWriter。base-writer right-margin miser-width))
nil
pre>

此外,这是非常信息:

  Clojure => ; (doc * print-right-margin *)
-------------------------
clojure.contrib.pprint / * print-right-margin *
nil
漂亮的打印将尽量避免超出这一列。
将其设置为nil以使pprint允许线任意长。这将忽略所有
非强制性换行符。
nil

无论如何,也许你已经知道,如果你真的想定制 pprint 的工作方式,您可以代理 clojure.contrib.pprint.PrettyWriter 并通过绑定到 * out * 传递。 PrettyWriter类是相当大和恐吓,所以我不知道这是否是你原来的意思是你的砖墙评论。


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?

解决方案

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 least the approach I took— is to use

Clojure=> (use 'clojure.contrib.repl-utils)
Clojure=> (source pprint)
(defn pprint 
  "Pretty print object to the optional output writer. If the writer is not provided, 
print the object to the currently bound value of *out*."
  ([object] (pprint object *out*)) 
  ([object writer]
     (with-pretty-writer writer
       (binding [*print-pretty* true]
         (write-out object))
       (if (not (= 0 (.getColumn #^PrettyWriter *out*)))
         (.write *out* (int \newline))))))
nil

Hmmrmmm.. what does with-pretty-writer do to *out*?

Clojure=> (source clojure.contrib.pprint/with-pretty-writer)
(defmacro #^{:private true} with-pretty-writer [base-writer & body]
  `(let [new-writer# (not (pretty-writer? ~base-writer))]
     (binding [*out* (if new-writer#
                      (make-pretty-writer ~base-writer *print-right-margin* *print-miser-width*)
                      ~base-writer)]
       ~@body
       (if new-writer# (.flush *out*)))))
nil

Okay, so *print-right-margin* sounds promising...

Clojure=> (source clojure.contrib.pprint/make-pretty-writer)
(defn- make-pretty-writer 
  "Wrap base-writer in a PrettyWriter with the specified right-margin and miser-width"
  [base-writer right-margin miser-width]
  (PrettyWriter. base-writer right-margin miser-width))
nil

Also, this is pretty informative:

Clojure=> (doc *print-right-margin*)
-------------------------
clojure.contrib.pprint/*print-right-margin*
nil
  Pretty printing will try to avoid anything going beyond this column.
Set it to nil to have pprint let the line be arbitrarily long. This will ignore all 
non-mandatory newlines.
nil

Anyway —and perhaps you already knew even this— if you really want to customize the way that pprint works, you can proxy clojure.contrib.pprint.PrettyWriter and pass that down by binding it to *out*. The PrettyWriter class is pretty large and intimidating, so I'm not sure if this was what you originally meant by your "brick wall" comment.

这篇关于如何使用pprint格式化几行地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆