+++ /dev/null
-# Copyright 2007 James Bunton <jamesbunton@fastmail.fm>
-# Licensed for distribution under the GPL version 2, check COPYING for details
-# Quicker, easier regular expressions
-
-import re
-
-class RE(object):
- def __init__(self, regex):
- self.regex = re.compile(regex)
-
- def __eq__(self, s):
- assert isinstance(s, basestring)
- return self.regex.search(s)
-
- def __ne__(self, s):
- return not self == s
-
+++ /dev/null
-#!/usr/bin/env python
-
-__all__ = ["string_interpolate"]
-
-def string_interpolate(format, data):
- fakeData = KeyDetect()
- format % fakeData
- keys = fakeData.keys
-
- for d in combinations(data, keys):
- yield format % d
-
-def myiter(obj):
- if isinstance(obj, basestring):
- return [obj]
- else:
- return obj
-
-def combinations(data, keys):
- key = keys[0]
- keys = keys[1:]
-
- if len(keys) == 0:
- for val in myiter(data[key]):
- yield {key: val}
-
- else:
- for val in myiter(data[key]):
- for d in combinations(data, keys):
- d[key] = val
- yield d
-
-
-class KeyDetect(object):
- def __init__(self):
- self.keys = []
- def __getitem__(self, key):
- self.keys.append(key)
- return ""
-
+++ /dev/null
-#!/usr/bin/env python
-
-import unittest
-
-from stringinterpolate import string_interpolate
-
-class TestInterpolate(unittest.TestCase):
- def test1(self):
- format = "%(a)s %(b)s"
- data = {"a": "hello", "b": "world"}
- result = list(string_interpolate(format, data))
- expected = ["hello world"]
- self.assertEquals(result, expected)
-
- def test2(self):
- format = "%(a)s %(b)s"
- data = {"a": ["hello", "hello2"], "b": ["world", "world2"]}
- result = list(string_interpolate(format, data))
- expected = ["hello world", "hello world2", "hello2 world", "hello2 world2"]
- self.assertEquals(result, expected)
-
- def test3(self):
- format = "%(a)s %(b)s"
- data = {"a": ["hello", "hello2"], "b": "world"}
- result = list(string_interpolate(format, data))
- expected = ["hello world", "hello2 world"]
- self.assertEquals(result, expected)
-
-if __name__ == "__main__":
- unittest.main()
-