table = QTableView()
model = QStandardItemModel()
table.setModel(model)
r = table.model().rowCount()
c = table.model().columnCount()
tLeft = table.model().index(0, 0)
bRight = table.model().index(r, c)
table.dataChanged(tLeft, bRight).connect(SomeFunction)
AttributeError: 'NoneType' object has no attribute 'connect'
Goal - to call SomeFunction when one of the items was changed directly by user in QTableView().
What do I do wrong? I see that NoneType object cannot has attribute connect, but why it is NoneType.
Please comment. I am a beginner. Qt5.
You should do:
table.model().dataChanged.connect(someFunction)
# or model.dataChanged.connect(someFunction)
No need to precise the arguments. The slot should look like this:
def someFunction(tLeft,bRight):
#do stuff
Related
i have a problem with python objects where i am creating and iterating over many objects simultaneously.
objects are always created with the correct attributes, but when i iterate over those objects, i check for a certain attribute in the object, this specific attribute randomly gets deleted from the object and then i get an error that the object does not have this attribute.
code:
dead_list = []
for key, trackable_vehicle in self.lane_vehciles.items():
if trackable_vehicle.vehicle_status == 'DEAD' :
dead_list.append(key)
for key in dead_list:
save_object(trackable_vehicle.vehicle_type+'_'+str(trackable_vehicle.object_ID), trackable_vehicle)
self.lane_vehciles.pop(key, None)
Error: 'TrackableTruck' object has no attribute 'vehicle_status'
please note that this code works for me 95% of the time, and this happens for random objects although all objects has the same structure.
after a lot of debugging i just cant get my head around why this happens. the object is correctly created and if i print it before iteration it does have the "vehicle_status" attribute but after iteration the attribute disappears from the object
for example:
object at creation:
{"object_ID": 55, "vehicle_type": "truck", "OTS": "2020-03-10 16:07:16", "lane_ID": 2.0, "vehicle_status": "ALIVE"}
object after iteration:
{"object_ID": 55, "vehicle_type": "truck", "OTS": "2020-03-10 16:07:16", "lane_ID": 2.0}
i hope someone with better knowledge at python than myself can help me.
thanks
EDIT object definition:
class TrackableTruck:
def __init__(self, object_ID, vehicle_type, OTS, lane_ID, vehicle_status):
# info from tracking server
self.object_ID = object_ID
self.vehicle_type = vehicle_type
self.OTS = OTS
self.lane_ID = lane_ID
self.vehicle_status = vehicle_status
code to check and change vehicle status:
d = pickle.loads(full_msg[self.HEADERSIZE:])
d = json.loads(d)
vehicle_type = str(d['vehicle_type'])
vehicle_lane = int(d['lane_ID'])
vehicle_id = int(d['object_ID'])
vehicle_status = d['vehicle_status']
if vehicle_lane == 1:
trackable_vehicle = self.streetLane_1.get_vehicle(vehicle_id)
if trackable_vehicle is None:
self.streetLane_1.add_vehicle(d)
else:
trackable_vehicle.vehicle_status = 'DEAD'
object creation code:
def object_decoder(self,json_obj):
return TrackableTruck(int(json_obj['object_ID']), json_obj['vehicle_type'], json_obj['OTS'], json_obj['lane_ID'], json_obj['vehicle_status'])
def add_vehicle(self, json_obj):
trackable_vehicle = self.object_decoder(json_obj)
self.lane_vehciles[int(trackable_vehicle.object_ID)] = trackable_vehicle
Can anyone explain why this keeps happening to me?
class TourAgency:
def __init__(self):
self._tours = {}
self._scheduledtours = {}
self._customers = {}
self._booking = {}
def addTour(self,code,tour):
self._tours[code] = tour
def addscheduledtours(self,code,scheduledtour):
self._scheduledtours[code] = scheduledtour
def addCustomer(self,code,customer):
self._customers[code] = customer
def addBooking(self,bookingId,booking):
self._booking[bookingId] = booking
def searchscheduledtours(self,code):
if code in self.scheduledtours.keys():
return self._scheduledtours[code]
else:
return None
mytour = TourAgency()
t1 = Tour("KO111","Discover Korea",8,7,1449.36)
print(t1)
ta = mytour.addTour('KO111',t1)
print(TourAgency.tours)
I get an error saying:
print(TourAgency.tours)
AttributeError: type object 'TourAgency' has no attribute 'tours'
Your class hasn't got the tours attribute, it has only got the _tours attribute. Maybe you want to use it instead.
Remember that, in Python, if an attribute name starts with an underscore, it means that the attribute should be private and not intended to be used by the user.
I hope this could help you! Cheers!
I'm getting this error:
[...], line 28, in <module>
PlayerDamage = Dice * int(set_p_w.player_damage)
AttributeError: 'NoneType' object has no attribute 'player_damage'
When I run this code:
import player
Dice = random.randrange(1, 7)
set_p_w = player.set_player_weapon()
PlayerDamage = Dice * set_p_w.player_damage
This is how player.set_player_weapon() looks like:
def set_player_weapon():
import items
player_weapon = items.WBrokenSword
player_damage = player_weapon.damage
I searched everywhere and tried a bunch of different solutions, but nothing helped me. What is wrong with my code?
From the code you posted, player.set_player_weapon() doesn’t return anything. So set_p_w is nothing. You are interacting with set_p_w as if it is an object, but set_player_weapon() doesn’t create an object, it just sets two local variables (player_weapon and player_damage) and then discards them when the function ends.
The simplest way to get this to work is to have your player.set_player_weapon() method return a tuple with that information so it can be stored in the a variable outside the function: (player_weapon, player_damage).
Tuple Method
def set_player_weapon():
import items
player_weapon = items.WBrokenSword
player_damage = player_weapon.damage
return (player_weapon, player_damage)
player_weapon_damage = player.set_player_weapon()
PlayerDamage = Dice * player_weapon_damage[0]
A better way would be to make an class for Player which has the attributes player_weapon and player_damage as well as methods like def set_player_weapon() that set and change its attributes.
im new to python and i know there has been a lot of discussion on this but I still have a question. I am trying to access a variable from class to a same class function, its throughing a error as
"AttributeError: 'int' object has no attribute 'isOpen'"
Code is:
class serialCommunication():
ser = 0 #class global variable
def ser_port():
.....
def ser_Burdrate():
BurdRate = ( "4800",
"9600",
"19200",
"38400",
"57600",
"115200"
)
BR = int(input("Enter the Burd Rate\n0\t--\t4800\n1\t--\t9600\n2\t--\t19200\n3\t--\t38400\n4\t--\t57600\n5\t--\t115200\n\n"))
return Portno , int(BurdRate[BR]), timeout
def ser_open():
port = serialCommunication.ser_Burdrate()
serialCommunication.ser = serial.Serial(port[0], port[1], timeout=port[2])
port = serialCommunication.ser.getPort()
print (serialCommunication.ser , '\r\n')
.....
def ser_Write():
if (serialCommunication.ser.isOpen()):
print ('open: ', serialCommunication.ser.getPort())
elif (serialCommunication.ser.closed()):
serialCommunication.ser_open()
please advice on the same
thanks in advance
thanks for advice i changed
ser = serial.Serial()
and it's throughing a error as
"TypeError: 'bool' object is not callable"
in if statement the bool object can be executed right..?
You're trying to call a method .isOpen() on a int type (ser = 0 declared in the class serialCommunication) as the error suggests the object does not have this method.
I can't see the rest of the class but are you sure you shouldn't be trying to reference a method of the class directly? Also without an instance the method you call would need to be a class method prefixed with #classmethod decorator.
I'm using Python 2.6.4 and its module sqlite3 for a small database project and I have the following problem: I'm trying to use a user-defined function, I mean, a function that you define in Python to use after inside your queries. The function is a wrapper for other function I have in another module. The problem is that when doing the query, I always get an AttributeError exception with the message: 'builtin_function_or_method' object has no attribute 'execute' and I have no clue why. The code is the following. Could you point me what I'm doing wrong?
Thanks in advance.
Wrapper function:
def SQLAreSimilar(userSelectedName, artistName):
'''
Wrapper to the areSimilar function of strUtils to use it directly inside the
queries. Not to be called directly.
'''
if strUtils.areSimilar(userSelectedName, artistName):
return 1
else:
return 0
Function that actually does the query. Note the use of "create_function" method of the Connection object.
def getArtistsBySimilarName(name):
'''
Returns all artists with a similar name, according to the Levenshtein
distance. The DB is supposed to be initialised. Returns a dictionary of
dictionaries with the data of all similar artists indexed by its CODARTIST,
the PK. The dictionary is empty if no row is returned. None is returned on
error.
'''
try:
con = sqlite3.connect(genericConf.SQL_DBNAME)
con.row_factory = sqlite3.Row
con.create_function("ARESIMILAR", 2, SQLAreSimilar)
cur = con.cursor
rows = cur.execute(specificConf.SQL_SELECT_ARTIST_SIMILARNAME, name)
retDict = {}
for row in rows:
d = {}
d['NUMCD'] = row['NUMCD']
d['NAME'] = row['NAME']
retDict[row['CODARTIST']] = d
return retDict
except:
return None
And finally, the query. It is inside the module called "specificConf". So it is used correctly in the function above, the problem is not there.
SQL_SELECT_ARTIST_SIMILARNAME = u'''
SELECT CODARTIST, NAME, NUMCD, ARESIMILAR(?, NAME) AS SIMILAR
FROM ARTIST
WHERE SIMILAR = 1
'''
cur = con.cursor # sets `cur` to a method
should be
cur = con.cursor() # calls the method and sets `cur` to the return value
This is why you were getting the error saying cur has no execute attribute:
AttributeError exception with the
message: 'builtin_function_or_method'
object has no attribute 'execute'