From dd1b9a567b01be0f0bde0688f772d4c698446d65 Mon Sep 17 00:00:00 2001 From: Claudio Pisa Date: Thu, 21 May 2015 23:15:29 +0200 Subject: [PATCH 1/3] implement __sub__ in base parser use the substraction overloaded operator to call the diff between parsers / topologies example: >>> from netdiff import OlsrParser >>> url = 'telnet://127.0.0.1:9090/' >>> p = OlsrParser(url) >>> q = OlsrParser(url) >>> p - q --- netdiff/parsers/base.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/netdiff/parsers/base.py b/netdiff/parsers/base.py index c83f544..5997bb7 100644 --- a/netdiff/parsers/base.py +++ b/netdiff/parsers/base.py @@ -11,6 +11,7 @@ import urllib.parse as urlparse from ..exceptions import NetParserException, NetParserJsonException, NetJsonException +from ..utils import diff class BaseParser(object): @@ -44,6 +45,9 @@ def __init__(self, data, version=None, revision=None, metric=None): if self.__class__ is not BaseParser: self.parse(self.original_data) + def __sub__(self, other): + return diff(other, self) + def _to_python(self, data): """ Private method which parses the input data and converts it into a Python data structure From 6be2fa2df7c1fb373476ee5549f041d787b548b9 Mon Sep 17 00:00:00 2001 From: Claudio Pisa Date: Thu, 21 May 2015 23:32:16 +0200 Subject: [PATCH 2/3] add test for subtraction between parsers --- tests/olsr/tests.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/olsr/tests.py b/tests/olsr/tests.py index 46bda18..9a8b204 100644 --- a/tests/olsr/tests.py +++ b/tests/olsr/tests.py @@ -91,6 +91,17 @@ def test_added_1_link(self): self.assertIn('10.150.0.5', result['added'][0]) self.assertIn('10.150.0.4', result['added'][0]) + def test_added_1_link_sub(self): + old = OlsrParser(links2) + new = OlsrParser(links3) + result = new - old + # ensure there are differences + self.assertEqual(len(result['added']), 1) + self.assertEqual(len(result['removed']), 0) + # ensure 1 link added + self.assertIn('10.150.0.5', result['added'][0]) + self.assertIn('10.150.0.4', result['added'][0]) + def test_removed_1_link(self): old = OlsrParser(links3) new = OlsrParser(links2) From a435be5a4583b17aeab4b35e90b18e41df5c9f93 Mon Sep 17 00:00:00 2001 From: Claudio Pisa Date: Thu, 21 May 2015 23:35:39 +0200 Subject: [PATCH 3/3] Updated README to include the subtraction operation --- README.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.rst b/README.rst index f2269a6..6226c09 100755 --- a/README.rst +++ b/README.rst @@ -84,6 +84,17 @@ The output will be a dictionary with the following structure: "removed": [] } +In alternative, you can use the subtraction operator: + +.. code-block:: python + + from netdiff import OlsrParser + from netdiff import diff + + stored = OlsrParser('./stored-olsr.json') + latest = OlsrParser('telnet://127.0.0.1:9090') + latest - stored + Parsers -------