From 6cc2c48016b2ec1378b6796828a8e30264410dbe Mon Sep 17 00:00:00 2001 From: Isaku Yamahata Date: Mon, 10 Jun 2013 15:58:55 +0900 Subject: [PATCH] packet lib: add Packet.__contains__ you can do something like: if arp.arp in Packet(msg.data): a = arp.arp(...) if a in Packet(msg.data): >>> from ryu.lib.packet import packet >>> from ryu.lib.packet import arp >>> a = arp.arp_ip(1, 0, 0, 0, 0) >>> p = packet.Packet() >>> p.protocols = [a] >>> arp.arp in p True >>> a in p True Signed-off-by: Isaku Yamahata Signed-off-by: FUJITA Tomonori --- ryu/lib/packet/packet.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ryu/lib/packet/packet.py b/ryu/lib/packet/packet.py index def580b6..ff06cc3a 100644 --- a/ryu/lib/packet/packet.py +++ b/ryu/lib/packet/packet.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import inspect + from . import packet_base from . import ethernet @@ -109,3 +111,9 @@ class Packet(object): def __len__(self): return len(self.protocols) + + def __contains__(self, protocol): + if (inspect.isclass(protocol) and + issubclass(protocol, packet_base.PacketBase)): + return protocol in [p.__class__ for p in self.protocols] + return protocol in self.protocols