def __iter__(self):
        for alias in self.settings.iterkeys():
            config = self.get_value(alias)

            shard_info = config.get('SHARDS', {})
            host_list = config.get('HOSTS')

            if not shard_info and not host_list:
                yield alias, config
                continue

            # if servers are present this is a virtual connection mapping to N connections
            hosts = {}
            if host_list:
                for num, host_config in host_list.iteritems():
                    for key, value in config.iteritems():
                        if key in ('SHARDS', 'HOSTS'):
                            continue
                        host_config.setdefault(key, value)
                    hosts[num] = host_config
            else:
                hosts[0] = config

            assert not set(hosts.iterkeys()).difference(set(xrange(len(hosts)))), 'Host indexes must not contain any gaps'

            cluster_size = shard_info.get('size') or len(hosts)
            assert cluster_size > 0, 'Cluster cannot be empty'

            # we create ``size`` new connections based on the parent connections
            for num in xrange(cluster_size):
                shard = '%s.shard%d' % (alias, num)
                host_num = num % len(hosts)

                shard_n_config = hosts[host_num].copy()

                # test mirror can vary if its referencing a clustered connection
                if not shard_n_config.get('TEST_MIRROR'):
                    shard_n_config['TEST_MIRROR'] = alias

                # ensure test mirror points to parent shard if a mirror is not
                # explicitly set
                yield shard, shard_n_config

            # maintain the original connection for dev/test environments
            yield alias, config
dataset/ETHPy150Open disqus/sharding-example/sqlshards/utils.py/DatabaseConfigurator.__iter__/original