當前位置: 首頁>>代碼示例>>Python>>正文


Python Counter.update方法代碼示例

本文整理匯總了Python中collections.Counter.update方法的典型用法代碼示例。如果您正苦於以下問題:Python Counter.update方法的具體用法?Python Counter.update怎麽用?Python Counter.update使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在collections.Counter的用法示例。


在下文中一共展示了Counter.update方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: update

# 需要導入模塊: from collections import Counter [as 別名]
# 或者: from collections.Counter import update [as 別名]
    def update(self, *args, **kwargs):
        """D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.
        If E present, does:  for k in E: D[k] = E[k]
        This is followed by: for k in F: D[k] = F[k]

        **Example:**

        >>> e = Exponents()
        >>> e.update(a=1)
        >>> e.update(dict(b=2), c=3)
        >>> e.update('1/d')
        >>> e # doctest: +SKIP
        Exponents({'a': 1, 'b': 2, 'c': 3, 'd': -1})

        .. testcleanup::
           >>> assert e == dict(a=1, b=2, c=3, d=-1)
        """
        try:
            # Assume args[0] is a string.
            arg = args[0].replace(' ', '')  # Remove spaces.
        except (IndexError, AttributeError):
            Counter.update(self, *args, **kwargs)
        else:
            if len(args) > 1:
                raise TypeError("update expected at most 1 arguments, got %i"
                                % len(args))
            Counter.update(self, Exponents.fromstr(arg), **kwargs)
開發者ID:joewa,項目名稱:natu,代碼行數:29,代碼來源:exponents.py

示例2: _HitLog

# 需要導入模塊: from collections import Counter [as 別名]
# 或者: from collections.Counter import update [as 別名]
class _HitLog(AbstractLog):
    # This is a storage class that keep track of the hits that have
    # occurred over a given duration.
    # This particular implementation keeps track of hits in-memory.
    def __init__(self, duration, _): # last argument is resource (or None), but it is unused.
        self._hits = []
        self._delta = duration if isinstance(duration, timedelta) \
                               else timedelta(seconds=duration)
        self._thread_lock = Lock()
        self._counter = PyCounter()

    def _prune(self):
        if self._delta.total_seconds() < 0:
            # negative seconds means keep everything.
            return 

        now = datetime.now()
        with self._thread_lock:
            while self._hits and (now - self._hits[0][0]) > self._delta:
                time, pickled_counter_keys = heapq.heappop(self._hits)
                self._counter.subtract(pickle.loads(pickled_counter_keys))

    def _generate_counter_keys(self, partitions):
        sub_keys = chain.from_iterable(
            combinations(partitions, r) for r in range(1, len(partitions)+1)
        )

        for key_list in sub_keys:
            counter_key = tuple(sorted(map(lambda k: (k, partitions[k]), key_list)))
            yield counter_key

    def track(self, partitions):
        now = datetime.now()

        with self._thread_lock:
            counter_keys = tuple(self._generate_counter_keys(partitions))
            heapq.heappush(self._hits, (now, pickle.dumps(counter_keys)))
            self._counter.update(counter_keys)

    def count(self, **partitions):
        self._prune()

        if not partitions:
            return len(self._hits)

        else:
            counter_key = tuple(sorted(partitions.items()))
            return self._counter[counter_key]

    def __add__(self, other):
        if isinstance(other, _HitLog):
            if self._delta != other._delta:
                return NotImplemented
            else:
                new_log = _HitLog(self._delta, None)

                new_log._hits.extend(self._hits)
                new_log._hits.extend(other._hits)
                heapq.heapify(new_log._hits)

                new_log._counter.update(self._counter)
                new_log._counter.update(other._counter)
                
                return new_log

        else:
            return NotImplemented

    def __iter__(self):
        ascending = heapq.nsmallest(self.count(), self._hits)
        for time, partitions in ascending:
            yield Hit(time, partitions)

    def __len__(self):
        return self.count()

    def __repr__(self):
        return "HitLog({})".format(self.count())
開發者ID:geniphi,項目名稱:findig,代碼行數:80,代碼來源:counter.py

示例3: update

# 需要導入模塊: from collections import Counter [as 別名]
# 或者: from collections.Counter import update [as 別名]
 def update(self, *args, **kwargs):
     Counter.update(self, *args, **kwargs)
     self.clean()
開發者ID:speezepearson,項目名稱:units,代碼行數:5,代碼來源:dimensions.py


注:本文中的collections.Counter.update方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。