简体   繁体   English

ipdb输出和Python解释器之间的差异

[英]Discrepency between ipdb output and Python interpreter

Here is the error my python script is reporting: 这是我的python脚本报告的错误:

TypeError                                 Traceback (most recent call last)
/home/jhourani/openbel-contributions/resource_generator/change_log.py in <module>()
     37         for k, v in namespaces.items():
     38             #ipdb.set_trace()

---> 39             if v[0]:
     40                 v[1].append(token)
     41 

TypeError: 'bool' object is not subscriptable

Ok, thats all well and good I guess. 好吧,我猜这一切都很好。 But when I examine this element further in ipdb , this is the result: 但是当我在ipdb进一步检查这个元素时,结果如下:

>>> v
(False, [])
>>> type(v)
<class 'tuple'>
>>> v[0]
False
>>> if v[0]:
...     print('true')
... else:
...     print('false')
... 
false
>>> 

The conditional test works in ipdb , but when I run the script the interpreter seems to be treating v as a boolean, not as a tuple which is of course subscriptable. 条件测试在ipdb ,但是当我运行脚本时,解释器似乎将v视为布尔值,而不是作为可订阅的元组。 1. Why? 1.为什么? 2. Why the difference between the two? 2.为什么两者有所不同?

Here is the block of code I have written: 这是我写的代码块:

old_entrez = []
old_hgnc = []
old_mgi = []
old_rgd = []
old_sp = []
old_affy = []
# iterate over the urls to the .belns files
for url in parser.parse():
    namespaces = { 'entrez' : (False, old_entrez), 'hgnc' : (False, old_hgnc),
                   'mgi' : (False, old_mgi), 'rgd' : (False, old_rgd),
                   'swissprot' : (False, old_sp), 'affy' : (False, old_affy) }
    open_url = urllib.request.urlopen(url)
    for ns in namespaces.keys():
        if ns in open_url.url:
            namespaces[ns] = True
    marker = False
    for u in open_url:
        # skip all lines from [Values] up
        if '[Values]' in str(u):
            marker = True
            continue
        if marker is False:
            continue
        # we are into namespace pairs with '|' delimiter
        tokenized = str(u).split('|')
        token = tokenized[0]
        for k, v in namespaces.items():
            ipdb.set_trace()
            if v[0]:
                v[1].append(token)

You are examining the first iteration , which works fine. 您正在检查第一次迭代 ,它工作正常。

The exception occurs later on. 稍后会发生异常。 Step through the loop some more, because at some point you'll run into a namespace key for which the value has been set to True ( not a tuple of a boolean and a list). 再循环一遍循环,因为在某些时候你会遇到一个名称空间键,其值已设置为True不是布尔值和列表的元组)。

Why? 为什么? Because earlier in your code you do: 因为在您的代码中,您可以:

for ns in namespaces.keys():
    if ns in open_url.url:
        namespaces[ns] = True

Note the = True there; 注意那里= True ; you perhaps meant to set that to: 你或许打算将其设置为:

namespaces[ns] = (True, namespaces[ns][1])

Note that to loop over the keys of a dictionary, you can do so directly: 请注意,要遍历字典的键,您可以直接执行此操作:

for ns in namespaces:

and save yourself an attribute lookup, a function call, and the creation of a whole new list object. 并保存自己的属性查找,函数调用和创建一个全新的列表对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM