本文整理汇总了Python中sortedcontainers.SortedDict.index方法的典型用法代码示例。如果您正苦于以下问题:Python SortedDict.index方法的具体用法?Python SortedDict.index怎么用?Python SortedDict.index使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sortedcontainers.SortedDict
的用法示例。
在下文中一共展示了SortedDict.index方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: InMemoryStorage
# 需要导入模块: from sortedcontainers import SortedDict [as 别名]
# 或者: from sortedcontainers.SortedDict import index [as 别名]
class InMemoryStorage(object):
def __init__(self):
self.kvstore = SortedDict() # hashtable
def get(self, k):
try:
return self.kvstore[k]
except:
return 1
def put(self, k, v):
self.kvstore[k] = v
return 0
def delete(self, k):
try:
del self.kvstore[k]
return 0
except:
return 1
def split(self, section, keyspace_mid):
""" delete one half of keystore for group split operation """
midKey = None
for key in self.kvstore.keys(): # TODO make more efficient for better performance
if key > str(keyspace_mid): # use iloc to estimate midpoint
midKey = self.kvstore.index(key)
break
if section: # section is either 0 or 1
self.kvstore = self.kvstore.items()[midKey:]
else:
self.kvstore = self.kvstore.items()[:midKey]
print(self.kvstore)
return 0
def save(self): # need metadata here
save_state("data/backup/db_copy.pkl", self.kvstore)
def load(self):
self.kvstore = load_state("data/backup/db_copy.pkl")
示例2: test_index
# 需要导入模块: from sortedcontainers import SortedDict [as 别名]
# 或者: from sortedcontainers.SortedDict import index [as 别名]
def test_index():
mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
temp = SortedDict(mapping)
assert temp.index('a') == 0
assert temp.index('f', 3, -3) == 5
示例3: __ne__
# 需要导入模块: from sortedcontainers import SortedDict [as 别名]
# 或者: from sortedcontainers.SortedDict import index [as 别名]
return self.fr <= t and t < self.to
return self.fr == t.fr and self.to == t.to
def __ne__(self, t):
return not self.__eq__(t)
def __hash__(self):
return hash(str(self))
def __str__(self):
return "[{}-{}]".format(self.fr, self.to)
from sortedcontainers import SortedDict
d = SortedDict()
d[Range(50,60)] = None
d[Range(40,50)] = None
d[Range(10,20)] = None
d[Range(80,90)] = None
d[Range(110,120)] = None
for k in d.keys():
print(k)
print("==============================")
print("85", d.iloc[d.index(85)])
print("40", d.iloc[d.index(40)])
print("49", d.iloc[d.index(49)])
print("50", d.iloc[d.index(50)])
print("59", d.iloc[d.index(59)])
try:
d.index(60)
except ValueError:
print("60 is not in d")
d[60]
示例4: TxGraph
# 需要导入模块: from sortedcontainers import SortedDict [as 别名]
# 或者: from sortedcontainers.SortedDict import index [as 别名]
#.........这里部分代码省略.........
#remove the edge from the edgelist with old timestamp
edgeList = self.__get_edgelist(currEdge.tstamp, create=False)
del edgeList.edges[currEdge.name]
#update the tstamp in the edge
currEdge.tstamp = tstamp
#move this edge to the correct edgelist
edgeList = self.__get_edgelist(tstamp)
edgeList.edges[currEdge.name] = currEdge
def __update_tx_window(self):
"""updates the transaction window of the graph
This method is called when a newer transaction out the
window arrives. It does the following:
1. Gets the edgeList's that are below the lowMarker
2. Goes through the edges and deletes them from the edgeMap
3. Update the degree of the nodes
4. Moves the window by deleting the stale edgeLists
"""
tsIter = self.txMap.irange(None, self.lowMarker, inclusive=(True,False))
lastTStamp = None
for tstamp in tsIter:
lastTStamp = tstamp
edgeList = self.txMap[tstamp]
for edge in edgeList.edges.itervalues():
self.__remove_edge(edge)
#lets delete the stale edgelists
if lastTStamp:
lowIdx = self.txMap.index(lastTStamp)
del self.txMap.iloc[:lowIdx+1]
def process_transaction(self, tstamp, source, target):
"""this is the starting point of transaction processing.
We first check whether the tx is within the window.
If it is, then we update the Edge (if it already exists) or
create a new Edge if necessary and update the median.
If the tx is not within the window and is newer, we then
move the window and remove all stale(older) edges and create
a new edge for the newer transaction and finally update the
median
"""
#basic sanity checks
if source is None or target is None:
raise Exception("Invalid node")
if len(source) == 0 or len(target) == 0:
raise Exception("Invalid node")
if source == target:
raise Exception("source and target cannot be the same")
#timestamp of the transaction is old and can be ignored
if tstamp < self.lowMarker:
return
#create a new edge representing this transaction
newEdge = Edge(tstamp, source, target)
if tstamp <= self.highMarker:
示例5: test_index_key
# 需要导入模块: from sortedcontainers import SortedDict [as 别名]
# 或者: from sortedcontainers.SortedDict import index [as 别名]
def test_index_key():
temp = SortedDict(negate, 7, ((val, val) for val in range(100)))
assert all(temp.index(val) == (99 - val) for val in range(100))
示例6: Model
# 需要导入模块: from sortedcontainers import SortedDict [as 别名]
# 或者: from sortedcontainers.SortedDict import index [as 别名]
class Model(object):
'''
The model of a Stranbeest. The Model consists of a set of nodes, edges and boundary
conditions. Each node has a unique name and a x and y position which may change
whenever the simuation is incremented. Each node introduces two degrees of freedom.
The edges are specified by the nodes they are connecting. The edges are the push/pull
rods which connect the edges whith one another. An edges keeps the distances between
two nodes constant and therefore constrains exactly one degree of freedom in the system.
'''
def __init__(self):
'''
Constructor
'''
self._nodes = SortedDict()
self._edges = defaultdict(set)
def addNode(self,name,x,y):
if not isinstance(name,str ): raise Exception("The 1st argument must be the node's name as str.")
if not isinstance(x ,float): raise Exception("The 2nd argument must be the node's x position as float.")
if not isinstance(y ,float): raise Exception("The 2nd argument must be the node's y position as float.")
if name in self._nodes: raise Exception( 'There already exists a node by the name of "%(name)s"' % locals() )
self._nodes[name] = x,y
self.__t = 0.0
for listener in self.onNodeAddListeners:
listener(name,x,y)
def addEdge(self,node1,node2):
if node1 == node2:
raise Exception('"node1" cannot be equal to "node2".')
self._edges[node1].add(node2)
self._edges[node2].add(node1)
for listener in self.onEdgeAddListeners:
listener( min(node1,node2), max(node1,node2) )
def pos(self,name):
return self._nodes[name]
def move(self,name,x,y):
self._nodes[name] = x,y
for listener in self.onNodeMoveListeners:
listener(name,x,y)
def state(self):
return fromiter( chain.from_iterable( self._nodes.values() ), float )
def setState(self,state):
for i,(x,y) in enumerate( zip(state[::2],state[1::2]) ):
self.move(self._nodes.keys()[i],x,y)
@property
def t(self):
return self.__t
def increment(self,dt):
v = self.v
t0 = self.__t
x0 = self.state()
# https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods#The_Runge.E2.80.93Kutta_method
k0 = v(x0, t0)
k1 = v(x0+k0*(dt/2), t0+dt/2)
k2 = v(x0+k1*(dt/2), t0+dt/2)
k3 = v(x0+k2*(dt), t0+dt)
self.setState( x0 + dt/6 * (k0+k1+k2+k3) )
self.__t += dt
def v(self,x,t):
lhs = zeros( 2*[len(x)] )
rhs = zeros( len(x) )
iRows = iter( range( len(x) ) )
for start,end in self.edges():
iStart = 2*self._nodes.index(start)
iEnd = 2*self._nodes.index(end)
iRow = next(iRows)
dx = x[iEnd+0] - x[iStart+0]
dy = x[iEnd+1] - x[iStart+1]
lhs[iRow,iStart+0] = dx; lhs[iRow,iEnd+0] = -dx
lhs[iRow,iStart+1] = dy; lhs[iRow,iEnd+1] = -dy
rhs[iRow] = 0
for bc in self.bcs:
bc.addEquations(x,t,iRows,lhs,rhs)
return linalg.solve(lhs,rhs)
def nodes(self):
return self._nodes.iteritems()
def edges(self):
for node1,neighbors in self._edges.items():
for node2 in neighbors:
if node1 < node2:
yield node1,node2
bcs = []
onEdgeAddListeners = set() # <- FIXME should be a multiset
onNodeAddListeners = set() # <- FIXME should be a multiset
onNodeMoveListeners = set() # <- FIXME should be a multiset
示例7: FileTable
# 需要导入模块: from sortedcontainers import SortedDict [as 别名]
# 或者: from sortedcontainers.SortedDict import index [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
#.........这里部分代码省略.........