What is wrong with my function that contains a "Attribute error" - python

Hey I'm getting this error on my code but I'm not sure why
my code for this function is:
def rotateImage90(image):
ei = EmptyImage(image.getHeight(), image.getWidth())
for i in range (image.getWidth()):
for j in range(image.getHeight()):
orginal = image.getPixel(i, j)
ei.setPixel(image.getHeight() - j - 1, i, orginal)
return ei
the error im getting is
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
drawImage("Spiderman", [spiderman, rotateImage90(spiderman)])
File "/Users/luisarroyo/Documents/project 7.py", line 52, in rotateImage90
orginal = image.getPixel(i, j)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cImage.py", line 310, in getTkPixel
p = [int(j) for j in self.im.get(x,y).split()]
AttributeError: 'tuple' object has no attribute 'split'

Related

TypeError: 'NoneType' object is not iterable in Phyton

My program removes the substring 'rotten' from the string list:
bag_of_fruits = ["apple","rottenBanana","apple"]
def remove_rotten(bag_of_fruits):
bag_of_fruits = [x.removeprefix('rotten') for x in bag_of_fruits]
return [x.lower() for x in bag_of_fruits]
print(remove_rotten(bag_of_fruits))
All tests is completed, but in the end program shows 'Unexpected exception raised':
Traceback (most recent call last):
File "/workspace/default/.venv/lib/python3.10/site-packages/codewars_test/test_framework.py", line 112, in wrapper
func()
File "/workspace/default/tests.py", line 21, in fixed_tests
test.assert_equals(remove_rotten(tst[0]), tst[1], f"Input = {tst[0]}")
File "/workspace/default/solution.py", line 4, in remove_rotten
bag_of_fruits = [x.removeprefix('rotten') for x in bag_of_fruits]
TypeError: 'NoneType' object is not iterable
Try to change the name of the variable to avoid the error
bag_of_fruits = ["apple","rottenBanana","apple"]
def remove_rotten(bag_of_fruits):
# ---- > variable name should be changed
bag_of_fruits_edited = [x.removeprefix('rotten') for x in bag_of_fruits]
return [x.lower() for x in bag_of_fruits_edited]
print(remove_rotten(bag_of_fruits))

'NoneType' Object is not Subscriptable on deap expr func call

I'm aware that there are other questions with the same name, but they're all about indexing and I'm getting this error when trying to invoke func(r_curr). I've already checked and func is not NoneType after assignment, it is <function <lambda> at 0x000001BB1E91E510>.
def eval_symb_reg(individual, points):
global X
global y
global size
func = toolbox.compile(expr=individual)
predict = []
for i in range(X.shape[0]):
total = 0
for j in range(size):
r_curr = X[i][j*size:(j+1)*size]
result = func(r_curr)
total += result[0]
predict.append(total)
error = np.sum((np.array(predict) - y) ** 2)
return [1 / (error / X.shape[0])]
Stack trace:
Traceback (most recent call last):
File "symbreg.py", line 298, in <module>
main()
File "symbreg.py", line 265, in main
halloffame=hof, verbose=True)
File "C:\Users\<myuser>\AppData\Local\Programs\Python\Python36\lib\site-packages\deap\algorithms.py", line 151, in eaSimple
for ind, fit in zip(invalid_ind, fitnesses):
File "symbreg.py", line 198, in eval_symb_reg
result = func(r_curr)
File "<string>", line 1, in <lambda>
TypeError: 'NoneType' object is not subscriptable

Why the TypeError in the tempfile.py?

I'm importing the pltlib on Python3.7.5 by from imview.lib import pltlib.
Then I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/bowen/.local/lib/python3.7/site-packages/imview/lib/pltlib.py", line 15, in <module>
cpt_rainbow = gmtColormap.get_rainbow()
File "/home/bowen/.local/lib/python3.7/site-packages/imview/lib/gmtColormap.py", line 21, in get_rainbow
fn = write_rainbow_cpt()
File "/home/bowen/.local/lib/python3.7/site-packages/imview/lib/gmtColormap.py", line 375, in write_rainbow_cpt
N 255 0 0""")
File "/usr/lib/python3.7/tempfile.py", line 620, in func_wrapper
return func(*args, **kwargs)
TypeError: a bytes-like object is required, not 'str'
I also check the source code here: github.com/python/cpython/blob/3.7/Lib/tempfile.py
But it seems that my tempfile.py is correct, so why this error happen?

TypeError: 'NoneType' object is not subscriptable referring to split function

I am running a piece of code in python which after running produces the following error:
... reading line 1400000 from file users.csv
Traceback (most recent call last):
File "xgb.py", line 32, in <module>
(header_items, items) = select(ITEMS_FILE, lambda x: True, build_item, lambda x: int(x[0]))
File "/Users/yas/Documents/GitHub/fairness/2017-master/baseline/parser.py", line 34, in select
obj = toObject(cmp, header)
File "/Users/yas/Documents/GitHub/fairness/2017-master/baseline/parser.py", line 56, in build_item
[int(x) for x in str_item[names["title"]].split(",") if len(x) > 0],
TypeError: 'NoneType' object is not subscriptable
The error is pointing me to this part of the code:
def build_item(str_item, names):
return Item(
[int(x) for x in str_item[names["title"]].split(",") if len(x) > 0],
int(str_item[names["career_level"]]),
int(str_item[names["industry_id"]]),
int(str_item[names["discipline_id"]]),
str_item[names["country"]],
str_item[names["region"]]
)
How can I fix it, please?

TypeError: object of type 'int' has no len() when using sop.brute

I use the Python3.6 and I've been confused about this question for a long time..so here is my code.
def fo(x,y):
z=np.sin(x)+0.05*x**2+np.cos(y)+0.05*y**2
if output == True:
print("%8.4f %8.4f %8.4f" % (x,y,z))
return z
import scipy.optimize as sop
sop.brute(fo,(-10,10.1,5),(-10,10.1,5),finish = None)
Here is the error I get:
Traceback (most recent call last):
File "<ipython-input-12-c7886e35ff4b>", line 1, in <module>
sop.brute(fo,(-10,10.1,5),(-10,10.1,5),finish = None)
File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 2811, in brute
if len(lrange[k]) < 3:
TypeError: object of type 'int' has no len()
here's another try:
r1=slice(-10,10.1,5)
r2=slice(-10,10.1,5)
sop.brute(fo,r1,r2,finish = None)
and the error:
Traceback (most recent call last):
File "<ipython-input-48-230c07265998>", line 1, in <module>
sop.brute(fo,r1,r2,finish = None)
File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 2804, in brute
N = len(ranges)
TypeError: object of type 'slice' has no len()
sop.brute(fo,(r1,r2),finish = None)
TypeError: fo() missing 1 required positional argument: 'y'
I'm new to here and sorry if I ask a stupid question but I cant' work it out T.T thx a lot
def fo(p):
x, y = p
z = np.sin(x)+0.05*x**2+np.sin(y)+0.05*y**2
if output == True:
print('%8.4f %8.4f %8.4f' % (x,y,z))
return z
unpack tuple like in the code

Categories