当前位置: 首页>>代码示例>>Python>>正文


Python MultiDict.iteritems方法代码示例

本文整理汇总了Python中paste.util.multidict.MultiDict.iteritems方法的典型用法代码示例。如果您正苦于以下问题:Python MultiDict.iteritems方法的具体用法?Python MultiDict.iteritems怎么用?Python MultiDict.iteritems使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在paste.util.multidict.MultiDict的用法示例。


在下文中一共展示了MultiDict.iteritems方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: MimeHeaders

# 需要导入模块: from paste.util.multidict import MultiDict [as 别名]
# 或者: from paste.util.multidict.MultiDict import iteritems [as 别名]
class MimeHeaders(object):
    """Dictionary-like object that preserves the order and
    supports multiple values for the same key, knows
    whether it has been changed after the creation
    """

    def __init__(self, items=()):
        self._v = MultiDict([(normalize(key), remove_newlines(val))
                             for (key, val) in items])
        self.changed = False

    def __getitem__(self, key):
        return self._v.get(normalize(key), None)

    def __len__(self):
        return len(self._v)

    def __iter__(self):
        return iter(self._v)

    def __contains__(self, key):
        return normalize(key) in self._v

    def __setitem__(self, key, value):
        self._v[normalize(key)] = remove_newlines(value)
        self.changed = True

    def __delitem__(self, key):
        del self._v[normalize(key)]
        self.changed = True

    def __nonzero__(self):
        return len(self._v) > 0

    def prepend(self, key, value):
        self._v._items.insert(0, (normalize(key), remove_newlines(value)))
        self.changed = True

    def add(self, key, value):
        """Adds header without changing the
        existing headers with same name"""

        self._v.add(normalize(key), remove_newlines(value))
        self.changed = True

    def keys(self):
        """
        Returns the keys. (message header names)
        It remembers the order in which they were added, what
        is really important
        """
        return self._v.keys()

    def transform(self, fn):
        """Accepts a function, getting a key, val and returning
        a new pair of key, val and applies the function to all
        header, value pairs in the message.
        """

        changed = [False]

        def tracking_fn(key, val):
            new_key, new_val = fn(key, val)
            if new_val != val or new_key != key:
                changed[0] = True
            return new_key, new_val

        v = MultiDict(tracking_fn(key, val) for key, val in self._v.iteritems())
        if changed[0]:
            self._v = v
            self.changed = True

    def items(self):
        """
        Returns header,val pairs in the preserved order.
        """
        return list(self.iteritems())

    def iteritems(self):
        """
        Returns iterator header,val pairs in the preserved order.
        """
        return self._v.iteritems()

    def get(self, key, default=None):
        """
        Returns header value (case-insensitive).
        """
        return self._v.get(normalize(key), default)

    def getall(self, key):
        """
        Returns all header values by the given header name
        (case-insensitive)
        """
        return self._v.getall(normalize(key))

    def have_changed(self):
        """Tells whether someone has altered the headers
        after creation"""
#.........这里部分代码省略.........
开发者ID:Appono,项目名称:flanker,代码行数:103,代码来源:headers.py


注:本文中的paste.util.multidict.MultiDict.iteritems方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。