本文整理匯總了Python中sortedcontainers.SortedList.as_list方法的典型用法代碼示例。如果您正苦於以下問題:Python SortedList.as_list方法的具體用法?Python SortedList.as_list怎麽用?Python SortedList.as_list使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sortedcontainers.SortedList
的用法示例。
在下文中一共展示了SortedList.as_list方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: Log
# 需要導入模塊: from sortedcontainers import SortedList [as 別名]
# 或者: from sortedcontainers.SortedList import as_list [as 別名]
class Log(object):
"""Keep a random sample of stuff seen so far. Based on Dr. Menzies'
implementation."""
MAX_SIZE = 256
def __init__(self, inits=None, label=None, max_size=MAX_SIZE):
self._cache = SortedList()
self._report = None
self.label = label or ''
self._n = 0
self.max_size = max_size
self._valid_statistics = False
self._invalidate_statistics()
if inits:
map(self.__iadd__, inits)
def random_index(self):
return base.random_index(self._cache)
@classmethod
def wrap(cls, x, max_size=MAX_SIZE):
if isinstance(x, cls):
return x
return cls(inits=x, max_size=max_size)
def __len__(self):
return len(self._cache)
def extend(self, xs):
if not isinstance(xs, collections.Iterable):
raise TypeError()
map(self.__iadd__, xs)
def __iadd__(self, x):
if x is None:
return x
self._n += 1
if issubclass(x.__class__, Log):
map(self.__iadd__, x._cache)
return self
changed = False
# if cache has room, add item
if self.max_size is None or len(self._cache) < self.max_size:
changed = True
self._cache.add(x)
# cache is full: maybe replace an old item
else:
# items less likely to be replaced later in the run:
# leads to uniform sample of entire run
if random.random() <= self.max_size / len(self):
changed = True
self._cache.remove(random.choice(self._cache))
self._cache.add(x)
if changed:
self._invalidate_statistics()
self._change(x)
return self
def __add__(self, x, max_size=MAX_SIZE):
inits = itertools.chain(self._cache, x._cache)
return self.__class__(inits=inits, max_size=max_size)
def any(self):
return random.choice(self._cache)
def report(self):
if self._report is None:
self._report = self._generate_report()
return self._report
def setup(self):
raise NotImplementedError()
def as_list(self):
return self._cache.as_list()
def _invalidate_statistics(self):
'''
default implementation. if _valid_statistics is something other than
a boolean, reimplement!
'''
self._valid_statistics = False
def ish(self, *args, **kwargs):
raise NotImplementedError()
def _change(self, x):
'''
override to add incremental updating functionality
'''
pass
def _prepare_data(self):
#.........這裏部分代碼省略.........