KeyError when formatting locals

本小妞迷上赌 提交于 2021-02-08 11:55:52

问题


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.

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:

Code bringing the error:

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 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 !


回答1:


Outside a function, locals() is the same as globals(), so you have no issue.

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.

Instead of formatting with %, why not just use .format:

"{}/datasets/logs/{}s".format(PREFIX, basename)

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)



来源:https://stackoverflow.com/questions/44884857/keyerror-when-formatting-locals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!