How about this? It is more direct to use Packet.__contains__.

>From b4d813f51cf01ed564033ad71a6db60338d41d74 Mon Sep 17 00:00:00 2001
Message-Id: 
<b4d813f51cf01ed564033ad71a6db60338d41d74.1370830592.git.yamah...@valinux.co.jp>
In-Reply-To: <cover.1370830592.git.yamah...@valinux.co.jp>
References: <cover.1370830592.git.yamah...@valinux.co.jp>
From: Isaku Yamahata <yamah...@valinux.co.jp>
Date: Mon, 10 Jun 2013 11:14:59 +0900
Subject: [PATCH] lib/packet: 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 <yamah...@valinux.co.jp>
---
 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 da1077b..a40202f 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
 
@@ -110,3 +112,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
-- 
1.7.10.4




On Mon, Jun 10, 2013 at 08:30:55AM +0900, FUJITA Tomonori wrote:
> you can do something like:
> 
> if arp.arp in Packet(msg.data):
> 
> Signed-off-by: FUJITA Tomonori <fujita.tomon...@lab.ntt.co.jp>
> ---
>  ryu/lib/packet/packet_base.py | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/ryu/lib/packet/packet_base.py b/ryu/lib/packet/packet_base.py
> index b0aeca0..6525664 100644
> --- a/ryu/lib/packet/packet_base.py
> +++ b/ryu/lib/packet/packet_base.py
> @@ -80,3 +80,7 @@ class PacketBase(object):
>          For example, *prev* is ipv4 or ipv6 for tcp.serialize.
>          """
>          pass
> +
> +    def __eq__(self, other):
> +        if self.__class__ == other:
> +            return True
> -- 
> 1.7.12.4 (Apple Git-37)
> 
> 
> ------------------------------------------------------------------------------
> How ServiceNow helps IT people transform IT departments:
> 1. A cloud service to automate IT design, transition and operations
> 2. Dashboards that offer high-level views of enterprise services
> 3. A single system of record for all IT processes
> http://p.sf.net/sfu/servicenow-d2d-j
> _______________________________________________
> Ryu-devel mailing list
> Ryu-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/ryu-devel
> 

-- 
yamahata

------------------------------------------------------------------------------
How ServiceNow helps IT people transform IT departments:
1. A cloud service to automate IT design, transition and operations
2. Dashboards that offer high-level views of enterprise services
3. A single system of record for all IT processes
http://p.sf.net/sfu/servicenow-d2d-j
_______________________________________________
Ryu-devel mailing list
Ryu-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to