clap icon indicating copy to clipboard operation
clap copied to clipboard

string.Template identifier

Open g000001 opened this issue 13 years ago • 10 comments

$identifier names a substitution placeholder matching a mapping key of "identifier". By default, "identifier" must spell a Python identifier.

identifier form for cl

g000001 avatar Dec 05 '10 09:12 g000001

ん, けっきょくどういうことですか?

garaemon avatar Dec 06 '10 08:12 garaemon

英語で書くのかなと思って途中で挫折してましたw pythonだと $識別という構成になっている様子で、$は識別子に含まれないのでPythonでは都合が良いのですが、CLだと$も{}も普通にシンボルに使えるのでCLなりに考えた方が良いのかなという気がしました。 :foo:というような形式はどうかなと思っています。:はあまり使われないので。

g000001 avatar Dec 06 '10 10:12 g000001

pythonで$識別子を使った例ってどんな感じなのでしょうか (Python素人です)

garaemon avatar Dec 06 '10 12:12 garaemon

すいません、大本のドキュメントを示してなかったですね http://docs.python.org/release/2.7/library/string.html#template-strings ここの記述です。 $fooというのは多分シェルの記法を踏襲しているのではないかなと思います

g000001 avatar Dec 06 '10 13:12 g000001

なるほど, 理解しました

:foo: 後ろの:はいりますかね? 普通にキーワードでは微妙でしょうか?

以下の三つのパターンを考えて見ましたが, 使いやすさではキーワードが楽ですね. ただ, 健全性は微妙です

(substitute ":who likes :what" '((:who . "tim") (:what . "kung pao")))
(substitute ":who: likes :what:" '((":who:" . "tim") (":what:" . "kung pao")))
(substitute ":who: likes :what:" '((|:who:| . "tim") (|:what:| . "kung pao")))

garaemon avatar Dec 06 '10 14:12 garaemon

substituteは識別子がたりないときにエラーにするのですね

>>> d = dict(who='tim')
>>> Template('Give $who $100').substitute(d)
Traceback (most recent call last):
[...]
ValueError: Invalid placeholder in string: line 1, col 10
>>> Template('$who likes $what').substitute(d)
Traceback (most recent call last):
[...]
KeyError: 'what'

文字列中では, :foo:か|foo|でしょうか? :foo:でよいきがしてきました.

garaemon avatar Dec 06 '10 14:12 garaemon

:foo:にした理由ですが、オリジナルの場合、${foo}ooooとすることによって識別子を他とまざらないようにすることができるのですが、:foo:ooooという感じで再現できるかなというところでした。

g000001 avatar Dec 07 '10 07:12 g000001

お, なるほど. デフォルト":"で, キーワードとかで選べるようにするというのはどうでしょうか?

garaemon avatar Dec 07 '10 09:12 garaemon

なるほど、できたらやってみます

g000001 avatar Dec 08 '10 11:12 g000001

とりあえず適当なものを実装しました. $と:の問題はとりあえず何も考えずに, pythonのものを踏襲しました.

TODO: 今はdictionaryを文字列で作る仕様にしちゃってますが, シンボルでもできるようにすべき

使い方は

(clap-string:substitute
  (clap-string:make-template
  "$$hoge $hoge $a $$odiejw oiajdeoj fuga piyo ${hoge fuga} $aabbcc")
  (clap-builtin:dict '(("hoge" . "HOGE")
                      ("a" . "A")
                      ("hoge fuga" . "HOGE FUGA")
                      ("aabbcc" . "AABBCC"))
                    :test #'equal))

garaemon avatar Jan 08 '11 07:01 garaemon