def high_degree_nodes(k, G):
if nx.is_directed(G):
my_degree_function = G.out_degree
else:
my_degree_function = G.degree
# the top k nodes to be returned; initialization with first k elements
H = [(my_degree_function(i), i) for i in G.nodes()[0:k]]
hq.heapify(H)
# iterate through the remaining nodes; add to heap if larger than heap root
for i in G.nodes()[k:]:
if my_degree_function(i) > H[0][0]:
hq.heappushpop(H, (my_degree_function(i), i))
return H
# Generator variant of high_degree_nodes(k, G)
# More time-efficient for range calls if k log k > log V
# Generates _all_ sets of i nodes of highest degree, with 1 <= i <= k
# -> Time complexity: O(V log (V))
# -> Memory complexity: Theta(V)
python类heappushpop()的实例源码
def search_docs(inputs, max_ex=5, opts=None):
"""Given a set of document ids (returned by ranking for a question), search
for top N best matching (by heuristic) paragraphs that contain the answer.
"""
if not opts:
raise RuntimeError('Options dict must be supplied.')
doc_ids, q_tokens, answer = inputs
examples = []
for i, doc_id in enumerate(doc_ids):
for j, paragraph in enumerate(re.split(r'\n+', fetch_text(doc_id))):
found = find_answer(paragraph, q_tokens, answer, opts)
if found:
# Reverse ranking, giving priority to early docs + paragraphs
score = (found[0], -i, -j, random.random())
if len(examples) < max_ex:
heapq.heappush(examples, (score, found[1]))
else:
heapq.heappushpop(examples, (score, found[1]))
return [e[1] for e in examples]
def search_docs(inputs, max_ex=5, opts=None):
"""Given a set of document ids (returned by ranking for a question), search
for top N best matching (by heuristic) paragraphs that contain the answer.
"""
if not opts:
raise RuntimeError('Options dict must be supplied.')
doc_ids, q_tokens, answer = inputs
examples = []
for i, doc_id in enumerate(doc_ids):
for j, paragraph in enumerate(re.split(r'\n+', fetch_text(doc_id))):
found = find_answer(paragraph, q_tokens, answer, opts)
if found:
# Reverse ranking, giving priority to early docs + paragraphs
score = (found[0], -i, -j, random.random())
if len(examples) < max_ex:
heapq.heappush(examples, (score, found[1]))
else:
heapq.heappushpop(examples, (score, found[1]))
return [e[1] for e in examples]
def _addResult(self, test, *args):
try:
name = test.id()
except AttributeError:
name = 'Unknown.unknown'
test_class, test_name = name.rsplit('.', 1)
elapsed = (self._now() - self.start_time).total_seconds()
item = (elapsed, test_class, test_name)
if len(self.slow_tests) >= self.num_slow_tests:
heapq.heappushpop(self.slow_tests, item)
else:
heapq.heappush(self.slow_tests, item)
self.results.setdefault(test_class, [])
self.results[test_class].append((test_name, elapsed) + args)
self.last_time[test_class] = self._now()
self.writeTests()
def _addResult(self, test, *args):
try:
name = test.id()
except AttributeError:
name = 'Unknown.unknown'
test_class, test_name = name.rsplit('.', 1)
elapsed = (self._now() - self.start_time).total_seconds()
item = (elapsed, test_class, test_name)
if len(self.slow_tests) >= self.num_slow_tests:
heapq.heappushpop(self.slow_tests, item)
else:
heapq.heappush(self.slow_tests, item)
self.results.setdefault(test_class, [])
self.results[test_class].append((test_name, elapsed) + args)
self.last_time[test_class] = self._now()
self.writeTests()
def _addResult(self, test, *args):
try:
name = test.id()
except AttributeError:
name = 'Unknown.unknown'
test_class, test_name = name.rsplit('.', 1)
elapsed = (self._now() - self.start_time).total_seconds()
item = (elapsed, test_class, test_name)
if len(self.slow_tests) >= self.num_slow_tests:
heapq.heappushpop(self.slow_tests, item)
else:
heapq.heappush(self.slow_tests, item)
self.results.setdefault(test_class, [])
self.results[test_class].append((test_name, elapsed) + args)
self.last_time[test_class] = self._now()
self.writeTests()
colorizer.py 文件源码
项目:Trusted-Platform-Module-nova
作者: BU-NU-CLOUD-SP16
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def _addResult(self, test, *args):
try:
name = test.id()
except AttributeError:
name = 'Unknown.unknown'
test_class, test_name = name.rsplit('.', 1)
elapsed = (self._now() - self.start_time).total_seconds()
item = (elapsed, test_class, test_name)
if len(self.slow_tests) >= self.num_slow_tests:
heapq.heappushpop(self.slow_tests, item)
else:
heapq.heappush(self.slow_tests, item)
self.results.setdefault(test_class, [])
self.results[test_class].append((test_name, elapsed) + args)
self.last_time[test_class] = self._now()
self.writeTests()
def call_plugins(self, queue_name, time, *args):
args = (time,) + args
queue = self.plugin_queues[queue_name]
if len(queue) == 0:
return
while queue[0][0] <= time:
plugin = queue[0][2]
getattr(plugin, queue_name)(*args)
for trigger in plugin.trigger_interval:
if trigger[1] == queue_name:
interval = trigger[0]
new_item = (time + interval, queue[0][1], plugin)
heapq.heappushpop(queue, new_item)
def add(self, item):
if item is None:
return
if len(self._heap) < self._n:
heapq.heappush(self._heap, item)
else:
heapq.heappushpop(self._heap, item)
def call_plugins(self, queue_name, time, *args):
args = (time,) + args
queue = self.plugin_queues[queue_name]
if len(queue) == 0:
return
while queue[0][0] <= time:
plugin = queue[0][2]
getattr(plugin, queue_name)(*args)
for trigger in plugin.trigger_interval:
if trigger[1] == queue_name:
interval = trigger[0]
new_item = (time + interval, queue[0][1], plugin)
heapq.heappushpop(queue, new_item)
def push(self, x):
"""Pushes a new element."""
assert self._data is not None
if len(self._data) < self._n:
heapq.heappush(self._data, x)
else:
heapq.heappushpop(self._data, x)
def push(self, x):
"""Pushes a new element."""
assert self._data is not None
if len(self._data) < self._n:
heapq.heappush(self._data, x)
else:
heapq.heappushpop(self._data, x)
def call_plugins(self, queue_name, time, *args):
args = (time,) + args
queue = self.plugin_queues[queue_name]
if len(queue) == 0:
return
while queue[0][0] <= time:
plugin = queue[0][2]
getattr(plugin, queue_name)(*args)
for trigger in plugin.trigger_interval:
if trigger[1] == queue_name:
interval = trigger[0]
new_item = (time + interval, queue[0][1], plugin)
heapq.heappushpop(queue, new_item)
def call_plugins(self, queue_name, time, *args):
args = (time,) + args
queue = self.plugin_queues[queue_name]
if len(queue) == 0:
return
while queue[0][0] <= time:
plugin = queue[0][2]
getattr(plugin, queue_name)(*args)
for trigger in plugin.trigger_interval:
if trigger[1] == queue_name:
interval = trigger[0]
new_item = (time + interval, queue[0][1], plugin)
heapq.heappushpop(queue, new_item)
def push(self, x):
"""Pushes a new element."""
assert self._data is not None
if len(self._data) < self._n:
heapq.heappush(self._data, x)
else:
heapq.heappushpop(self._data, x)
def push(self, x):
"""Pushes a new element."""
assert self._data is not None
if len(self._data) < self._n:
heapq.heappush(self._data, x)
else:
heapq.heappushpop(self._data, x)
def call_plugins(self, queue_name, time, *args):
args = (time,) + args
queue = self.plugin_queues[queue_name]
if len(queue) == 0:
return
while queue[0][0] <= time:
plugin = queue[0][2]
getattr(plugin, queue_name)(*args)
for trigger in plugin.trigger_interval:
if trigger[1] == queue_name:
interval = trigger[0]
new_item = (time + interval, queue[0][1], plugin)
heapq.heappushpop(queue, new_item)
def nsmallest(n, a):
h = []
for e in a[:n]:
heapq.heappush(h, -e) # we use a max heap but push -1 * e
for e in a[n:]:
heapq.heappushpop(h, -e)
return -heapq.heappop(h)
def push(self, x):
"""Pushes a new element."""
assert self._data is not None
if len(self._data) < self._n:
heapq.heappush(self._data, x)
else:
heapq.heappushpop(self._data, x)
def push(self, x):
"""Pushes a new element."""
assert self._data is not None
if len(self._data) < self._n:
heapq.heappush(self._data, x)
else:
heapq.heappushpop(self._data, x)
def shuf_file(f, shuf_win):
heap = []
for line in f:
key = hash(line)
if len(heap) < shuf_win:
heapq.heappush(heap, (key, line))
else:
_, out = heapq.heappushpop(heap, (key, line))
yield out
while len(heap) > 0:
_, out = heapq.heappop(heap)
yield out
def push(self, x):
"""Pushes a new element."""
assert self._data is not None
if len(self._data) < self._n:
heapq.heappush(self._data, x)
else:
heapq.heappushpop(self._data, x)
def push(self, x):
"""Pushes a new element."""
assert self._data is not None
if len(self._data) < self._n:
heapq.heappush(self._data, x)
else:
heapq.heappushpop(self._data, x)
215. Kth Largest Element in an Array.py 文件源码
项目:py-leetcode
作者: coolmich
项目源码
文件源码
阅读 35
收藏 0
点赞 0
评论 0
def findKthLargest(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
# heap = []
# for num in nums:
# if len(heap) < k:
# heapq.heappush(heap, num)
# else:
# heapq.heappushpop(heap, num)
# return heap[0]
def select_sort(s, e):
l, r = s, e - 1
while l < r:
while l < r and nums[l] < nums[e]: l += 1
while l < r and nums[r] >= nums[e]: r -= 1
nums[l], nums[r] = nums[r], nums[l]
nums[l], nums[e] = nums[e], nums[l]
return l
s, e = 0, len(nums)-1
i, k = select_sort(s, e), len(nums)-k
print i
print nums
while i != k:
# print '{},{}'.format(i, k)
if i < k: s = i + 1
else: e = i - 1
i = select_sort(s, e)
return nums[i]
def call_plugins(self, queue_name, time, *args):
args = (time,) + args
queue = self.plugin_queues[queue_name]
if len(queue) == 0:
return
while queue[0][0] <= time:
plugin = queue[0][2]
getattr(plugin, queue_name)(*args)
for trigger in plugin.trigger_interval:
if trigger[1] == queue_name:
interval = trigger[0]
new_item = (time + interval, queue[0][1], plugin)
heapq.heappushpop(queue, new_item)
def push(self, x):
"""Pushes a new element."""
assert self._data is not None
if len(self._data) < self._n:
heapq.heappush(self._data, x)
else:
heapq.heappushpop(self._data, x)
def push(self, x):
"""Pushes a new element."""
assert self._data is not None
if len(self._data) < self._n:
heapq.heappush(self._data, x)
else:
heapq.heappushpop(self._data, x)
def call_plugins(self, queue_name, time, *args):
args = (time,) + args
queue = self.plugin_queues[queue_name]
if len(queue) == 0:
return
while queue[0][0] <= time:
plugin = queue[0][2]
getattr(plugin, queue_name)(*args)
for trigger in plugin.trigger_interval:
if trigger[1] == queue_name:
interval = trigger[0]
new_item = (time + interval, queue[0][1], plugin)
heapq.heappushpop(queue, new_item)
def push(self, x):
"""Pushes a new element."""
assert self._data is not None
if len(self._data) < self._n:
heapq.heappush(self._data, x)
else:
heapq.heappushpop(self._data, x)
def push(self, x):
"""Pushes a new element."""
assert self._data is not None
if len(self._data) < self._n:
heapq.heappush(self._data, x)
else:
heapq.heappushpop(self._data, x)