格式化本地人时出现KeyError [英] KeyError when formatting locals
问题描述
我面对的是KeyError
我无法解释或理解.
我有一个笔记本,其中在单元格中定义了变量PREFIX
:
I'm facing a KeyError
I can't explain or understand.
I have a notebook, in which I define a variable PREFIX
in a cell:
PREFIX = "/home/mavax/Documents/info/notebook/log_study"
这只是指向包含日志的文件夹的路径,因此使用笔记本的人只需执行以下代码即可更改路径.
which is simply a path to a folder containing logs, so people using the notebook just need to change the path if they want to execute the code below.
然后,稍后(在下面有很多单元格),我使用它,没有任何问题:
Then, later (quite a bunch of cells beneath), I use it, without any problem:
for basename in ["log_converted_full.txt", "log_converted_trimmed.txt"]:
entries = load_log_for_insertion("%(PREFIX)s/datasets/logs/%(basename)s" % locals())
pprint(entries)
然后我得到了期望的输出,这意味着找到了文件,并且正在打印日志中的(很长)输出.
I then get the output I expect, meaning files are found and the (very long) output from the logs is being printed.
我还有更多单元格描述我为解决此问题而实现的结构,当需要再次执行同一段代码时,我得到了KeyError
:
I have some more cells describing the structure I implement for this problem, and when the time comes to execute again the same piece of code, I get the KeyError
:
带来错误的代码:
def demo_synthetic_dig_dag(data_size):
for basename in ["alert_converted_trimmed.txt"]:
###
entries = load_log_for_insertion("%(PREFIX)s/datasets/logs/%(basename)s" % locals())[:data_size]
g = AugmentedDigDag()
g.build(entries)
html(
"""
<table>
<tr><td>%s</td></tr>
</table>
""" % (
synthetic_graph_to_html(g, 2, 0.03)
)
)
,然后在下一个单元格中:
and, in the next cell:
demo_synthetic_dig_dag(200)
Jupyter输出:
Jupyter output:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-179-7c2a79d0afd6> in <module>()
----> 1 demo_synthetic_dig_dag_armen(200)
<ipython-input-178-d17f57de3c01> in demo_synthetic_dig_dag(data_size)
18 for basename in ["log_converted_trimmed.txt"]:
19 ###
---> 20 entries = load_log_for_insertion("%(PREFIX)s/datasets/logs/%(basename)s" % locals())[:data_size]
21 g = AugmentedDigDag()
22 g.build(entries)
KeyError: 'PREFIX'
我敢肯定这个错误很简单而且很愚蠢,但是如果有人可以睁开眼睛,我将非常感激!
I'm pretty sure the mistake is quite simple and plain stupid, but still, if someone could open my eyes, i'd be very thankful !
推荐答案
在函数之外,locals()
与globals()
相同,因此没有问题.
Outside a function, locals()
is the same as globals()
, so you have no issue.
但是,当放置在函数内部时,locals()
不会以任何方式包含PREFIX
(存储在globals()
中,它包含该函数的本地名称.这就是为什么格式化它们会失败,正在尝试从locals()
字典返回的字典中获取名为PREFIX
的键.
When placed inside a function, though, locals()
doesn't contain PREFIX
in any way (it is stored in globals()
, it contains the local names for that function. That's why formatting these fail, it's trying to get a key named PREFIX
from the dictionary returned from the locals()
dict.
为什么不使用.format
进行格式化,而不是使用.format
:
Instead of formatting with %
, why not just use .format
:
"{}/datasets/logs/{}s".format(PREFIX, basename)
或者,您可以将PREFIX
引入本地范围,并为函数添加一个附加参数:
Alternatively, you could bring PREFIX
in the local scope with an additional parameter to your function:
def demo_synthetic_dig_dag(data_size, PREFIX=PREFIX):
但是我看不出有什么好处. (是的,本地查找的性能会有小幅度提高,但我怀疑它会起到一定作用)
but I don't see much of an upside to that. (Yes, there is a small performance boost for local look-up but I doubt it would play a role)
这篇关于格式化本地人时出现KeyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!