Python Iterating Through Dictionary With Tuples - python

I am designing a system that involves iterating through a pre-made non python standard dictionary with an iter function. I am trying to iterate through the tuples but keep getting this stupid Error. Also, this error only happens half the time, due to a bit flipping function that messes with inputs
TypeError: 'tuple' object cannot be interpreted as an integer
Probably an easy fix, can anyone spot?
Heres relevant code:
In my main program:
for tup in crusherdict.CrusherDict(db2, fields[0]):
log.write("VOTE\t{}\t{}\n".format(tup[0][0], tup[0][1]))
Place in Dictionary giving me the iter error:
def __iter__(self):
for i in range(self.__len__()):
yield self.db.fetch(entryName(self.name,i))
Heres db.fetch:
def fetch(self,key):
return self.cache[key]
And entryName:
def entryName(dict, str):
return (dict, "E", str)
Full BackTrace:
in <module>
if commands[line[0]](db, tempDict, logFile, line):
in cast
return inq(db, tempDict, logFile, ("INQ", tempDict["voterID"]))
line 100, in inq
for tup in crusherdict.CrusherDict(db3, fields[0]):
crusherdict.py", line 91, in __iter__
for i in range(self.__len__()):
TypeError: 'tuple' object cannot be interpreted as an integer
>>>

The traceback says the error happens at:
for i in range(self.__len__()):
We know that range expects an integer so this is hopeful, and indeed we can replicate the error message in Python 3:
>>> range((1,2))
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'tuple' object cannot be interpreted as an integer
Therefore __len__ is not returning an integer as it should.

Your function entryName() returns a tuple of three elements. This tuple is used by fetch as an index into your cache. Since we have to guess from the bits you provide, I'm guessing your cache is a plain list, and needs an integer index, not a tuple. (Also you're passing an int to an argument you named str, so you're definitely confused about something here.)

Related

NoneType Type Error - locating href in a tags [duplicate]

What does TypeError: 'NoneType' object is not iterable mean? Example:
for row in data: # Gives TypeError!
print(row)
It means the value of data is None.
Explanation of error: 'NoneType' object is not iterable
In python2, NoneType is the type of None. In Python3 NoneType is the class of None, for example:
>>> print(type(None)) #Python2
<type 'NoneType'> #In Python2 the type of None is the 'NoneType' type.
>>> print(type(None)) #Python3
<class 'NoneType'> #In Python3, the type of None is the 'NoneType' class.
Iterating over a variable that has value None fails:
for a in None:
print("k") #TypeError: 'NoneType' object is not iterable
Python methods return NoneType if they don't return a value:
def foo():
print("k")
a, b = foo() #TypeError: 'NoneType' object is not iterable
You need to check your looping constructs for NoneType like this:
a = None
print(a is None) #prints True
print(a is not None) #prints False
print(a == None) #prints True
print(a != None) #prints False
print(isinstance(a, object)) #prints True
print(isinstance(a, str)) #prints False
Guido says only use is to check for None because is is more robust to identity checking. Don't use equality operations because those can spit bubble-up implementationitis of their own. Python's Coding Style Guidelines - PEP-008
NoneTypes are Sneaky, and can sneak in from lambdas:
import sys
b = lambda x : sys.stdout.write("k")
for a in b(10):
pass #TypeError: 'NoneType' object is not iterable
NoneType is not a valid keyword:
a = NoneType #NameError: name 'NoneType' is not defined
Concatenation of None and a string:
bar = "something"
foo = None
print foo + bar #TypeError: cannot concatenate 'str' and 'NoneType' objects
What's going on here?
Python's interpreter converted your code to pyc bytecode. The Python virtual machine processed the bytecode, it encountered a looping construct which said iterate over a variable containing None. The operation was performed by invoking the __iter__ method on the None.
None has no __iter__ method defined, so Python's virtual machine tells you what it sees: that NoneType has no __iter__ method.
This is why Python's duck-typing ideology is considered bad. The programmer does something completely reasonable with a variable and at runtime it gets contaminated by None, the python virtual machine attempts to soldier on, and pukes up a bunch of unrelated nonsense all over the carpet.
Java or C++ doesn't have these problems because such a program wouldn't be allowed to compile since you haven't defined what to do when None occurs. Python gives the programmer lots of rope to hang himself by allowing you to do lots of things that should cannot be expected to work under exceptional circumstances. Python is a yes-man, saying yes-sir when it out to be stopping you from harming yourself, like Java and C++ does.
Code: for row in data:
Error message: TypeError: 'NoneType' object is not iterable
Which object is it complaining about? Choice of two, row and data.
In for row in data, which needs to be iterable? Only data.
What's the problem with data? Its type is NoneType. Only None has type NoneType. So data is None.
You can verify this in an IDE, or by inserting e.g. print "data is", repr(data) before the for statement, and re-running.
Think about what you need to do next:
How should "no data" be represented? Do we write an empty file? Do we raise an exception or log a warning or keep silent?
Another thing that can produce this error is when you are setting something equal to the return from a function, but forgot to actually return anything.
Example:
def foo(dict_of_dicts):
for key, row in dict_of_dicts.items():
for key, inner_row in row.items():
Do SomeThing
#Whoops, forgot to return all my stuff
return1, return2, return3 = foo(dict_of_dicts)
This is a little bit of a tough error to spot because the error can also be produced if the row variable happens to be None on one of the iterations. The way to spot it is that the trace fails on the last line and not inside the function.
If your only returning one variable from a function, I am not sure if the error would be produced... I suspect error "'NoneType' object is not iterable in Python" in this case is actually implying "Hey, I'm trying to iterate over the return values to assign them to these three variables in order but I'm only getting None to iterate over"
It means that the data variable is passing None (which is type NoneType), its equivalent for nothing. So it can't be iterable as a list, as you are trying to do.
You're calling write_file with arguments like this:
write_file(foo, bar)
But you haven't defined 'foo' correctly, or you have a typo in your code so that it's creating a new empty variable and passing it in.
For me it was a case of having my Groovy hat on instead of the Python 3 one.
Forgot the return keyword at the end of a def function.
Had not been coding Python 3 in earnest for a couple of months. Was thinking last statement evaluated in routine was being returned per the Groovy (or Rust) way.
Took a few iterations, looking at the stack trace, inserting try: ... except TypeError: ... block debugging/stepping thru code to figure out what was wrong.
The solution for the message certainly did not make the error jump out at me.
It also depends on Python version you are using. Seeing different error message thrown in python 3.6 and python 3.8 as following which was the issue in my case
Python 3.6
(a,b) = None
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable
Python 3.8
(a,b) = None
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot unpack non-iterable NoneType object
because using for loop while the result it is just one value not a set of value
pola.py
#app.route("/search")
def search():
title='search'
search_name = request.form.get('search')
search_item = User.query.filter_by(id=search_name).first()
return render_template('search.html', title=title, search_item=search_item )
search.html (wrong)
{% for p in search %}
{{ p }}
search.html (correct)
<td>{{ search_item }}</td>
i had this error with pandas in databricks.
The solution for this error was install the library in the cluster
enter image description here
It means data is None, which is not an iterable. Adding an or []* prevents the exception and doesn't print anything:
for row in data or []: # no more TypeError!
print(row)
* credits to some earlier comments; please beware that raising an exception may be a desired behavior too and/or an indicator of improper data setting.
Just continue the loop when you get None Exception,
example:
a = None
if a is None:
continue
else:
print("do something")
This can be any iterable coming from DB or an excel file.

Float Arguments and dict_values with NumPy

The main issue is that I'm being told by Python 3 that dict_values are being used as an argument while, apparently, a string or number is required. (Even though a float is a number, or so I thought.)
I have searched for various solutions to this problem and while I see aspects that seem to suggest what might be done, nothing just comes right out and says it in a way that I can generalize to my particulars.
The code is:
def get_features(self, state, action):
q_state = np.array(SimpleExtractor().get_features(state, action).values()).astype(dtype=float)
return q_state
That returns the following error when the code is executed:
float() argument must be a string or a number, not 'dict_values'
As per one of the suggested solutions, I tried to change the np line so that the dtype was indicated as such:
def get_features(self, state, action):
q_state = np.array(SimpleExtractor().get_features(state, action).values(), dtype=float)
return q_state
However that returns the exact same error.
I'm not sure if it matters but that SimpleExtractor that I'm calling is a large bit of code: SimpleExtractor
I have some reason to believe that this particular bit of code, as written, would work on Python 2. But the particular project I'm on has been restructured to work with Python 3 and so I can no longer run it with Python 2 to test that out.
I think this may have to do with the fact of how the features variable is being returned from the get_features method in SimpleExtractor. But I'm not clear what I would do to get that into some form that my code above would accept.
I'm mainly just looking for insight into what I should do in this kind of situation.
Convert the dict_values object to list first so the array is created from the content of the list. NumPy cannot build an array of the contained items from dict_values, it instead creates an array of type object and puts the dict_values object inside:
>>> np.array(d.values())
array(dict_values([2, 4]), dtype=object)
What you're doing:
>>> import numpy as np
>>> d = {1: 2, 3: 4}
>>> np.array(d.values()).astype(float)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: float() argument must be a string or a number, not 'dict_values'
What you should do:
>>> np.array(list(d.values())).astype(float)
array([ 2., 4.])
q_state = np.array(list(SimpleExtractor().get_features(state, action).values()))\
.astype(dtype=float)

Why am I getting a "type 'bool' is not iterable" if I am not trying to iterate?

Could someone with superior python skills please explain why am I getting a "bool is not iterable" error, when I am only doing some dict lookups and comparisons?
This is the last line of the stack trace:
---> 54 if 'identifiers' in document and 'doi' in document['identifiers'] and document['identifiers']['doi'] == row['doi']:
55 print 'Found DOI'
56 return True
TypeError: ("argument of type 'bool' is not iterable", u'occurred at index 2914')
Is best practice to use a try/except to attempt to read my dict and catch if the key does not exist?
When an object doesn't provide contains(), in attempts to iterate it (https://docs.python.org/2/reference/expressions.html#membership-test-details)
>>> print 1 in True
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument of type 'bool' is not iterable
>>>
The problem seems to be that the key does exist, but has the wrong type, so the in check isn't helpful here. Generally, in python it's better to follow EAFP and handle an exception instead of trying to pre-check everything.
document['identifiers'] is a bool type, and the 'in' is trying to iterate over a bool variable which is giving you this error.
I don't think this is the cause of your error, but you can use the get method:
dict.get(key, default=None)
You can define an optional default value to return if the key is not present, and it won't trip an error.

Python Error Handling

I was trying to convert the list to a set, with the following code:
set1=set(list1)
the code was running fine, but all on a sudden started to give the following error,
set1=set(list1)
TypeError: unhashable type: 'list'
please let me know how may I resolve.
And sometimes some good running program gives error all on a sudden with no parameter changed, how may I debug it?
Your error suggests that your list contains a list. Lists are mutable and thus can't be hashed for use in a set or a dictionary. One work-around is to convert your list into a tuple using tuple(some_list), but if they're heavily nested, it becomes more complex.
Your list contains another list:
>>> set([['contained list'], 1, 2])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
Sets can only contain items that are hashable; any standard type that cannot be mutated is hashable, but a list is not (it can be mutated). By contrast, a tuple is not mutable and can be stored in a set.
Bugs happen, even in code that has been running fine for a while. Debug it with print statements, or better still, by using a debugger like the pdb.
If your bug only appears intermittently, use a try/except block to catch the error, then print out information or use a debugger to figure out what is going on:
try:
set1=set(list1)
except TypeError:
print 'list1 not hashable? contents: %r' % list1
# optionally: import pdb; pdb.set_trace()
raise

TypeError: list objects are unhashable

totalCost = problem.getCostOfActions(self.actions)
Looks like you are trying to use a list as key in a dictionary or something like that. Lists are not hashable, and so they cannot be used as dictionary keys or in sets.
On another note, python gives you a stacktrace when such an error happens, and that includes file-names and line-numbers. You should be able to track down the offending code with that.
Edit About stacktraces:
cat > script.py
foo = [1,2,3]
bar = {}
bar[foo] = "Boom"
print "Never happens"
python script.py
Traceback (most recent call last):
File "script.py", line 3, in <module> // this is the file and the line-number
bar[foo] = "Boom"
TypeError: unhashable type: 'list'
You've probably attempted to use mutable objects such as lists, as the key for a dictionary, or as a member of a set. Mutable items cannot be tracked for such uses efficiently and predictably so they do not provide the hash special attribute.
The error is produced when an unhashable type is added to sets.
>>> s=set((1,2))
>>> a.add([3,4])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
I think this may be also your case. Use tuple instead of list:
>> a.add((3,4))
>>>
Maybe the line should be like this instead
totalCost = sum(map(problem.getCostOfActions,self.actions))
or if you prefer a generator expression
totalCost = sum(problem.getCostOfActions(action) for action in self.actions)
Since I can't see your code, I assumed that problem.getCostOfActions() returns the cost of a single action, as that might cause the error you are getting if self.actions is a list
Alternatively you could fix the function problem.getCostOfActions() so that it returns the total cost of a list of actions as the name suggests.
Consider adding the body of this function to your question if you want help fixing it
I had the same error with django :
food_list = ['spam' , 'more spam' , 'spam spam']
table.cum.add(food_list)
and I got error - TypeError: list objects are unhashable.
The fix is table.cum.add(*food_list)
-add * infront of list to unpack it
the model method add accepts args - (x,y,z) but not ([x,y,z])
hope it helps

Categories