python - error in sqlalchemy after replacing count query with exists query -


this question has answer here:

class sourcetoport(base):     """"""     __tablename__ = 'source_to_port'     id = column(integer, primary_key=true)     port_no        = column(integer)     src_address    = column(string,index=true)      #----------------------------------------------------------------------     def __init__(self, src_address,port_no):         """"""         self.src_address = src_address         self.port_no     = port_no     def act_like_switch (self, packet, packet_in):     """     implement switch-like behavior.     """     # learn port source mac     #print "recieved port ",packet_in.in_port , "source ",packet.src     # create session     #session = sessionmaker(bind=engine)     #session = session()     self.mac_to_port[packet.src]=packet_in.in_port     #if self.mac_to_port.get(packet.dst)!=none:     print "count dst",session.query(sourcetoport).filter_by(src_address=str(packet.dst)).count(),str(packet.dst)     #if session.query(sourcetoport).filter_by(src_address=str(packet.dst)).count():     if session.query(exists().where(sourcetoport.src_address == str(packet.dst))).scalar() not none:            #send packet            print "got info database"            q_res = session.query(sourcetoport).filter_by(src_address=str(packet.dst)).first()            self.send_packet(packet_in.buffer_id, packet_in.data,q_res.port_no, packet_in.in_port)            #create flow modification message            msg = of.ofp_flow_mod()            #set fields match incoming packet            msg.match = of.ofp_match.from_packet(packet)            #send rule switch not query controller again.            msg.actions.append(of.ofp_action_output(port=q_res.port_no))            #push rule            self.connection.send(msg)     else:            #flood packet out don't know node.            print "flooding first packet"            self.send_packet(packet_in.buffer_id, packet_in.data,                        of.ofpp_flood, packet_in.in_port)            #self.matrix[(packet.src,packet.dst)]+=1                  entry = sourcetoport(src_address=str(packet.src) , port_no=packet_in.in_port)            #add record session object            session.add(entry)            #add record session object            session.commit() 

i have peice of code.i replaced

#if session.query(sourcetoport).filter_by(src_address=str(packet.dst)).count(): 

with

if session.query(exists().where(sourcetoport.src_address == str(packet.dst))).scalar() not none:  getting following error.     file "/home/karthik/pox/tutorial.py", line 86, in act_like_switch     self.send_packet(packet_in.buffer_id, packet_in.data,q_res.port_no, packet_in.in_port) attributeerror: 'nonetype' object has no attribute 'port_no' ^cinfo:core:going down... 

the above piece of code used work count query.why working exists query.

as francis-avila explained in brilliant answer other question there issue in logic exists()...scalar() not none. returns true or false - return not none. sorry mistake while suggesting how use exists in sqlalchemy yesterday.

other that code correct , should work after changing logic of using exists() query results from:

if session.query(exists().where(sourcetoport.src_address == str(packet.dst))).scalar() not none: 

to

if session.query(exists().where(sourcetoport.src_address == str(packet.dst))).scalar(): 

approach using exists()...one() described here , handling exception work too. keep in mind handling exception more expensive operation (use more cpu cycles) in python doing condition check. if application not performance critical - using try/catch exception handling fine.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -