0

Here is the error my python script is reporting:

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:

>>> 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. 1. Why? 2. Why the difference between the two?

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)
5
  • 1
    Did you perhaps change the code and forget to reload it in the interpreter? Try restarting ipython. Commented Jul 16, 2013 at 16:04
  • I restarted, same result. Commented Jul 16, 2013 at 16:07
  • I note that you set namespaces[ns] = True earlier in your code. That doesn't jive with the rest of your namespaces code. Commented Jul 16, 2013 at 16:10
  • Wait, are you running that debug statement the first iteration, or after the exception has occurred? Commented Jul 16, 2013 at 16:11
  • Was during first iteration, looks like when I modified namespaces I forgot to change the way I set the value. Thanks! Commented Jul 16, 2013 at 17:03

1 Answer 1

2

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).

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; 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Martijn, much appreciated. Also thanks for pointing out that optimization , +1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.