本文整理汇总了Python中sortedcontainers.SortedDict.bisect_left方法的典型用法代码示例。如果您正苦于以下问题:Python SortedDict.bisect_left方法的具体用法?Python SortedDict.bisect_left怎么用?Python SortedDict.bisect_left使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sortedcontainers.SortedDict
的用法示例。
在下文中一共展示了SortedDict.bisect_left方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_bisect
# 需要导入模块: from sortedcontainers import SortedDict [as 别名]
# 或者: from sortedcontainers.SortedDict import bisect_left [as 别名]
def test_bisect():
mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
temp = SortedDict(mapping)
assert temp.bisect_left('a') == 0
assert temp.bisect_right('f') == 6
assert temp.bisect('f') == 6
示例2: xrange
# 需要导入模块: from sortedcontainers import SortedDict [as 别名]
# 或者: from sortedcontainers.SortedDict import bisect_left [as 别名]
posStrand = [False]
# CAN THEORETICALLY EXTRACT FROM BOTH STRANDS
else: posStrand = [True,False]
for strand in posStrand:
kmer = ""
if strand: kmer = context[posInWindow:posInWindow+forwardLength]
else: kmer = context[posInWindow-forwardLength+1:posInWindow+1]
for i in xrange(targetCov//len(posStrand)):
# Pick a threshold
rval = random.random()
if rval <= forward[strand][kmer]*correctForward:
# Pick a length
lval = random.random()
key = lengthDist.bisect_left(lval)
selLength = lengthDist[lengthDist.iloc[key]]
# Check the kmer at the other end
rkmer = ""
if strand: rkmer = context[posInWindow+selLength-reverseLength:posInWindow+selLength]
else: rkmer = context[posInWindow-selLength+1:posInWindow-selLength+reverseLength+1]
rval = random.random()
if rval <= reverse[not strand][rkmer]*correctReverse:
# Extract sequence and write BAM record
selSeq = ""
if strand: selSeq = context[posInWindow:posInWindow+selLength]
else: selSeq = context[posInWindow-selLength+1:posInWindow+1]
writeBAMentry(chrom,pos,selSeq,selLength,strand)
#print strand,kmer,rkmer,selSeq,len(selSeq),selLength
示例3: FileTable
# 需要导入模块: from sortedcontainers import SortedDict [as 别名]
# 或者: from sortedcontainers.SortedDict import bisect_left [as 别名]
class FileTable(object):
"""docstring for FileTable"""
def __init__(self, myip, server):
super(FileTable, self).__init__()
self.ring = SortedDict()
self.hasher = hashlib.sha224
self.myhash = self.hash(myip)
self.add_node(myip)
self.server = server
def hash(self, key):
return self.hasher(key).hexdigest()[:-10]
def hash_at(self, idx):
idx %= len(self.ring)
hash = self.ring.iloc[idx]
return hash
def add_node(self, ip):
hash = self.hash(ip)
self.ring[hash] = {'ip': ip, 'files': []}
SDFS_LOGGER.info('After adding %s - %s' % (ip, repr(self.ring)))
def remove_node(self, failed_list):
start_time = time.time()
# this is for debug
flag = False
# deep copy failed list because it will be reset soon
ip_list = list(failed_list)
# change the order of failed node
# make sure the smaller id node be handled first
if len(ip_list) == 2:
if self.hash(ip_list[0]) == 0 and self.hash(ip_list[1]) == len(self.ring) - 1:
ip_list[0], ip_list[1] = ip_list[1], ip_list[0]
elif self.ring.index(self.hash(ip_list[0])) == self.ring.index(self.hash(ip_list[1])) + 1:
ip_list[0], ip_list[1] = ip_list[1], ip_list[0]
for ip in ip_list:
hash = self.hash(ip)
idx = self.ring.index(hash)
# if the node is not the direct successor of the failed node, do nothing
if len(ip_list) == 2 and ip == ip_list[1] and self.hash_at((idx + 2) % len(self.ring)) == self.myhash:
continue
if self.hash_at((idx + 1) % len(self.ring)) == self.myhash or (self.hash_at((idx + 2) % len(self.ring)) == self.myhash and len(ip_list) == 2):
# this is for debug
flag = True
heritage = set(self.ring[hash]['files'])
my_files = set(self.ring[self.myhash]['files'])
next_files = set(self.ring[self.hash_at(idx + 2)]['files'])
# determine the
to_me = heritage - my_files
to_next = (heritage & my_files) - next_files
to_next_next = heritage & my_files & next_files
replica_list = [list(to_me), list(to_next), list(to_next_next)]
self.ring[self.myhash]['files'].extend(to_me)
# handle replica
dest_ip_to_me = self.ring[self.hash_at(self.ring.index(hash) - 1)]['ip']
dest_ip_to_next = self.ring[self.hash_at(self.ring.index(self.myhash) + 1)]['ip']
dest_ip_to_next_next = self.ring[self.hash_at(self.ring.index(self.myhash) + 2)]['ip']
dest_ip_list = [dest_ip_to_me, dest_ip_to_next, dest_ip_to_next_next]
del self.ring[hash]
self.server.handle_replica(replica_list, dest_ip_list, ip_list)
else:
del self.ring[hash]
elapsed_time = time.time() - start_time
if flag:
print "It takes", elapsed_time, "to handle replica"
def lookup(self, sdfs_filename):
hash = self.hash(sdfs_filename)
idx = self.ring.bisect_left(hash) if self.ring.bisect_left(hash) < len(self.ring) else 0
ip_list = [self.ring[self.hash_at(idx + i)]['ip'] for i in xrange(3)]
return ip_list
def insert(self, sdfs_filename):
hash = self.hash(sdfs_filename)
idx = self.ring.bisect_left(hash) if self.ring.bisect_left(hash) < len(self.ring) else 0
for i in xrange(3):
node_hash = self.hash_at(idx + i)
self.ring[node_hash]['files'].append(sdfs_filename)
SDFS_LOGGER.info('Inserted %s to %s' % (sdfs_filename, self.ring[node_hash]['ip']))
def delete(self, sdfs_filename):
hash = self.hash(sdfs_filename)
idx = self.ring.bisect_left(hash) if self.ring.bisect_left(hash) < len(self.ring) else 0
#.........这里部分代码省略.........
示例4: test_bisect_key
# 需要导入模块: from sortedcontainers import SortedDict [as 别名]
# 或者: from sortedcontainers.SortedDict import bisect_left [as 别名]
def test_bisect_key():
temp = SortedDict(modulo, 7, ((val, val) for val in range(100)))
assert all(temp.bisect(val) == ((val % 10) + 1) * 10 for val in range(100))
assert all(temp.bisect_right(val) == ((val % 10) + 1) * 10 for val in range(100))
assert all(temp.bisect_left(val) == (val % 10) * 10 for val in range(100))
示例5: FederationRemoteSendQueue
# 需要导入模块: from sortedcontainers import SortedDict [as 别名]
# 或者: from sortedcontainers.SortedDict import bisect_left [as 别名]
class FederationRemoteSendQueue(object):
"""A drop in replacement for FederationSender"""
def __init__(self, hs):
self.server_name = hs.hostname
self.clock = hs.get_clock()
self.notifier = hs.get_notifier()
self.is_mine_id = hs.is_mine_id
self.presence_map = {} # Pending presence map user_id -> UserPresenceState
self.presence_changed = SortedDict() # Stream position -> list[user_id]
# Stores the destinations we need to explicitly send presence to about a
# given user.
# Stream position -> (user_id, destinations)
self.presence_destinations = SortedDict()
self.keyed_edu = {} # (destination, key) -> EDU
self.keyed_edu_changed = SortedDict() # stream position -> (destination, key)
self.edus = SortedDict() # stream position -> Edu
self.device_messages = SortedDict() # stream position -> destination
self.pos = 1
self.pos_time = SortedDict()
# EVERYTHING IS SAD. In particular, python only makes new scopes when
# we make a new function, so we need to make a new function so the inner
# lambda binds to the queue rather than to the name of the queue which
# changes. ARGH.
def register(name, queue):
LaterGauge("synapse_federation_send_queue_%s_size" % (queue_name,),
"", [], lambda: len(queue))
for queue_name in [
"presence_map", "presence_changed", "keyed_edu", "keyed_edu_changed",
"edus", "device_messages", "pos_time", "presence_destinations",
]:
register(queue_name, getattr(self, queue_name))
self.clock.looping_call(self._clear_queue, 30 * 1000)
def _next_pos(self):
pos = self.pos
self.pos += 1
self.pos_time[self.clock.time_msec()] = pos
return pos
def _clear_queue(self):
"""Clear the queues for anything older than N minutes"""
FIVE_MINUTES_AGO = 5 * 60 * 1000
now = self.clock.time_msec()
keys = self.pos_time.keys()
time = self.pos_time.bisect_left(now - FIVE_MINUTES_AGO)
if not keys[:time]:
return
position_to_delete = max(keys[:time])
for key in keys[:time]:
del self.pos_time[key]
self._clear_queue_before_pos(position_to_delete)
def _clear_queue_before_pos(self, position_to_delete):
"""Clear all the queues from before a given position"""
with Measure(self.clock, "send_queue._clear"):
# Delete things out of presence maps
keys = self.presence_changed.keys()
i = self.presence_changed.bisect_left(position_to_delete)
for key in keys[:i]:
del self.presence_changed[key]
user_ids = set(
user_id
for uids in self.presence_changed.values()
for user_id in uids
)
keys = self.presence_destinations.keys()
i = self.presence_destinations.bisect_left(position_to_delete)
for key in keys[:i]:
del self.presence_destinations[key]
user_ids.update(
user_id for user_id, _ in self.presence_destinations.values()
)
to_del = [
user_id for user_id in self.presence_map if user_id not in user_ids
]
for user_id in to_del:
del self.presence_map[user_id]
# Delete things out of keyed edus
keys = self.keyed_edu_changed.keys()
i = self.keyed_edu_changed.bisect_left(position_to_delete)
for key in keys[:i]:
#.........这里部分代码省略.........