Merge into master from pull request #34:
Small fixes (https://github.com/floodlight/loxigen/pull/34)
diff --git a/.gitignore b/.gitignore
index 917b900..8206747 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,3 +7,6 @@
loxi_output
loxigen.log
tags
+.*.swp
+.*.swo
+*.cache
diff --git a/generic_utils.py b/generic_utils.py
index cebfb7f..eeb26d6 100644
--- a/generic_utils.py
+++ b/generic_utils.py
@@ -31,6 +31,7 @@
Intended to be imported into another namespace
"""
+import functools
import sys
import of_g
@@ -73,3 +74,102 @@
@param out_str The stringified output to write
"""
of_g.loxigen_log_file.write(str(obj) + "\n")
+
+################################################################
+#
+# Memoize
+#
+################################################################
+
+def memoize(obj):
+ """ A function/method decorator that memoizes the result"""
+ cache = obj.cache = {}
+
+ @functools.wraps(obj)
+ def memoizer(*args, **kwargs):
+ key = args + tuple(kwargs.items())
+ if key not in cache:
+ cache[key] = obj(*args, **kwargs)
+ return cache[key]
+ return memoizer
+
+################################################################
+#
+# OrderedSet
+#
+################################################################
+
+import collections
+
+class OrderedSet(collections.MutableSet):
+ """
+ A set implementations that retains insertion order. From the receipe
+ http://code.activestate.com/recipes/576694/
+ as referred to in the python documentation
+ """
+
+ def __init__(self, iterable=None):
+ self.end = end = []
+ end += [None, end, end] # sentinel node for doubly linked list
+ self.map = {} # key --> [key, prev, next]
+ if iterable is not None:
+ self |= iterable
+
+ def __len__(self):
+ return len(self.map)
+
+ def __contains__(self, key):
+ return key in self.map
+
+ def add(self, key):
+ if key not in self.map:
+ end = self.end
+ curr = end[1]
+ curr[2] = end[1] = self.map[key] = [key, curr, end]
+
+ def discard(self, key):
+ if key in self.map:
+ key, prev, next = self.map.pop(key)
+ prev[2] = next
+ next[1] = prev
+
+ def __iter__(self):
+ end = self.end
+ curr = end[2]
+ while curr is not end:
+ yield curr[0]
+ curr = curr[2]
+
+ def __reversed__(self):
+ end = self.end
+ curr = end[1]
+ while curr is not end:
+ yield curr[0]
+ curr = curr[1]
+
+ def pop(self, last=True):
+ if not self:
+ raise KeyError('set is empty')
+ key = self.end[1][0] if last else self.end[2][0]
+ self.discard(key)
+ return key
+
+ def __repr__(self):
+ if not self:
+ return '%s()' % (self.__class__.__name__,)
+ return '%s(%r)' % (self.__class__.__name__, list(self))
+
+ def __eq__(self, other):
+ if isinstance(other, OrderedSet):
+ return len(self) == len(other) and list(self) == list(other)
+ return set(self) == set(other)
+
+def find(iterable, func):
+ """
+ find the first item in iterable for which func returns something true'ish.
+ @raise KeyError if no item in iterable fulfills the condition
+ """
+ for i in iterable:
+ if func(i):
+ return i
+ raise KeyError("Couldn't find value that matches: %s" % repr(func))
diff --git a/utest/test_generic_utils.py b/utest/test_generic_utils.py
new file mode 100755
index 0000000..58e3a4a
--- /dev/null
+++ b/utest/test_generic_utils.py
@@ -0,0 +1,117 @@
+#!/usr/bin/env python
+# Copyright 2013, Big Switch Networks, Inc.
+#
+# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
+# the following special exception:
+#
+# LOXI Exception
+#
+# As a special exception to the terms of the EPL, you may distribute libraries
+# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
+# that copyright and licensing notices generated by LoxiGen are not altered or removed
+# from the LoxiGen Libraries and the notice provided below is (i) included in
+# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
+# documentation for the LoxiGen Libraries, if distributed in binary form.
+#
+# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
+#
+# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
+# a copy of the EPL at:
+#
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# EPL for the specific language governing permissions and limitations
+# under the EPL.
+
+import sys
+import os
+import unittest
+
+root_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..')
+sys.path.insert(0, root_dir)
+
+from generic_utils import *
+
+class MyHash(object):
+ def __init__(self, val):
+ self.val = val
+
+ def __hash__(self):
+ return hash(self.val)
+
+ def __str__(self):
+ return "BoringConstantString"
+
+ def __eq__(self, o ):
+ return type(self) == type(o) and self.val == o.val
+
+class GenericTest(unittest.TestCase):
+ def test_memoize_simple(self):
+ self.count = 0
+
+ @memoize
+ def function():
+ self.count += 1
+ return "Foo"
+
+ self.assertEquals(0, self.count)
+ self.assertEquals("Foo", function())
+ self.assertEquals(1, self.count)
+ self.assertEquals("Foo", function())
+ self.assertEquals(1, self.count)
+
+ def test_memoize_string_args(self):
+ self.count = 0
+
+ @memoize
+ def function(a, b):
+ self.count += 1
+ return "%s:%s" % (a,b)
+
+ self.assertEquals(0, self.count)
+ self.assertEquals("a:b", function('a', 'b'))
+ self.assertEquals(1, self.count)
+ self.assertEquals("ab:", function('ab', ''))
+ self.assertEquals(2, self.count)
+ self.assertEquals("ab:", function('ab', ''))
+ self.assertEquals(2, self.count)
+
+ def test_memoize_kw_args(self):
+ self.count = 0
+
+ @memoize
+ def function(**kw):
+ self.count += 1
+ return ",".join("{k}={v}".format(k=k,v=v) for k,v in kw.items())
+
+ self.assertEquals(0, self.count)
+ self.assertEquals("a=1", function(a=1))
+ self.assertEquals(1, self.count)
+ self.assertEquals("a=1,b=2", function(a=1, b=2))
+ self.assertEquals(2, self.count)
+ self.assertEquals("a=1", function(a=1))
+ self.assertEquals(2, self.count)
+ self.assertEquals("a=1,b=BoringConstantString", function(a=1, b=MyHash('1')))
+ self.assertEquals(3, self.count)
+
+ def test_memoize_with_hashable_object(self):
+ self.count = 0
+
+ @memoize
+ def function(a):
+ self.count += 1
+ return a.val
+
+ self.assertEquals(0, self.count)
+ self.assertEquals("a", function(MyHash('a')))
+ self.assertEquals(1, self.count)
+ self.assertEquals("b", function(MyHash('b')))
+ self.assertEquals(2, self.count)
+ self.assertEquals("a", function(MyHash('a')))
+ self.assertEquals(2, self.count)
+
+if __name__ == '__main__':
+ unittest.main()