def test_memoize(self): r_const = lambda imax: random.randint(0, imax) f0 = r_const f1 = T.memoize(r_const) self.assertNotEquals(f0(100), f0(100)) self.assertEquals(f1(100), f1(100))
>>> get_hostname_("foo.example.com") 'foo.example.com' >>> get_hostname_("foo.example.com", False) 'foo' """ if h is None: try: h = socket.getfqdn() or socket.gethostname() or os.uname()[1] except: h = "localhost.localdomain" return h if fqdn else (h.split('.')[0] if '.' in h else h) # Memoized versions: get_username = memoize(get_username_) get_hostname = memoize(get_hostname_) def find_dups_in_dicts_list_g(ds, keys): """ Generator to find out duplicated items (values) in given list of dicts. :param ds: A list of dicts to find out duplicated items. :param keys: Key to search duplicated items. >>> d0 = {'a': 1, 'b': 2} >>> d1 = {'b': 2, 'c': 4} >>> ds = [d0, d1, {}] >>> dups = [t for t in find_dups_in_dicts_list_g(ds, ('a', 'b'))]