Best Python code snippet using yandex-tank
GLib.py
Source:GLib.py  
...157    def __hash__(self):158        # We're not using just hash(self.unpack()) because otherwise we'll have159        # hash collisions between the same content in different variant types,160        # which will cause a performance issue in set/dict/etc.161        return hash((self.get_type_string(), self.unpack()))162    def unpack(self):163        '''Decompose a GVariant into a native Python object.'''164        LEAF_ACCESSORS = {165            'b': self.get_boolean,166            'y': self.get_byte,167            'n': self.get_int16,168            'q': self.get_uint16,169            'i': self.get_int32,170            'u': self.get_uint32,171            'x': self.get_int64,172            't': self.get_uint64,173            'h': self.get_handle,174            'd': self.get_double,175            's': self.get_string,176            'o': self.get_string, # object path177            'g': self.get_string, # signature178        }179        # simple values180        la = LEAF_ACCESSORS.get(self.get_type_string())181        if la:182            return la()183        # tuple184        if self.get_type_string().startswith('('):185            res = [self.get_child_value(i).unpack() 186                    for i in range(self.n_children())]187            return tuple(res)188        # dictionary189        if self.get_type_string().startswith('a{'):190            res = {}191            for i in range(self.n_children()):192                v = self.get_child_value(i)193                res[v.get_child_value(0).unpack()] = v.get_child_value(1).unpack()194            return res195        # array196        if self.get_type_string().startswith('a'):197            return [self.get_child_value(i).unpack() 198                    for i in range(self.n_children())]199        # variant (just unbox transparently)200        if self.get_type_string().startswith('v'):201            return self.get_variant().unpack()202        raise NotImplementedError('unsupported GVariant type ' + self.get_type_string())203    @classmethod204    def split_signature(klass, signature):205        '''Return a list of the element signatures of the topmost signature tuple.206        If the signature is not a tuple, it returns one element with the entire207        signature. If the signature is an empty tuple, the result is [].208        209        This is useful for e. g. iterating over method parameters which are210        passed as a single Variant.211        '''212        if signature == '()':213            return []214        if not signature.startswith('('):215            return [signature]216        result = []217        head = ''218        tail = signature[1:-1] # eat the surrounding ( )219        while tail:220            c = tail[0]221            head += c222            tail = tail[1:]223            if c in ('m', 'a'):224                # prefixes, keep collecting225                continue226            if c in ('(', '{'):227                # consume until corresponding )/}228                level = 1229                up = c230                if up == '(':231                    down = ')'232                else:233                    down = '}'234                while level > 0:235                    c = tail[0]236                    head += c237                    tail = tail[1:]238                    if c == up:239                        level += 1240                    elif c == down:241                        level -= 1242            # otherwise we have a simple type243            result.append(head)244            head = ''245        return result246    #247    # Pythonic iterators248    #249    def __len__(self):250        if self.get_type_string() in ['s', 'o', 'g']:251            return len(self.get_string())252        # Array, dict, tuple253        if self.get_type_string().startswith('a') or self.get_type_string().startswith('('):254            return self.n_children()255        raise TypeError('GVariant type %s does not have a length' % self.get_type_string())256    def __getitem__(self, key):257        # dict258        if self.get_type_string().startswith('a{'):259            try:260                val = self.lookup_value(key, variant_type_from_string('*'))261                if val is None:262                    raise KeyError(key)263                return val.unpack()264            except TypeError:265                # lookup_value() only works for string keys, which is certainly266                # the common case; we have to do painful iteration for other267                # key types268                for i in range(self.n_children()):269                    v = self.get_child_value(i)270                    if v.get_child_value(0).unpack() == key:271                        return v.get_child_value(1).unpack()272                raise KeyError(key)273        # array/tuple274        if self.get_type_string().startswith('a') or self.get_type_string().startswith('('):275            key = int(key)276            if key < 0:277                key = self.n_children() + key278            if key < 0 or key >= self.n_children():279                raise IndexError('list index out of range')280            return self.get_child_value(key).unpack()281        # string282        if self.get_type_string() in ['s', 'o', 'g']:283            return self.get_string().__getitem__(key)284        raise TypeError('GVariant type %s is not a container' % self.get_type_string())285    #286    # Pythonic bool operations287    #288    def __nonzero__(self):289        return self.__bool__()290    def __bool__(self):291        if self.get_type_string() in ['y', 'n', 'q', 'i', 'u', 'x', 't', 'h', 'd']:292            return self.unpack() != 0293        if self.get_type_string() in ['b']:294            return self.get_boolean()295        if self.get_type_string() in ['s', 'o', 'g']:296            return len(self.get_string()) != 0297        # Array, dict, tuple298        if self.get_type_string().startswith('a') or self.get_type_string().startswith('('):299            return self.n_children() != 0300        if self.get_type_string() in ['v']:301            # unpack works recursively, hence bool also works recursively302            return bool(self.unpack())303        # Everything else is True304        return True305    def keys(self):306        if not self.get_type_string().startswith('a{'):307            return TypeError, 'GVariant type %s is not a dictionary' % self.get_type_string()308        res = []309        for i in range(self.n_children()):310            v = self.get_child_value(i)311            res.append(v.get_child_value(0).unpack())312        return res313@classmethod314def new_tuple(cls, *elements):315    return variant_new_tuple(elements)316def get_string(self):317    value, length = GLib.Variant.get_string(self)318    return value319setattr(Variant, 'new_tuple', new_tuple)320setattr(Variant, 'get_string', get_string)321__all__.append('Variant')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!!
