[Tutor] Multiple search and replace?

2013-05-02 Thread Andy McKenzie
Hey folks,

  I'm trying to figure out how to do something, and it feels like it should
be possible, but I can't figure out how.  What I want is to define four
expressions, like so:

(\sNorth\s|\sN\s)(\sSouth\s|\sS\s)(\sEast\s|\sE\s)(\sWest\s|\sW\s)

And then run a replace such that the groups are replaced, in order, with "N
", "S ", "E ", or "W " (capital letters, no spaces before).

It's easy enough to use re.sub to replace ONE of those, and I could just do
that four times, but is there a way to do all at once?  It looks from some
things I've seen like it should be, but I can't figure out how.

Thanks,
  Andy McKenzie
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Multiple search and replace?

2013-05-02 Thread Peter Otten
Andy McKenzie wrote:

> Hey folks,
> 
>   I'm trying to figure out how to do something, and it feels like it
>   should
> be possible, but I can't figure out how.  What I want is to define four
> expressions, like so:
> 
> (\sNorth\s|\sN\s)(\sSouth\s|\sS\s)(\sEast\s|\sE\s)(\sWest\s|\sW\s)
> 
> And then run a replace such that the groups are replaced, in order, with
> "N ", "S ", "E ", or "W " (capital letters, no spaces before).
> 
> It's easy enough to use re.sub to replace ONE of those, and I could just
> do
> that four times, but is there a way to do all at once?  It looks from some
> things I've seen like it should be, but I can't figure out how.

re.sub() accepts a function where you can do anything you like:

>>> r = re.compile(r"\b(red|yellow|blue)\b")
>>> def sub(match):
... s = match.group(1)
... return {"red": "pink", "yellow": "monsters"}.get(s, "starts to cry")
... 
>>> r.sub(sub, "Who's afraid of red yellow and blue?")
"Who's afraid of pink monsters and starts to cry?"

>>> re.compile(r"\b(north|south|east|west)\b").sub(lambda m: m.group(1)[:1],
... "north by northwest, south of southpark")
'n by northwest, s of southpark'


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] query from sqlalchemy returns AttributeError: 'NoneType' object

2013-05-02 Thread Karthik Sharma
from pox.core import core
import pox.openflow.libopenflow_01 as of
import re
import datetime

from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Date, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql.expression import exists

log = core.getLogger()

engine = create_engine('sqlite:///nwtopology.db', echo=False)
Base = declarative_base()
Session = sessionmaker(bind=engine)
session = Session()


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



#create tables
Base.metadata.create_all(engine)

class Tutorial (object):
  def __init__ (self, connection):
self.connection = connection
connection.addListeners(self)
# Use this table to keep track of which ethernet address is on
# which switch port (keys are MACs, values are ports).
self.mac_to_port = {}
self.matrix={}

#This will keep track of the traffic matrix.
#matrix[i][j]=number of times a packet from i went to j

  def send_packet (self, buffer_id, raw_data, out_port, in_port):
#print "calling send_packet"
#Sends a packet out of the specified switch port.
msg = of.ofp_packet_out()
msg.in_port = in_port
msg.data = raw_data
# Add an action to send to the specified port
action = of.ofp_action_output(port = out_port)
msg.actions.append(action)
# Send message to switch
self.connection.send(msg)

  def act_like_hub (self, packet, packet_in):
#flood packet on all ports
self.send_packet(packet_in.buffer_id, packet_in.data,
 of.OFPP_FLOOD, packet_in.in_port)

  def act_like_switch (self, packet, packet_in):
"""
Implement switch-like behavior.
"""
# Learn the port for the source MAC
#print "RECIEVED FROM PORT ",packet_in.in_port , "SOURCE
",packet.src
# create a 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 for
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() is not None:
 #send this packet
   print "got info from the database"
   q_res =
session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).one()
   self.send_packet(packet_in.buffer_id, packet_in.data,q_res.port_no,
packet_in.in_port)
   #create a flow modification message
 msg = of.ofp_flow_mod()
 #set the fields to match from the incoming packet
   msg.match = of.ofp_match.from_packet(packet)
   #send the rule to the switch so that it does not query the
controller again.
   msg.actions.append(of.ofp_action_output(port=q_res.port_no))
   #push the rule
   self.connection.send(msg)
else:
   #flood this packet out as we don't know about this node.
   print "flooding the 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 the record to the session object
   session.add(entry)
   #add the record to the session object
   session.commit()

  def _handle_PacketIn (self, event):
"""
Handles packet in messages from the switch.
"""
packet = event.parsed # This is the parsed packet data.
if not packet.parsed:
  log.warning("Ignoring incomplete packet")
  return
packet_in = event.ofp # The actual ofp_packet_in message.

#self.act_like_hub(packet, packet_in)
self.act_like_switch(packet, packet_in)

def launch ():
  """
  Starts the component
  """
  def start_switc

Re: [Tutor] query from sqlalchemy returns AttributeError: 'NoneType' object :p:

2013-05-02 Thread Paradox


On 05/03/2013 06:13 AM, Karthik Sharma wrote:


from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Date, Integer, String

This list is mostly for standard library questions (though there may be 
some that can help here too).  Did you know there is an sqlalchemy 
google group (


This list is really for standard library questions (though there may be some 
that can help here too).  Did you know there is an sqlalchemy google group?  
The site is here: http://groups.google.com/group/sqlalchemy?hl=en.  Michael 
Bayer frequently answers questions there too, though there are lots of 
knowledgeable people on that list that can handle simpler problems.

thomas

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Why do I not get the same results for these two functions when I pass 7 as an argument?

2013-05-02 Thread Nonso Ibenegbu
Hello everyone,
Wonder if someone can help me understand why these two codes do not give
the same results for what looks essentially the same ("?") code. The
argument passed is
7.

def rental_car_cost(days):
payment = days * 40
if days >= 7:
return payment - 50
elif days >= 3 < 7:
return payment - 20
else:
return payment

and...

def rental_car_cost(days):
payment = days * 40
if days >= 3 < 7:
return payment - 20
elif days >= 7:
return payment - 50
else:
return payment
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor