Reputation: 3542
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)
Upvotes: 0
Views: 128
Reputation: 1121266
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.
Upvotes: 2