本文整理汇总了Python中queue.PriorityQueue.full方法的典型用法代码示例。如果您正苦于以下问题:Python PriorityQueue.full方法的具体用法?Python PriorityQueue.full怎么用?Python PriorityQueue.full使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类queue.PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.full方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from queue import PriorityQueue [as 别名]
# 或者: from queue.PriorityQueue import full [as 别名]
class KthLargestV1:
def __init__(self, k: int, nums: List[int]):
self.pq = PriorityQueue(maxsize=k)
for _, num in enumerate(nums):
if self.pq.full():
q_top = self.pq.get()
if q_top >= num:
self.pq.put(q_top)
else:
self.pq.put(num)
else:
self.pq.put(num)
def add(self, val: int) -> int:
if self.pq.full():
q_top = self.pq.get()
if q_top >= val:
result = q_top
self.pq.put(q_top)
else:
self.pq.put(val)
result = self.pq.get()
self.pq.put(result)
else:
self.pq.put(val)
result = self.pq.get()
self.pq.put(result)
return result
示例2: compute_BM25
# 需要导入模块: from queue import PriorityQueue [as 别名]
# 或者: from queue.PriorityQueue import full [as 别名]
def compute_BM25(st_terms, index, lenindex):
#compute idfs
max_urls = len(links)
idfs = [log(max_urls / lenindex[i]) for i in range(len(st_terms))]
#compute BM25
resdocs = PriorityQueue(101)
for i in range(len(index)):
doc = loaddoc(index[i])
bm = -sum([BM25(st_terms[j], doc, idfs[j]) for j in range(len(st_terms))])
if resdocs.full():
resdocs.get()
resdocs.put((bm, [index[i], doc]))
res = []
while not resdocs.empty():
res.append(list(resdocs.get()))
result = [[-i[0], i[1][0], i[1][1]] for i in res[:101]]
return result, idfs
示例3: Precinct
# 需要导入模块: from queue import PriorityQueue [as 别名]
# 或者: from queue.PriorityQueue import full [as 别名]
class Precinct(object):
'''
Represents a precinct with N number of booths. Capable of
adding voters to and removing (in increasing order of departure time)
voters from voting booths.
Also contains an attribute that can track voters who are currently
occupying booths and determine which voters should be departing next.
Note each voter's priority is its departure time.
(voters with earlier departure times get removed first)
'''
def __init__(self, num_booths):
'''
Constructs the precinct.
Inputs:
num_booths: max number of booths for voters. If full,
then voters are going to have to wait.
'''
self.booths = PriorityQueue(maxsize = num_booths)
@property
def booths(self):
return self._booths
@booths.setter
def booths(self, booths):
self._booths = booths
def add_voter(self, departure_time, voter):
'''
Adds a voter to one of the booths. Note that the priority
parameter in queue.put function is the departure_time
of the voter
Inputs:
voter: a voter to be added.
'''
self.booths.put((departure_time, voter))
def remove_voter(self):
'''
Removes the voter from the booths PriorityQueue by the earliest
departure_time
return:
tuple of (time voter was removed, the voter that was removed)
'''
return self.booths.get()
def booths_full(self):
'''
Returns true if all the booths are occupied.
'''
return self.booths.full()
def booths_is_empty(self):
'''
Returns true if all the booths are empty.
'''
return self.booths.empty()
def __str__(self):
return str(self.booths)
示例4: P2PProtocol
# 需要导入模块: from queue import PriorityQueue [as 别名]
# 或者: from queue.PriorityQueue import full [as 别名]
#.........这里部分代码省略.........
for msg in self._parse_buffer(read_bytes):
self._observable.notify(msg)
if read_bytes[0]:
p2p_ack = qrl_pb2.P2PAcknowledgement(bytes_processed=read_bytes[0])
msg = qrllegacy_pb2.LegacyMessage(func_name=qrllegacy_pb2.LegacyMessage.P2P_ACK,
p2pAckData=p2p_ack)
self.send(msg)
def send_next(self):
if self.bytes_sent < config.dev.max_bytes_out:
outgoing_bytes = self.get_bytes_from_q()
if outgoing_bytes:
self.bytes_sent += len(outgoing_bytes)
self.transport.write(outgoing_bytes)
def get_bytes_from_q(self):
outgoing_bytes = b''
while not self.outgoing_queue.empty():
outgoing_msg = self.outgoing_queue.get()[2]
if not outgoing_msg.is_expired():
wrapped_message = self._wrap_message(outgoing_msg.message)
if len(wrapped_message) + len(outgoing_bytes) > config.dev.max_bytes_out:
self.outgoing_queue.put((outgoing_msg.priority, outgoing_msg.timestamp, outgoing_msg))
break
outgoing_bytes += wrapped_message
return outgoing_bytes
def send(self, message: qrllegacy_pb2.LegacyMessage):
priority = self.factory.p2p_msg_priority[message.func_name]
outgoing_msg = OutgoingMessage(priority, message)
if self.outgoing_queue.full():
return
self.outgoing_queue.put((outgoing_msg.priority, outgoing_msg.timestamp, outgoing_msg))
self.send_next()
def loseConnection(self):
self.transport.loseConnection()
###################################################
###################################################
###################################################
###################################################
# Low-level serialization/connections/etc
# FIXME: This is a temporary refactoring, it will be completely replaced before release
@staticmethod
def _wrap_message(protobuf_obj) -> bytes:
"""
Receives a protobuf object and encodes it as (length)(data)
:return: the encoded message
:rtype: bytes
>>> veData = qrllegacy_pb2.VEData(version="version", genesis_prev_hash=b'genesis_hash')
>>> msg = qrllegacy_pb2.LegacyMessage(func_name=qrllegacy_pb2.LegacyMessage.VE, veData=veData)
>>> bin2hstr(P2PProtocol._wrap_message(msg))
'000000191a170a0776657273696f6e120c67656e657369735f68617368'
"""
# FIXME: This is not the final implementation, it is just a workaround for refactoring
# FIXME: struct.pack may result in endianness problems
# NOTE: This temporary approach does not allow for alignment. Once the stream is off, it will need to clear
data = protobuf_obj.SerializeToString()
str_data_len = struct.pack('>L', len(data))
return str_data_len + data
示例5: PriorityQueue
# 需要导入模块: from queue import PriorityQueue [as 别名]
# 或者: from queue.PriorityQueue import full [as 别名]
from queue import PriorityQueue
pq = PriorityQueue(10)
for i in range(1,16):
if pq.full():
pq.get()
pq.put(i, timeout=True)
while not pq.empty():
print(pq.get())