python类keys()的实例源码

odict.py 文件源码 项目:autoinjection 作者: ChengWiLL 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def setkeys(self, keys):
        """
        ``setkeys`` all ows you to pass in a new list of keys which will
        replace the current set. This must contain the same set of keys, but
        need not be in the same order.

        If you pass in new keys that don't match, a ``KeyError`` will be
        raised.

        >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
        >>> d.keys()
        [1, 3, 2]
        >>> d.setkeys((1, 2, 3))
        >>> d
        OrderedDict([(1, 3), (2, 1), (3, 2)])
        >>> d.setkeys(['a', 'b', 'c'])
        Traceback (most recent call last):
        KeyError: 'Keylist is not the same as current keylist.'
        """
        # FIXME: Efficiency? (use set for Python 2.4 :-)
        # NOTE: list(keys) rather than keys[:] because keys[:] returns
        #   a tuple, if keys is a tuple.
        kcopy = list(keys)
        kcopy.sort()
        self._sequence.sort()
        if kcopy != self._sequence:
            raise KeyError('Keylist is not the same as current keylist.')
        # NOTE: This makes the _sequence attribute a new object, instead
        #       of changing it in place.
        # FIXME: efficiency?
        self._sequence = list(keys)
odict.py 文件源码 项目:autoinjection 作者: ChengWiLL 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def __call__(self):
        """Pretend to be the keys method."""
        return self._main._keys()
odict.py 文件源码 项目:autoinjection 作者: ChengWiLL 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def __setitem__(self, index, name):
        """
        You cannot assign to keys, but you can do slice assignment to re-order
        them.

        You can only do slice assignment if the new set of keys is a reordering
        of the original set.
        """
        if isinstance(index, types.SliceType):
            # FIXME: efficiency?
            # check length is the same
            indexes = range(len(self._main._sequence))[index]
            if len(indexes) != len(name):
                raise ValueError('attempt to assign sequence of size %s '
                    'to slice of size %s' % (len(name), len(indexes)))
            # check they are the same keys
            # FIXME: Use set
            old_keys = self._main._sequence[index]
            new_keys = list(name)
            old_keys.sort()
            new_keys.sort()
            if old_keys != new_keys:
                raise KeyError('Keylist is not the same as current keylist.')
            orig_vals = [self._main[k] for k in name]
            del self._main[index]
            vals = zip(indexes, name, orig_vals)
            vals.sort()
            for i, k, v in vals:
                if self._main.strict and k in self._main:
                    raise ValueError('slice assignment must be from '
                        'unique keys')
                self._main.insert(i, k, v)
        else:
            raise ValueError('Cannot assign to keys')

    ### following methods pinched from UserList and adapted ###
odict.py 文件源码 项目:autoinjection 作者: ChengWiLL 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def __delitem__(self, i): raise TypeError('Can\'t delete items from keys')
odict.py 文件源码 项目:autoinjection 作者: ChengWiLL 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def __iadd__(self, other): raise TypeError('Can\'t add in place to keys')
odict.py 文件源码 项目:autoinjection 作者: ChengWiLL 项目源码 文件源码 阅读 15 收藏 0 点赞 0 评论 0
def __imul__(self, n): raise TypeError('Can\'t multiply keys in place')
odict.py 文件源码 项目:autoinjection 作者: ChengWiLL 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def append(self, item): raise TypeError('Can\'t append items to keys')
odict.py 文件源码 项目:autoinjection 作者: ChengWiLL 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def insert(self, i, item): raise TypeError('Can\'t insert items into keys')
odict.py 文件源码 项目:autoinjection 作者: ChengWiLL 项目源码 文件源码 阅读 15 收藏 0 点赞 0 评论 0
def remove(self, item): raise TypeError('Can\'t remove items from keys')
odict.py 文件源码 项目:autoinjection 作者: ChengWiLL 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def extend(self, other): raise TypeError('Can\'t extend keys')
odict.py 文件源码 项目:autoinjection 作者: ChengWiLL 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def __setitem__(self, index, item):
        """Set item at position i to item."""
        if isinstance(index, types.SliceType):
            # NOTE: item must be an iterable (list of tuples)
            self._main[index] = OrderedDict(item)
        else:
            # FIXME: Does this raise a sensible error?
            orig = self._main.keys[index]
            key, value = item
            if self._main.strict and key in self and (key != orig):
                raise ValueError('slice assignment must be from '
                        'unique keys')
            # delete the current one
            del self._main[self._main._sequence[index]]
            self._main.insert(index, key, value)
odict.py 文件源码 项目:autoinjection 作者: ChengWiLL 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def extend(self, other):
        # FIXME: is only a true extend if none of the keys already present
        for item in other:
            key, value = item
            self._main[key] = value
odict.py 文件源码 项目:autoinjection 作者: ChengWiLL 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def __setattr__(self, name, value):
        """Protect keys, items, and values."""
        if not '_att_dict' in self.__dict__:
            object.__setattr__(self, name, value)
        else:
            try:
                fun = self._att_dict[name]
            except KeyError:
                OrderedDict.__setattr__(self, name, value)
            else:
                fun(value)
odict.py 文件源码 项目:PocHunter 作者: DavexPro 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def __init__(self, init_val=(), strict=False):
        """
        Create a new ordered dictionary. Cannot init from a normal dict,
        nor from kwargs, since items order is undefined in those cases.

        If the ``strict`` keyword argument is ``True`` (``False`` is the
        default) then when doing slice assignment - the ``OrderedDict`` you are
        assigning from *must not* contain any keys in the remaining dict.

        >>> OrderedDict()
        OrderedDict([])
        >>> OrderedDict({1: 1})
        Traceback (most recent call last):
        TypeError: undefined order, cannot get items from dict
        >>> OrderedDict({1: 1}.items())
        OrderedDict([(1, 1)])
        >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
        >>> d
        OrderedDict([(1, 3), (3, 2), (2, 1)])
        >>> OrderedDict(d)
        OrderedDict([(1, 3), (3, 2), (2, 1)])
        """
        self.strict = strict
        dict.__init__(self)
        if isinstance(init_val, OrderedDict):
            self._sequence = init_val.keys()
            dict.update(self, init_val)
        elif isinstance(init_val, dict):
            # we lose compatibility with other ordered dict types this way
            raise TypeError('undefined order, cannot get items from dict')
        else:
            self._sequence = []
            self.update(init_val)

### Special methods ###
odict.py 文件源码 项目:PocHunter 作者: DavexPro 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def __delitem__(self, key):
        """
        >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
        >>> del d[3]
        >>> d
        OrderedDict([(1, 3), (2, 1)])
        >>> del d[3]
        Traceback (most recent call last):
        KeyError: 3
        >>> d[3] = 2
        >>> d
        OrderedDict([(1, 3), (2, 1), (3, 2)])
        >>> del d[0:1]
        >>> d
        OrderedDict([(2, 1), (3, 2)])
        """
        if isinstance(key, types.SliceType):
            # FIXME: efficiency?
            keys = self._sequence[key]
            for entry in keys:
                dict.__delitem__(self, entry)
            del self._sequence[key]
        else:
            # do the dict.__delitem__ *first* as it raises
            # the more appropriate error
            dict.__delitem__(self, key)
            self._sequence.remove(key)
odict.py 文件源码 项目:PocHunter 作者: DavexPro 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def __setattr__(self, name, value):
        """
        Implemented so that accesses to ``sequence`` raise a warning and are
        diverted to the new ``setkeys`` method.
        """
        if name == 'sequence':
            warnings.warn('Use of the sequence attribute is deprecated.'
                ' Use the keys method instead.', DeprecationWarning)
            # NOTE: doesn't return anything
            self.setkeys(value)
        else:
            # FIXME: do we want to allow arbitrary setting of attributes?
            #   Or do we want to manage it?
            object.__setattr__(self, name, value)
odict.py 文件源码 项目:PocHunter 作者: DavexPro 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def setkeys(self, keys):
        """
        ``setkeys`` all ows you to pass in a new list of keys which will
        replace the current set. This must contain the same set of keys, but
        need not be in the same order.

        If you pass in new keys that don't match, a ``KeyError`` will be
        raised.

        >>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
        >>> d.keys()
        [1, 3, 2]
        >>> d.setkeys((1, 2, 3))
        >>> d
        OrderedDict([(1, 3), (2, 1), (3, 2)])
        >>> d.setkeys(['a', 'b', 'c'])
        Traceback (most recent call last):
        KeyError: 'Keylist is not the same as current keylist.'
        """
        # FIXME: Efficiency? (use set for Python 2.4 :-)
        # NOTE: list(keys) rather than keys[:] because keys[:] returns
        #   a tuple, if keys is a tuple.
        kcopy = list(keys)
        kcopy.sort()
        self._sequence.sort()
        if kcopy != self._sequence:
            raise KeyError('Keylist is not the same as current keylist.')
        # NOTE: This makes the _sequence attribute a new object, instead
        #       of changing it in place.
        # FIXME: efficiency?
        self._sequence = list(keys)
odict.py 文件源码 项目:PocHunter 作者: DavexPro 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def __call__(self):
        """Pretend to be the keys method."""
        return self._main._keys()
odict.py 文件源码 项目:PocHunter 作者: DavexPro 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def __setitem__(self, index, name):
        """
        You cannot assign to keys, but you can do slice assignment to re-order
        them.

        You can only do slice assignment if the new set of keys is a reordering
        of the original set.
        """
        if isinstance(index, types.SliceType):
            # FIXME: efficiency?
            # check length is the same
            indexes = range(len(self._main._sequence))[index]
            if len(indexes) != len(name):
                raise ValueError('attempt to assign sequence of size %s '
                    'to slice of size %s' % (len(name), len(indexes)))
            # check they are the same keys
            # FIXME: Use set
            old_keys = self._main._sequence[index]
            new_keys = list(name)
            old_keys.sort()
            new_keys.sort()
            if old_keys != new_keys:
                raise KeyError('Keylist is not the same as current keylist.')
            orig_vals = [self._main[k] for k in name]
            del self._main[index]
            vals = zip(indexes, name, orig_vals)
            vals.sort()
            for i, k, v in vals:
                if self._main.strict and k in self._main:
                    raise ValueError('slice assignment must be from '
                        'unique keys')
                self._main.insert(i, k, v)
        else:
            raise ValueError('Cannot assign to keys')

    ### following methods pinched from UserList and adapted ###
odict.py 文件源码 项目:PocHunter 作者: DavexPro 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def __delitem__(self, i): raise TypeError('Can\'t delete items from keys')


问题


面经


文章

微信
公众号

扫码关注公众号