-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tossi.format 을 이용해 문자열에 조사를 포맷할수 있습니다. ex) tossi.format('{0:을}', '나오') == '나오를'
- Loading branch information
1 parent
26c3b36
commit 49666ea
Showing
3 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
tossi.formatter | ||
~~~~~~~~~~~~~~~ | ||
String formatter for Tossi. | ||
:copyright: (c) 2016-2017 by What! Studio | ||
:license: BSD, see LICENSE for more details. | ||
""" | ||
import re | ||
from string import Formatter as StringFormatter | ||
|
||
|
||
class Formatter(StringFormatter): | ||
"""String formatter supports tossi format spec. | ||
>>> f = Formatter(tossi.registry) | ||
>>> t = u'{0:으로} {0:을}' | ||
>>> assert f.format(t, u'나오') == u'나오로 나오를' | ||
>>> assert f.format(t, u'키홀') == u'키홀로 키홀을' | ||
>>> assert f.format(t, u'모리안') == u'모리안으로 모리안을' | ||
""" | ||
hangul_pattern = re.compile(u'[가-힣]+') | ||
|
||
def __init__(self, registry): | ||
self.registry = registry | ||
|
||
def format_field(self, value, format_spec): | ||
if re.match(self.hangul_pattern, format_spec): | ||
return self.registry.postfix(value, format_spec) | ||
else: | ||
return super(Formatter, self).format_field(value, format_spec) |