本文整理汇总了Python中sortedcontainers.SortedSet.pop方法的典型用法代码示例。如果您正苦于以下问题:Python SortedSet.pop方法的具体用法?Python SortedSet.pop怎么用?Python SortedSet.pop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sortedcontainers.SortedSet
的用法示例。
在下文中一共展示了SortedSet.pop方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from sortedcontainers import SortedSet [as 别名]
# 或者: from sortedcontainers.SortedSet import pop [as 别名]
class Server:
_ids = 0
def __init__(self):
self.queue = SortedSet(key = lambda job: job.arrivalTime)
self.numServers = 1
Server._ids +=1
self.busyServers = 0
self.serviceTimeDistribution = None
self.name = "Server {}".format(Server._ids)
self.In = None
self.Out = None
self.scheduler = None
def receive(self, m):
if m.event == "end": # end of service
self.send(m.job)
if len(self.queue) > 0:
assert self.busyServers == self.numServers
job = self.queue.pop(0)
self.startService(job)
else:
assert self.busyServers > 0
self.busyServers -= 1
#self.departureStats()
else: # receive new job
assert "job" in m.event
job = m.job
job.setArrivalTime(self.scheduler.now())
serviceTime = self.serviceTimeDistribution.rvs()
job.setServiceTime(serviceTime)
job.log(self.scheduler.now(), "a", self.busyServers + len(self.queue))
if self.busyServers < self.numServers:
self.busyServers += 1
self.startService(job)
else:
self.queue.add(job)
def startService(self, job):
job.log(self.scheduler.now(), "s", len(self.queue))
t = self.scheduler.now() + job.serviceTime
m = Event(self, self, t, job = job, event = "end")
self.scheduler.add(m)
def send(self, job): # job departure
job.log(self.scheduler.now(), "d", len(self.queue))
m = Event(self, self.Out, self.scheduler.now(), job = job, event = "job")
self.scheduler.add(m)
def setServiceTimeDistribution(self, distribution):
self.serviceTimeDistribution = distribution
示例2: test_pop
# 需要导入模块: from sortedcontainers import SortedSet [as 别名]
# 或者: from sortedcontainers.SortedSet import pop [as 别名]
def test_pop():
temp = SortedSet(range(0, 100), load=7)
temp.pop()
temp.pop(0)
assert all(temp[val] == (val + 1) for val in range(98))
示例3: Server
# 需要导入模块: from sortedcontainers import SortedSet [as 别名]
# 或者: from sortedcontainers.SortedSet import pop [as 别名]
class Server(Machine):
_ids = 0
def __init__(self):
state = ['Up', 'Failed', 'Maintenance', 'Blocked']
Machine.__init__(self, states = state, initial='Up')
self.add_transition('start', 'Up', 'Up', after = "startJob")
self.add_transition('fail', 'Up', 'Failed', after = 'startFail')
self.add_transition('repair', 'Failed', 'Up', after = 'rep')
self.add_transition('maint', 'Up', 'Maintenance', after='startmaint')
self.add_transition('maintcpl', 'Maintenance', 'Up')
self.add_transition('interrep', 'Failed', 'Maintenance', after='startmaint')
self.add_transition('block', 'Up', 'Blocked')
self.add_transition('unblock', 'Blocked', 'Up')
self.queue = SortedSet(key = lambda job: job.arrivalTime)
self.numServers = 1
self.busyServers = 0
self.prevState = None
Server._ids +=1
self.serviceTimeDistribution = None
self.name = "Server {}".format(Server._ids)
self.In = None
self.Out = None
self.scheduler = None
self.activejob = None
self.interuptjob = None
#debugging
self.jobsarrived = 0
self.jobsprocessed = 0
self.numfailures = 0
self.nummaint = 0
self.onzin = 0
##### Process logic #####
# starting and ending jobs #
def startJob(self,job):
# logging
job.log(self.scheduler.now(), "s", len(self.queue))
self.activejob = job
# schedule job end
t = self.scheduler.now() + job.serviceTime
m = Event(self, self, t, job = job, event = "end")
self.scheduler.add(m)
def end(self, m):
self.send(m.job)
self.jobsprocessed +=1
self.activejob = None
if len(self.queue) > 0 and self.state == 'Up':
job = self.queue.pop(0)
self.start(job)
else:
self.busyServers -= 1
#self.departureStats()
# starting and ending jobs #
# Failures #
def rep(self, temp):
if self.interuptjob:
self.resumejob()
if not(self.scheduler.completed):
self.generateFailure()
def startFail(self, temp):
if self.activejob:
self.interuptJob()
self.numfailures +=1
t = self.scheduler.now() + 1
m = Event(self, self, t, job = None, event = "repair")
self.scheduler.add(m)
def generateFailure(self):
t = self.scheduler.now() + 200
m = Event(self, self, t, job = None, event = "fail")
self.scheduler.add(m)
# Failures #
# Maintenance #
def generateMaintenance(self):
t = self.scheduler.now() + 20
m = Event(self, self, t, job = None, event = "triggerMaint")
self.scheduler.add(m)
def startmaint(self):
#if self.activejob:9
# self.interuptJob()
self.nummaint +=1
self.scheduler.deleteEvent('repair')
self.scheduler.deleteEvent('fail')
t = self.scheduler.now() + 10
m = Event(self, self, t, job = None, event = "maintStop")
self.scheduler.add(m)
def maintStop(self, temp):
if not(self.scheduler.completed):
self.generateFailure()
self.generateMaintenance()
#.........这里部分代码省略.........
示例4: RequestQ
# 需要导入模块: from sortedcontainers import SortedSet [as 别名]
# 或者: from sortedcontainers.SortedSet import pop [as 别名]
#.........这里部分代码省略.........
def __init__(self, jitter=0.0, action_limit=16, param_key_limit=16, depth_limit=16):
self.queue = SortedSet(key=lambda _: _.rating)
self._visited = set()
self._cull = {}
self.write_lock = Semaphore()
self.not_empty = Event()
self.action_limit = action_limit
self.param_key_limit = param_key_limit
self.depth_limit = depth_limit
self._visited_tree = Tree()
self.jitter = jitter
def visited(self, request):
if(any([ _(request) for _ in [ self.cull_by_hash,
self.cull_by_depth,
self.cull_by_action,
self.cull_by_param_keys ]])):
return True
return False
def cull_by_depth(self, request):
if len(request.action.split('/')) > self.depth_limit:
return True
return False
def cull_by_hash(self, request):
request_hash = request.__hash__()
if request_hash in self._visited:
return True
self._visited.add(request_hash)
return False
def cull_by_action(self, request):
endpoint_hash = request.endpoint.__hash__()
self._cull.setdefault(endpoint_hash, CullNode())
count = self._cull[endpoint_hash].action_count + 1
self._cull[endpoint_hash].action_count = count
if count > self.action_limit:
logging.debug('culling {} with count {}'.format(request.url, count))
return True
return False
def cull_by_param_keys(self, request):
if len(request.all_params()) == 0:
return False
endpoint_hash = request.endpoint.__hash__()
self._cull.setdefault(endpoint_hash, CullNode())
for key in request.all_keys():
key_hash = key.__hash__()
self._cull[endpoint_hash].setdefault(key_hash, 0)
count = self._cull[endpoint_hash][key_hash] + 1
self._cull[endpoint_hash][key_hash] = count
if count <= self.param_key_limit:
return False
else:
logging.debug('culling candidate {} with {} {} keys'.format(request.url, count, key))
logging.debug('culling {}'.format(request.url))
return True
def inv_param_key_frq(self, request):
if len(request.all_params()) == 0 or (request.body is not None and request.all_params() == 1):
return 0
endpoint_hash = request.endpoint.__hash__()
param_key_rating = sum(map(lambda (k,v): v,
filter(lambda (k,v): k in [_.__hash__() for _ in request.all_keys()],
self._cull[endpoint_hash].iteritems())))
return 1 - param_key_rating/float(sum(self._cull[endpoint_hash].values()) or 1)
def put(self, request):
with self.write_lock:
if not self.visited(request):
request.rating += random.uniform(0, self.jitter)
inv_path_frq = self._visited_tree.rate(request.endpoint)
logging.debug('{} inverse path frequency rating for {}'.format(inv_path_frq, request))
request.rating += inv_path_frq
inv_param_key_frq = self.inv_param_key_frq(request)
logging.debug('{} inverse param key frequency rating for {}'.format(inv_param_key_frq, request))
request.rating += inv_param_key_frq
self._visited_tree.visit(request.endpoint)
self.queue.add(request)
self.not_empty.set()
def qsize(self):
return len(self.queue)
def get(self):
self.not_empty.wait()
with self.write_lock:
try:
return self.queue.pop()
except IndexError:
logging.error('queue tried to pop off of empty list')
raise Done()
finally:
if self.qsize()==0:
self.not_empty.clear()