Python help() function and the string.title function - python

Why doesn't
import string;help(string.title)
seem to work but
help(string.strip)
works just fine?
I get the error
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'module' object has no
attribute 'title'

title is a method on objects of type str, not a function in the string module. That means you can do "foo".title() or str.title("foo") but not string.title("foo").

help(str.title) seems to work just fine.

Related

AttributeError: 'function' object has no attribute 'make' in Flask web app

I am trying to generate QR code in python. My code is working fine in another python file but throws an error when I tried to use it in Flask webapp.
I've already installed pillow and qrcode.
#app.route('/qr')
def qrcode():
img = qrcode.make('https://youtube.com')
img.save('first-image1.png')
return 'all good'
Inside your function qrcode() refers to the local function, not to the external qrcode module/function/object. Your qrcode() function does not have any attribute named make, therefore an exception is raised. Here's an example:
def f():
print(f.make)
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in f
AttributeError: 'function' object has no attribute 'make'
To fix this, rename your function so that it does not clash with qrcode:
#app.route('/qr')
def qr():
img = qrcode.make('https://youtube.com')
img.save('first-image1.png')
return 'all good'
Here I simply renamed your function to qr() so that it will no longer shadow qrcode.

How to read Python json file?AttributeError: '_io.TextIOWrapper' object has no attribute 'load'

I am trying to load my json file. I am new to MacOS,but that should be the problem. My code
from terminal
import json
>>> with open('ticr_calculated_3_2020-05-27T11-01-30.json','r+', encoding='utf-8') as f:
data=json.load(f)
I got error
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
AttributeError: '_io.TextIOWrapper' object has no attribute 'load'
The same error happens with json example from Python docs
json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: '_io.TextIOWrapper' object has no attribute 'dumps'
Why?
Have you written everything into one Python interpreter session? It seems to me that you defined the name json before.
Try opening a new Python interpreter or writing your script into a file.

Python Error: TypeError: 'module' object is not callable

I have written the simplest python code (module) :
def newplus(x, y):
return x*y
This is stored in a folder sabya (which is my package). The folder has _init_ and newplus.py files.
In my IDLE I can open the module sabya.newplus. When I give import sabya.newplus there is no error. but when I issue :
>>> sabya.newplus(2, 3)
I am getting this error
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
sabya.newplus(2, 3)
TypeError: 'module' object is not callable
You need to qualify the function as follow:
sabya.newplus.newplus(2, 3)

Error when calling gimp-drawable-set-pixel in gimp's python console

I'm typing this code directly into Gimp's Python Console:
img=gimp.image_list()[0]
drw = pdb.gimp_image_active_drawable(img)
gimp-drawable-set-pixel(drw,x,y,3,[0xff,0x00,0x00])
and it's yeilding this error after calling the gimp-drawable-set-pixel function:
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'drawable' is not defined
It yields the same error when called with four bytes instead of three bytes as well.
I'm using gimp 2.8.10 on Ubuntu 14.04.01 x86_64
I think it's a typo of gimp_drawable_set_pixel.
pdb.gimp_drawable_set_pixel(drw,x,y,3,[0xff,0x00,0x00])

Randomly NoneType object is not callable error

Recently I've got this problem in my application:
File "main.py", line 31,
in File "app.pyc", line 205, in run
TypeError: 'NoneType' object is not callable"
My code:
xml = EXML()
for pid, product in store.products.items():
xml.addRow()
xml.addCell((product['name']).encode('utf-8'), "String")
xml.addCell((product['symbol']).encode('utf-8'), "String")
xml.addCell((product['brand_name']).encode('utf-8'), "String") # line 205
xml.addCell(str(product['price']), "String")
Python 2.7 32-bit
It's wired, because this showed up after ~1000 iterations, with out any previus problem.
This application scans online store to get current prices.
Firstly I thought that somewhere I missed someting, and as result there is None.encode('utf-8'), but no, and "".encode('utf-8') seems to work. Moreover, I can't reproduce this error on testing site, just sometimes shows up while hard-working with ~2500 products.
What are possible other sources of this error?
>>> None.encode
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'encode'
>>> None()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
On the given line you would have to set one of the two functions called to None somehow. Are you sure it's not the next line, because overwriting str is a rather common error.
OK, solved, it's bit bizzare, but this error is caused by product['brand_name'] which is sometimes BeautifulSoup.tag ( tag this time ) instead of BeautifulSoup.NavigableString as I planned. I still don't understad why and wtf ?
Anywat, great thanks for response. :)

Categories

Resources