Best Python code snippet using lisa_python
ring.py
Source:ring.py  
...27        self.hashi = self.runtime.hashi28        if weight_fn and not hasattr(weight_fn, '__call__'):29            raise TypeError('weight_fn should be a callable function')30        self._weight_fn = weight_fn31        if self._configure_nodes(nodes):32            self.runtime._create_ring(self.runtime._nodes.items())33    def _configure_nodes(self, nodes):34        """Parse and set up the given nodes.35        :param nodes: nodes used to create the continuum (see doc for format).36        """37        if isinstance(nodes, str):38            nodes = [nodes]39        elif not isinstance(nodes, (dict, list)):40            raise ValueError(41                'nodes configuration should be a list or a dict,'42                ' got {}'.format(type(nodes)))43        conf_changed = False44        for node in nodes:45            conf = {46                'hostname': node,47                'instance': None,48                'nodename': node,49                'port': None,50                'vnodes': self._default_vnodes,51                'weight': 152            }53            current_conf = self.runtime._nodes.get(node, {})54            nodename = node55            # new node, trigger a ring update56            if not current_conf:57                conf_changed = True58            # complex config59            if isinstance(nodes, dict):60                node_conf = nodes[node]61                if isinstance(node_conf, int):62                    conf['weight'] = node_conf63                elif isinstance(node_conf, dict):64                    for k, v in node_conf.items():65                        if k in conf:66                            conf[k] = v67                            # changing those config trigger a ring update68                            if k in ['nodename', 'vnodes', 'weight']:69                                if current_conf.get(k) != v:70                                    conf_changed = True71                else:72                    raise ValueError(73                        'node configuration should be a dict or an int,'74                        ' got {}'.format(type(node_conf)))75            if self._weight_fn:76                conf['weight'] = self._weight_fn(**conf)77            # changing the weight of a node trigger a ring update78            if current_conf.get('weight') != conf['weight']:79                conf_changed = True80            self.runtime._nodes[nodename] = conf81        return conf_changed82    def __delitem__(self, nodename):83        """Remove the given node.84        :param nodename: the node name.85        """86        self.runtime._remove_node(nodename)87    remove_node = __delitem__88    def __getitem__(self, key):89        """Returns the instance of the node matching the hashed key.90        :param key: the key to look for.91        """92        return self._get(key, 'instance')93    get_node_instance = __getitem__94    def __setitem__(self, nodename, conf={'weight': 1}):95        """Add the given node with its associated configuration.96        :param nodename: the node name.97        :param conf: the node configuration.98        """99        if self._configure_nodes({nodename: conf}):100            self.runtime._create_ring([(nodename, self._nodes[nodename])])101    add_node = __setitem__102    def _get_pos(self, key):103        """Get the index of the given key in the sorted key list.104        We return the position with the nearest hash based on105        the provided key unless we reach the end of the continuum/ring106        in which case we return the 0 (beginning) index position.107        :param key: the key to hash and look for.108        """109        p = bisect(self.runtime._keys, self.hashi(key))110        if p == len(self.runtime._keys):111            return 0112        else:113            return p...uhashring.py
Source:uhashring.py  
...47        self._hash_fn = self._ring.hash_value48        if weight_fn and not hasattr(weight_fn, '__call__'):49            raise TypeError('`weight_fn` should be a callable function')50        self._weight_fn = weight_fn51        if self._configure_nodes(nodes):52            self._ring._create_ring(self._ring._nodes.iteritems())53    def _configure_nodes(self, nodes):54        if isinstance(nodes, str):55            nodes = [nodes]56        elif not isinstance(nodes, (list, dict)):57            raise ValueError('nodes configuration should be a list or a dict')58        conf_changed = False59        for node in nodes:60            conf = {61                'hostname': node,62                'instance': None,63                'nodename': node,64                'port': None,65                'vnodes': self._default_vnodes,66                'weight': 1,67            }68            current_conf = self._ring._nodes.get(node, {})69            nodename = node70            if not current_conf:71                conf_changed = True72            if isinstance(nodes, dict):73                node_conf = nodes[node]74                if isinstance(node_conf, int):75                    conf['weight'] = node_conf76                elif isinstance(node_conf, dict):77                    for k, v in node_conf.iteritems():78                        if k in conf:79                            conf[k] = v80                            if k in ('nodename', 'vnodes', 'weight'):81                                if current_conf.get(k) != v:82                                    conf_changed = True83                else:84                    raise ValueError('node configuration should be a dict or an int')85            if self._weight_fn:86                conf['weight'] = self._weight_fn(**conf)87            if current_conf.get('weight') != conf['weight']:88                conf_changed = True89            self._ring._nodes[nodename] = conf90        return conf_changed91    def __delitem__(self, nodename):92        self._ring._remove_node(nodename)93    remove_node = __delitem__94    def __getitem__(self, key):95        return self._get(key, 'instance')96    get_node_instance = __getitem__97    def __setitem__(self, nodename, conf={'weight': 1}):98        if self._configure_nodes({nodename: conf}):99            self._ring._create_ring([(nodename, self._nodes[nodename])])100    add_node = __setitem__101    def _get_pos(self, key):102        pos = bisect(self._ring._keys, self._hash_fn(key))103        if pos == len(self._ring._keys):104            return 0105        else:106            return pos107    def _get(self, key, what):108        if not self._ring._ring:109            return None110        pos = self._get_pos(key)111        if what == 'pos':112            return pos...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
