def test_carteisan_product(self): """cartesian_product should return expected results.""" a = 'abc' b = [1,2,3] c = [1.0] d = [0,1] #cartesian_product of list of single list should be same list self.assertEqual(cartesian_product([c]), [(1.0,)]) self.assertEqual(cartesian_product([a]), [('a',),('b',),('c',)]) #should combine two lists correctly self.assertEqual(cartesian_product([a,b]), \ [('a',1),('a',2),('a',3),('b',1),('b',2),\ ('b',3),('c',1),('c',2),('c',3)]) #should combine three lists correctly self.assertEqual(cartesian_product([d,d,d]), \ [(0,0,0),(0,0,1),(0,1,0),(0,1,1),(1,0,0),(1,0,1),(1,1,0),(1,1,1)]) self.assertEqual(cartesian_product([c,d,d]), \ [(1.0,0,0),(1.0,0,1),(1.0,1,0),(1.0,1,1)])
def random_source(a, k, random_f=random): """Makes a random Markov source on alphabet a with memory k. Specifically, for all words k, pr(i|k) = rand(). """ result = dict.fromkeys(list(map(''.join, cartesian_product([a]*k)))) for k in result: result[k] = Freqs(dict(list(zip(a, random_f(len(a)))))) return result
def __new__(cls, data=[], Gap=None, MolType=None): """Fills in the tuple with tuples from the enumerations in data.""" sub_enums = cls._coerce_enumerations(data) return Enumeration.__new__(cls, cartesian_product(sub_enums), \ MolType=MolType)