jinja2 rendering issue: AttributeError: 'unicode' object has no attribute '__call__'
jinja2 rendering issue: AttributeError: 'unicode' object has no attribute '__call__'
我正在尝试执行以下 filtered transaction report from the piecash project:
from __future__ import print_function
import datetime
import re
import os.path
from piecash import open_book
if __name__=='__main__':
this_folder = os.path.dirname(os.path.realpath(__file__))
s = open_book(os.path.join(this_folder, "teste.gnucash"), open_if_lock=True)
else:
s = open_book(os.path.join("teste.gnucash"), open_if_lock=True)
# get default currency
print(s.default_currency)
regex_filter = re.compile(u"^/Ativos/Dinheiro/Carteira")
# retrieve relevant transactions
transactions = [tr for tr in s.transactions # query all transactions in the book/session and filter them on
if (regex_filter.search(tr.description) # description field matching regex
or any(regex_filter.search(spl.memo) for spl in tr.splits)) # or memo field of any split of transaction
and tr.post_date.date() >= datetime.date(2016, 03, 1)] # and with post_date no later than begin nov.
try:
import jinja2
except ImportError:
print("\n\t*** Install jinja2 ('pip install jinja2') to test the jinja2 template version ***\n")
jinja2 = None
if jinja2:
env = jinja2.Environment(trim_blocks=True, lstrip_blocks=True)
print(env.from_string("""
Here are the transactions for the search criteria '{{regex.pattern}}':
{% for tr in transactions %}
- {{ tr.post_date.strftime("%Y/%m/%d") }} : {{ tr.description }}
{% for spl in tr.splits %}
{{ spl.value.__abs__() }} {% if spl.value < 0 %} --> {% else %} <-- {% endif %} {{ spl.account.fullname() }} : {{ spl.memo }}
{% endfor %}
{% endfor %}
""").render(transactions=transactions,regex=regex_filter))
然而,当我尝试呈现 jinja2 模板时,在代码的最后一行出现以下错误:
AttributeError: 'unicode' object has no attribute 'call'
我发现 this question 的最后一个答案(来自@brianz)阐明了这个问题。 transactions
是一个unicode字符串列表,而regex_filter
是一个编译好的regex正则表达式对象,由regex.pattern
中的模板从中提取模式。
我试过直接通过regex.pattern
没有成功,试图按照jinja2 api render的例子。
关于我到底缺少什么的任何想法?
.format()
的最后一行有很多地方出错了,特别是我注意到:
{{ spl.account.fullname() }}
这似乎是一种可以作为属性或方法的名称,因此 并且您尝试调用的实际上似乎是 unicode。谜团解开了 ;)
我正在尝试执行以下 filtered transaction report from the piecash project:
from __future__ import print_function
import datetime
import re
import os.path
from piecash import open_book
if __name__=='__main__':
this_folder = os.path.dirname(os.path.realpath(__file__))
s = open_book(os.path.join(this_folder, "teste.gnucash"), open_if_lock=True)
else:
s = open_book(os.path.join("teste.gnucash"), open_if_lock=True)
# get default currency
print(s.default_currency)
regex_filter = re.compile(u"^/Ativos/Dinheiro/Carteira")
# retrieve relevant transactions
transactions = [tr for tr in s.transactions # query all transactions in the book/session and filter them on
if (regex_filter.search(tr.description) # description field matching regex
or any(regex_filter.search(spl.memo) for spl in tr.splits)) # or memo field of any split of transaction
and tr.post_date.date() >= datetime.date(2016, 03, 1)] # and with post_date no later than begin nov.
try:
import jinja2
except ImportError:
print("\n\t*** Install jinja2 ('pip install jinja2') to test the jinja2 template version ***\n")
jinja2 = None
if jinja2:
env = jinja2.Environment(trim_blocks=True, lstrip_blocks=True)
print(env.from_string("""
Here are the transactions for the search criteria '{{regex.pattern}}':
{% for tr in transactions %}
- {{ tr.post_date.strftime("%Y/%m/%d") }} : {{ tr.description }}
{% for spl in tr.splits %}
{{ spl.value.__abs__() }} {% if spl.value < 0 %} --> {% else %} <-- {% endif %} {{ spl.account.fullname() }} : {{ spl.memo }}
{% endfor %}
{% endfor %}
""").render(transactions=transactions,regex=regex_filter))
然而,当我尝试呈现 jinja2 模板时,在代码的最后一行出现以下错误:
AttributeError: 'unicode' object has no attribute 'call'
我发现 this question 的最后一个答案(来自@brianz)阐明了这个问题。 transactions
是一个unicode字符串列表,而regex_filter
是一个编译好的regex正则表达式对象,由regex.pattern
中的模板从中提取模式。
我试过直接通过regex.pattern
没有成功,试图按照jinja2 api render的例子。
关于我到底缺少什么的任何想法?
.format()
的最后一行有很多地方出错了,特别是我注意到:
{{ spl.account.fullname() }}
这似乎是一种可以作为属性或方法的名称,因此