本文整理汇总了Python中weakref.WeakValueDictionary.values方法的典型用法代码示例。如果您正苦于以下问题:Python WeakValueDictionary.values方法的具体用法?Python WeakValueDictionary.values怎么用?Python WeakValueDictionary.values使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weakref.WeakValueDictionary
的用法示例。
在下文中一共展示了WeakValueDictionary.values方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: WeakSet
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import values [as 别名]
class WeakSet(object):
def __init__(self):
self._data = WeakValueDictionary()
def add(self, obj):
self._data[id(obj)] = obj
def remove(self, obj):
try:
del self._data[id(obj)]
except:
raise KeyError(obj)
def discard(self, obj):
try:
self.remove(obj)
except:
return
def __iter__(self):
for obj in self._data.values():
yield obj
def __len__(self):
return len(self._data)
def __contains__(self, obj):
return id(obj) in self._data
def __hash__(self):
raise TypeError, "Can't hash a WeakSet."
示例2: Transport
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import values [as 别名]
class Transport(BaseTransport):
"""Server side of the LocalQueue transport"""
def __init__(self,callbacks,cfg):
#logger.debug("Server: setting up")
self.trace = cfg.get('trace,',0)
self.callbacks = callbacks
self.p = LocalQueue(cfg)
self.p.server = ref(self) # for clients to find me
self.clients = WeakValueDictionary() # clients add themselves here
@spawned
def _process(self,msg):
m = self.callbacks.recv(msg.msg)
msg.reply(m)
def run(self):
logger.debug("Server: wait for messages")
while self.p.request_q is not None:
msg = self.p.request_q.get()
#logger.debug("Server: received %r",msg)
self._process(msg)
def send(self,msg):
m = msg
msg = RPCmessage(msg, _trace=self.trace)
self.last_msgid -= 1
msg.msgid = self.last_msgid
if self.trace:
logger.debug("Server: send msg %s:\n%s",msg.msgid,format_msg(m))
for c in self.clients.values():
c.reply_q.put(msg)
示例3: Master
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import values [as 别名]
class Master(object):
def __init__(self, id, max_slots):
self.id = id
self.max_slots = max_slots
self.lock = Semaphore()
self.slots = WeakValueDictionary()
def serialize(self):
return {
'id': self.id,
'max_slots': self.max_slots,
'slots': len(self.slots),
'workers': sum(len(slot.workers) for slot in self.slots.values())
}
示例4: Supervisor
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import values [as 别名]
class Supervisor(object):
def __init__(self):
self.processes = WeakValueDictionary()
def register(self, process):
self.processes[process.id] = process
def process(self, process_id):
if process_id in self.processes:
return self.processes[process_id]
return None
def read_all(self):
for process in self.processes.values():
process.read()
示例5: NodeManager
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import values [as 别名]
class NodeManager(BaseNodeManager):
NODE_CLASS = Node
def __init__(self, project):
super().__init__()
# XXX [!] cycle reference
self.project = project
self.data = WeakValueDictionary()
self.root = None
def update_root(self, root_project, root_project_children):
self.root = self.new_root_node(root_project, root_project_children)
def __setitem__(self, projectid, node):
assert self.check_not_exist_node(node)
assert projectid == node.projectid
self.data[node.projectid] = node
assert self.check_exist_node(node)
def __delitem__(self, node):
assert self.check_exist_node(node)
del self.data[node.projectid]
assert self.check_not_exist_node(node)
def __iter__(self):
# how to support lock for iter? just copy dict?
return iter(self.data.values())
def __contains__(self, node):
if node is None:
return False
newnode = self.data.get(node.projectid)
return node is newnode # ?!
def __len__(self):
return len(self.data)
def __bool__(self):
return len(self) != 0
@property
def get(self):
return self.data.get
@property
def clear(self):
return self.data.clear
# TODO: add expend node. (not operation.)
def check_exist_node(self, node):
original_node = self.get(node.projectid)
if original_node is None:
raise WFNodeError("{!r} is not exists.".format(node))
elif original_node is not node:
raise WFNodeError("{!r} is invalid node.".format(node))
return True
def check_not_exist_node(self, node):
if node in self:
raise WFNodeError("{!r} is already exists.".format(node))
return True
def new_void_node(self, uuid=None):
return self.NODE_CLASS.from_void(uuid, project=self.project)
def new_node_from_json(self, data, parent=None):
return self.NODE_CLASS.from_json_with_project(data, parent=parent, project=self.project)
def add(self, node, recursion=True):
assert recursion is True
added_nodes = 0
def register_node(node):
nonlocal added_nodes
self[node.projectid] = node
added_nodes += 1
register_node(node)
if recursion:
def deep(node):
for subnode in node:
register_node(subnode)
deep(subnode)
deep(node)
return added_nodes
def remove(self, node, recursion=True):
assert self.check_exist_node(node)
if node.parent is not None:
assert self.check_exist_node(node.parent)
if node in node.parent:
raise WFNodeError("node are still exists in parent node.")
removed_nodes = 0
def unregister_node(node):
nonlocal removed_nodes
#.........这里部分代码省略.........
示例6: DispatchTree
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import values [as 别名]
#.........这里部分代码省略.........
maxNbCores,
ramUse,
environment,
lic=lic,
tags=tags,
timer=timer,
maxAttempt=maxAttempt,
runnerPackages=runnerPackages,
watcherPackages=watcherPackages,
)
for commandDef in taskDefinition["commands"]:
description = commandDef["description"]
arguments = commandDef["arguments"]
cmd = Command(
None, description, task, arguments, runnerPackages=runnerPackages, watcherPackages=watcherPackages
)
task.commands.append(cmd)
# import sys
# logger.warning("cmd creation : %s" % str(sys.getrefcount(cmd)))
return task
## Resets the lists of elements to create or update in the database.
#
def resetDbElements(self):
self.toCreateElements = []
self.toModifyElements = []
self.toArchiveElements = []
## Recalculates the max ids of all elements. Generally called after a reload from db.
#
def recomputeMaxIds(self):
self.nodeMaxId = max([n.id for n in self.nodes.values()]) if self.nodes else 0
self.nodeMaxId = max(self.nodeMaxId, StatDB.getFolderNodesMaxId(), StatDB.getTaskNodesMaxId())
self.poolMaxId = max([p.id for p in self.pools.values()]) if self.pools else 0
self.poolMaxId = max(self.poolMaxId, StatDB.getPoolsMaxId())
self.renderNodeMaxId = max([rn.id for rn in self.renderNodes.values()]) if self.renderNodes else 0
self.renderNodeMaxId = max(self.renderNodeMaxId, StatDB.getRenderNodesMaxId())
self.taskMaxId = max([t.id for t in self.tasks.values()]) if self.tasks else 0
self.taskMaxId = max(self.taskMaxId, StatDB.getTasksMaxId(), StatDB.getTaskGroupsMaxId())
self.commandMaxId = max([c.id for c in self.commands.values()]) if self.commands else 0
self.commandMaxId = max(self.commandMaxId, StatDB.getCommandsMaxId())
self.poolShareMaxId = max([ps.id for ps in self.poolShares.values()]) if self.poolShares else 0
self.poolShareMaxId = max(self.poolShareMaxId, StatDB.getPoolSharesMaxId())
## Removes from the dispatchtree the provided element and all its parents and children.
#
def unregisterElementsFromTree(self, element):
# /////////////// Handling of the Task
if isinstance(element, Task):
del self.tasks[element.id]
self.toArchiveElements.append(element)
for cmd in element.commands:
self.unregisterElementsFromTree(cmd)
for node in element.nodes.values():
self.unregisterElementsFromTree(node)
# /////////////// Handling of the TaskGroup
elif isinstance(element, TaskGroup):
del self.tasks[element.id]
self.toArchiveElements.append(element)
for task in element.tasks:
self.unregisterElementsFromTree(task)
for node in element.nodes.values():
self.unregisterElementsFromTree(node)
# /////////////// Handling of the TaskNode
示例7: PersistentList
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import values [as 别名]
#.........这里部分代码省略.........
self._bucket_store.store_bucket(bucket_key=bucket_key, bucket=bucket)
# free empty buckets
else:
self._bucket_store.free_bucket(bucket_key)
# cache management
# Item cache
def _set_cached_item(self, key, item):
"""Cache reference to existing item"""
try:
self._active_items[key] = item
except TypeError:
pass
def _get_cached_item(self, key):
"""Get reference to existing item; raises KeyError if item cannot be fetched"""
try:
return self._active_items[key]
except TypeError:
raise KeyError
def _del_cached_item(self, key):
"""Release reference to existing item"""
try:
del self._active_items[key]
except (TypeError, KeyError):
pass
# paths and files
def flush(self):
"""
Commit all outstanding changes to persistent store
"""
for bucket_key, bucket in self._active_buckets.values():
self._store_bucket(bucket_key, bucket)
# sequence interface
def __getitem__(self, pos):
if isinstance(pos, slice):
return self._get_slice(pos)
return self._get_item(pos)
def _get_item(self, index):
# - use cached reference to existing item
# - fetch item from cached reference to existing bucket
# - fetch item from fetched bucket
try:
return self._get_cached_item(index)
except KeyError:
bucket = self._get_bucket(self._bucket_key(index))
item = bucket[index % self._bucket_length]
self._set_cached_item(index, item)
return item
def _get_slice(self, positions):
start_idx, stop_idx, stride = positions.indices(self._length)
list_slice = []
# fetch sub-slice from each bucket
while start_idx < stop_idx:
bucket = self._get_bucket(self._bucket_key(start_idx))
# stop_idx in next bucket
if stop_idx // self._bucket_length > start_idx // self._bucket_length:
list_slice.extend(bucket[start_idx % self._bucket_length::stride])
slice_length = math.ceil((self._bucket_length - (start_idx % self._bucket_length)) / stride)
# stop_idx in this bucket
else:
示例8: ChannelManager
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import values [as 别名]
#.........这里部分代码省略.........
>>> c.fullname
'root'
>>> c = cm.get_channel_by_name('root.sub', autocreate=True)
>>> c.name
'sub'
>>> c.fullname
'root.sub'
>>> c = cm.get_channel_by_name('sub', autocreate=True)
>>> c.name
'sub'
>>> c.fullname
'root.sub'
>>> c = cm.get_channel_by_name('autosub.sub', autocreate=True)
>>> c.name
'sub'
>>> c.fullname
'root.autosub.sub'
>>> c = cm.get_channel_by_name(None)
>>> c.fullname
'root'
>>> c = cm.get_channel_by_name('root.sub')
>>> c.fullname
'root.sub'
>>> c = cm.get_channel_by_name('sub')
>>> c.fullname
'root.sub'
"""
if not channel_name or channel_name == self._root_channel.name:
return self._root_channel
if not channel_name.startswith(self._root_channel.name + '.'):
channel_name = self._root_channel.name + '.' + channel_name
if channel_name in self._channels_by_name:
return self._channels_by_name[channel_name]
if not autocreate:
raise ChannelNotFound('Channel %s not found' % channel_name)
parent = self._root_channel
for subchannel_name in channel_name.split('.')[1:]:
subchannel = parent.get_subchannel_by_name(subchannel_name)
if not subchannel:
subchannel = Channel(subchannel_name, parent, capacity=None)
self._channels_by_name[subchannel.fullname] = subchannel
parent = subchannel
return parent
def notify(self, db_name, channel_name, uuid,
seq, date_created, priority, eta, state):
try:
channel = self.get_channel_by_name(channel_name)
except ChannelNotFound:
_logger.warning('unknown channel %s, '
'using root channel for job %s',
channel_name, uuid)
channel = self._root_channel
job = self._jobs_by_uuid.get(uuid)
if job:
# db_name is invariant
assert job.db_name == db_name
# date_created is invariant
assert job.date_created == date_created
# if one of the job properties that influence
# scheduling order has changed, we remove the job
# from the queues and create a new job object
if (seq != job.seq or
priority != job.priority or
eta != job.eta or
channel != job.channel):
_logger.debug("job %s properties changed, rescheduling it",
uuid)
self.remove_job(uuid)
job = None
if not job:
job = ChannelJob(db_name, channel, uuid,
seq, date_created, priority, eta)
self._jobs_by_uuid[uuid] = job
# state transitions
if not state or state == DONE:
job.channel.set_done(job)
elif state == PENDING:
job.channel.set_pending(job)
elif state in (ENQUEUED, STARTED):
job.channel.set_running(job)
elif state == FAILED:
job.channel.set_failed(job)
else:
_logger.error("unexpected state %s for job %s", state, job)
def remove_job(self, uuid):
job = self._jobs_by_uuid.get(uuid)
if job:
job.channel.remove(job)
del self._jobs_by_uuid[job.uuid]
def remove_db(self, db_name):
for job in self._jobs_by_uuid.values():
if job.db_name == db_name:
job.channel.remove(job)
del self._jobs_by_uuid[job.uuid]
def get_jobs_to_run(self, now):
return self._root_channel.get_jobs_to_run(now)
示例9: UniversalCursors
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import values [as 别名]
class UniversalCursors(object):
def __init__(self):
self.name_cursors = {}
self.cursors_orient = WeakKeyDictionary()
self.all_canvas = WeakValueDictionary()
self.all_axes = WeakValueDictionary()
self.backgrounds = {}
self.visible = True
self.needclear = False
def _onmove(self, event):
for canvas in self.all_canvas.values():
if not canvas.widgetlock.available(self):
return
if event.inaxes is None or not self.visible:
if self.needclear:
self._update(event)
for canvas in self.all_canvas.values():
canvas.draw()
self.needclear = False
return
self._update(event)
def _update(self, event):
# 1/ Reset background
for canvas in self.all_canvas.values():
canvas.restore_region(self.backgrounds[id(canvas)])
# 2/ update cursors
for cursors in self.cursors_orient.keys():
orient = self.cursors_orient[cursors]
if (event.inaxes in [line.get_axes() for line in cursors]
and self.visible):
visible = True
self.needclear = True
else:
visible = False
for line in cursors:
if orient == 'vertical':
line.set_xdata((event.xdata, event.xdata))
if orient == 'horizontal':
line.set_ydata((event.ydata, event.ydata))
line.set_visible(visible)
ax = line.get_axes()
ax.draw_artist(line)
# 3/ update canvas
for canvas in self.all_canvas.values():
canvas.blit(canvas.figure.bbox)
def _clear(self, event):
"""clear the cursor"""
self.backgrounds = {}
for canvas in self.all_canvas.values():
self.backgrounds[id(canvas)] = (
canvas.copy_from_bbox(canvas.figure.bbox))
for cursor in self.cursors_orient.keys():
for line in cursor:
line.set_visible(False)
def add(self, name, axes=(), orient='vertical', **lineprops):
if name in self.name_cursors.keys():
raise NameError
class CursorList(list):
def __hash__(self):
return hash(tuple(self))
self.name_cursors[name] = CursorList() # Required to keep weakref
for ax in axes:
self.all_axes[id(ax)] = ax
ax_canvas = ax.get_figure().canvas
if ax_canvas not in self.all_canvas.values():
#if not ax_canvas.supports_blit:
# warnings.warn("Must use canvas that support blit")
# return
self.all_canvas[id(ax_canvas)] = ax_canvas
ax_canvas.mpl_connect('motion_notify_event', self._onmove)
ax_canvas.mpl_connect('draw_event', self._clear)
if orient == 'vertical':
line = ax.axvline(ax.get_xbound()[0], visible=False,
animated=True, **lineprops)
if orient == 'horizontal':
line = ax.axhline(ax.get_ybound()[0], visible=False,
animated=True, **lineprops)
self.name_cursors[name].append(line)
self.cursors_orient[self.name_cursors[name]] = orient
def remove(self, name):
del self.name_cursors[name]
示例10: ChannelManager
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import values [as 别名]
#.........这里部分代码省略.........
>>> ChannelManager.split_strip("foo: bar baz\\n: fred:", ":")
['foo', 'bar baz', 'fred', '']
"""
return [x.strip() for x in s.split(sep, maxsplit)]
@classmethod
def parse_simple_config(cls, config_string):
"""Parse a simple channels configuration string.
The general form is as follow:
channel(.subchannel)*(:capacity(:key(=value)?)*)? [, ...]
If capacity is absent, it defaults to 1.
If a key is present without value, it gets True as value.
When declaring subchannels, the root channel may be omitted
(ie sub:4 is the same as root.sub:4).
Returns a list of channel configuration dictionaries.
>>> from pprint import pprint as pp
>>> pp(ChannelManager.parse_simple_config('root:4'))
[{'capacity': 4, 'name': 'root'}]
>>> pp(ChannelManager.parse_simple_config('root:4,root.sub:2'))
[{'capacity': 4, 'name': 'root'}, {'capacity': 2, 'name': 'root.sub'}]
>>> pp(ChannelManager.parse_simple_config('root:4,root.sub:2:'
... 'sequential:k=v'))
[{'capacity': 4, 'name': 'root'},
{'capacity': 2, 'k': 'v', 'name': 'root.sub', 'sequential': True}]
>>> pp(ChannelManager.parse_simple_config('root'))
[{'capacity': 1, 'name': 'root'}]
>>> pp(ChannelManager.parse_simple_config('sub:2'))
[{'capacity': 2, 'name': 'sub'}]
It ignores whitespace around values, and drops empty entries which
would be generated by trailing commas, or commented lines on the Odoo
config file.
>>> pp(ChannelManager.parse_simple_config('''
... root : 4,
... ,
... foo bar:1: k=va lue,
... '''))
[{'capacity': 4, 'name': 'root'},
{'capacity': 1, 'k': 'va lue', 'name': 'foo bar'}]
It's also possible to replace commas with line breaks, which is more
readable if you're taking the channel configuration from a ConfigParser
file.
>>> pp(ChannelManager.parse_simple_config('''
... root : 4
... foo bar:1: k=va lue
... baz
... '''))
[{'capacity': 4, 'name': 'root'},
{'capacity': 1, 'k': 'va lue', 'name': 'foo bar'},
{'capacity': 1, 'name': 'baz'}]
"""
res = []
config_string = config_string.replace("\n", ",")
for channel_config_string in cls.split_strip(config_string, ','):
if not channel_config_string:
# ignore empty entries (commented lines, trailing commas)
continue
config = {}
config_items = cls.split_strip(channel_config_string, ':')
示例11: DispatchTree
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import values [as 别名]
class DispatchTree(object):
def _display_(self):
'''
Debug purpose method, returns a basic display of the dispatch tree as html
'''
startTimer = time.time()
timeout = 2.0
result="<html><head><style>table,th,td { margin: 5px; border-collapse:collapse; border:1px solid black; }</style></head><body font-family='verdana'>"
result +="<h3>Pools: %r</h3><table>" % len(self.pools)
for i,curr in enumerate(self.pools):
result += "<tr><td>%r</td><td>%s</td></tr>" % (i, self.pools[curr])
if (time.time()-startTimer) > timeout:
raise TimeoutException("TimeoutException occured: the dispatchTree might be too large to dump")
result+="</table>"
result +="<h3>Rendernodes: %r</h3><table>" % len(self.renderNodes)
for i,curr in enumerate(self.renderNodes):
result += "<tr><td>%r</td><td>%r</td></tr>" % (i, self.renderNodes[curr])
if (time.time()-startTimer) > timeout:
raise TimeoutException("TimeoutException occured: the dispatchTree might be too large to dump")
result+="</table>"
result +="<h3>PoolShares: (attribution de parc pour une tache fille du root, on attribue pas de poolshare aux autres)</h3><table>"
for i,curr in enumerate(self.poolShares):
result += "<tr><td>%r</td><td>%s</td></tr>" % (i, self.poolShares[curr])
if (time.time()-startTimer) > timeout:
raise TimeoutException("TimeoutException occured: the dispatchTree might be too large to dump")
result+="</table>"
result +="<h3>Main level nodes (proxy info only):</h3><table>"
result +="<tr><th>id</th><th>name</th><th>readyCommandCount</th><th>commandCount</th><th>completion</th><th>poolshares</th></tr>"
for i,curr in enumerate(self.nodes[1].children):
result += "<tr><td>%r</td><td>%s</td><td>%d</td><td>%d</td><td>%.2f</td><td>%s</td></tr>" % (i, curr.name, curr.readyCommandCount, curr.commandCount, curr.completion, curr.poolShares.values())
if (time.time()-startTimer) > timeout:
raise TimeoutException("TimeoutException occured: the dispatchTree might be too large to dump")
result+="</table>"
result +="<h3>All nodes:</h3><table>"
for i,curr in enumerate(self.nodes):
result += "<tr><td>%d</td><td>%s</td><td>%r</td></tr>" % (i, curr, self.nodes[curr].name)
if (time.time()-startTimer) > timeout:
raise TimeoutException("TimeoutException occured: the dispatchTree might be too large to dump")
result+="</table>"
result +="<h3>Tasks:</h3><table>"
for i,curr in enumerate(self.tasks):
result += "<tr><td>%r</td><td>%s</td></tr>" % (i, repr(self.tasks[curr]) )
if (time.time()-startTimer) > timeout:
raise TimeoutException("TimeoutException occured: the dispatchTree might be too large to dump")
result+="</table>"
result +="<h3>Commands:</h3><table>"
for i,curr in enumerate(self.commands):
result += "<tr><td>%r</td><td>%s</td></tr>" % (i, self.commands[curr] )
if (time.time()-startTimer) > timeout:
raise TimeoutException("TimeoutException occured: the dispatchTree might be too large to dump")
result+="</table>"
result +="<h3>Rules:</h3><table>"
for i,curr in enumerate(self.rules):
result += "<tr><td>%r</td><td>%s</td></tr>" % (i, curr )
if (time.time()-startTimer) > timeout:
raise TimeoutException("TimeoutException occured: the dispatchTree might be too large to dump")
result+="</table>"
result +="</body></html>"
logger.info("DispatchTree printed in %.6f s" % (time.time()-startTimer) )
return result
def __init__(self):
# core data
self.root = FolderNode(0, "root", None, "root", 1, 1, 0, FifoStrategy())
self.nodes = WeakValueDictionary()
self.nodes[0] = self.root
self.pools = {}
self.renderNodes = {}
self.tasks = {}
self.rules = []
self.poolShares = {}
self.commands = {}
# deduced properties
self.nodeMaxId = 0
self.poolMaxId = 0
self.renderNodeMaxId = 0
self.taskMaxId = 0
#.........这里部分代码省略.........
示例12: DispatchTree
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import values [as 别名]
#.........这里部分代码省略.........
arguments = taskDefinition['arguments']
environment = taskDefinition['environment']
requirements = taskDefinition['requirements']
maxRN = taskDefinition['maxRN']
priority = taskDefinition['priority']
dispatchKey = taskDefinition['dispatchKey']
validationExpression = taskDefinition['validationExpression']
minNbCores = taskDefinition['minNbCores']
maxNbCores = taskDefinition['maxNbCores']
ramUse = taskDefinition['ramUse']
lic = taskDefinition['lic']
tags = taskDefinition['tags']
task = Task(None, name, None, user, maxRN, priority, dispatchKey, runner,
arguments, validationExpression, [], requirements, minNbCores,
maxNbCores, ramUse, environment, lic=lic, tags=tags)
for commandDef in taskDefinition['commands']:
description = commandDef['description']
arguments = commandDef['arguments']
task.commands.append(Command(None, description, task, arguments))
return task
## Resets the lists of elements to create or update in the database.
#
def resetDbElements(self):
self.toCreateElements = []
self.toModifyElements = []
self.toArchiveElements = []
## Recalculates the max ids of all elements. Generally called after a reload from db.
#
def recomputeMaxIds(self):
self.nodeMaxId = max([n.id for n in self.nodes.values()]) if self.nodes else 0
self.poolMaxId = max([p.id for p in self.pools.values()]) if self.pools else 0
self.renderNodeMaxId = max([rn.id for rn in self.renderNodes.values()]) if self.renderNodes else 0
self.taskMaxId = max([t.id for t in self.tasks.values()]) if self.tasks else 0
self.commandMaxId = max([c.id for c in self.commands.values()]) if self.commands else 0
self.poolShareMaxId = max([ps.id for ps in self.poolShares.values()]) if self.poolShares else 0
## Removes from the dispatchtree the provided element and all its parents and children.
#
def unregisterElementsFromTree(self, element):
# /////////////// Handling of the Task
if isinstance(element, Task):
del self.tasks[element.id]
self.toArchiveElements.append(element)
for cmd in element.commands:
self.unregisterElementsFromTree(cmd)
for node in element.nodes.values():
self.unregisterElementsFromTree(node)
# /////////////// Handling of the TaskGroup
elif isinstance(element, TaskGroup):
del self.tasks[element.id]
self.toArchiveElements.append(element)
for task in element.tasks:
self.unregisterElementsFromTree(task)
for node in element.nodes.values():
self.unregisterElementsFromTree(node)
# /////////////// Handling of the TaskNode
elif isinstance(element, TaskNode):
# remove the element from the children of the parent
if element.parent:
element.parent.removeChild(element)
if element.poolShares:
for poolShare in element.poolShares.values():
示例13: Silk
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import values [as 别名]
class Silk(SilkObject):
_anonymous = None # bool
_props = None # list
dtype = None # list
_positional_args = None # list
__slots__ = [
"_parent", "_storage_enum", "_storage_nonjson_children",
"_data", "_children", "_is_none", "__weakref__"
]
def __init__(self, *args, _mode="any", **kwargs):
self._storage_enum = None
self._storage_nonjson_children = set()
self._children = None
if _mode == "parent":
self._init(
kwargs["parent"],
kwargs["storage"],
kwargs["data_store"],
)
elif _mode == "from_numpy":
assert "parent" not in kwargs
self._init(
None,
"numpy",
kwargs["data_store"],
)
else:
assert "parent" not in kwargs
assert "storage" not in kwargs
assert "data_store" not in kwargs
self._init(None, "json", None)
if _mode == "any":
self.set(*args, **kwargs)
elif _mode == "empty":
pass
elif _mode == "from_json":
self.set(*args, prop_setter=_prop_setter_json, **kwargs)
else:
raise ValueError(_mode)
def _init(self, parent, storage, data_store):
from .silkarray import SilkArray
if parent is not None:
if storage == "numpy":
self._parent = lambda: parent # hard ref
self._parent = weakref.ref(parent)
else:
self._parent = lambda: None
self.storage = storage
self._is_none = False
self._storage_nonjson_children.clear()
if self._children is not None:
for child in self._children.values():
child._parent = lambda: None
if storage == "json":
self._children = {}
if data_store is None:
data_store = {}
elif storage == "numpy":
self._children = WeakValueDictionary()
assert data_store is not None
assert data_store.dtype == np.dtype(self.dtype, align=True)
assert data_store.shape == ()
self._data = data_store
return
else:
raise ValueError(storage)
assert storage == "json"
for pname, p in self._props.items():
if p["elementary"]:
continue
t = self._get_typeclass(pname)
if pname not in data_store:
if issubclass(t, SilkArray):
data_store[pname] = []
else:
data_store[pname] = {}
c_data_store = data_store[pname]
self._children[pname] = t(
_mode="parent",
storage="json",
parent=self,
data_store=c_data_store,
len_data_store=None,
)
self._data = data_store
def _get_typeclass(self, propname):
p = self._props[propname]
if "typeclass" in p:
t = p["typeclass"]
else:
typename = p["typename"]
t = typenames._silk_types[typename]
return t
#.........这里部分代码省略.........
示例14: __init__
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import values [as 别名]
class Entity:
def __init__(self, entType, entValue, entField):
if isinstance(entField, Field):
self.type = entType
self.value = entValue
self.field = entField
self.group = None
self.links = WeakValueDictionary() # dict of linked entities
self.field.registerEntity(self) # update the entity registry
else:
raise TypeError("Invalid field argument, field instance expected!")
def linkTo(self, eTwo):
''' Linking operation is bi-directional, affects both entities equally.'''
# check if entities not already linked
if Edge.linkId(self, eTwo) not in self.links.keys():
# update both entities' list of links
# create a new edge
newlink = Edge(self, eTwo, self.field)
self.links[newlink.id] = eTwo
eTwo.links[newlink.id] = self
# case when the first entity's group is not set
if self.group is None:
# assuming the second entity has already a group assigned
try:
eTwo.group.addMember(self)
# except the second entity has no group
except AttributeError:
newGroup = Group(self.field)
newGroup.addMember(self)
newGroup.addMember(eTwo)
# case when the first entity's group is set, but the second entity's is not
elif eTwo.group is None:
self.group.addMember(eTwo)
# case when both entities have groups set and they are different groups
elif self.group.name != eTwo.group.name:
if self.group.size > eTwo.group.size:
# first group wins
self.group.annexMembers(eTwo.group)
else:
# second group wins
eTwo.group.annexMembers(self.group)
def getLinks(self):
''' Print the list of entities directly linked.'''
return self.links.values()
def removeLink(self, eTwo):
''' Remove linked entity.'''
linkId = Edge.linkId(self, eTwo)
self.links.pop(linkId)
def __repr__(self):
return repr(self.value)
def __del__(self):
''' Delete itself from linked entities, and delete links.'''
# remove link from linked entity necessary? no because it's a weaklink
for linkId in self.links.keys():
self.field.eliminateEdge(linkId)
del self
示例15: Signal
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import values [as 别名]
class Signal(object):
def __init__(self, *args):
self.__slots = WeakValueDictionary()
for slot in args:
self.connect(slot)
def __call__(self, slot, *args, **kwargs):
"""
Emit signal. If slot passed signal will be called only for this
slot, for all connected slots otherwise.
Calling this method directly lead to immediate signal processing.
It may be not thread-safe. Use emit method from this module for
delayed calling of signals.
"""
if slot is not None:
slots = (self.__slots[self.key(slot)],)
else:
slots = self.__slots.values()
for func in slots:
func(*args, **kwargs)
def key(self, slot):
"""
Get local key name for slot.
"""
if type(slot) == types.FunctionType:
key = (slot.__module__, slot.__name__)
elif type(slot) == types.MethodType:
key = (slot.__func__, id(slot.__self__))
elif isinstance(slot, basestring):
if not slot in registred_slots.keys():
raise ValueError('Slot {0} does not exists.'.format(slot))
key = slot
else:
raise ValueError('Slot {0} has non-slot type'.format(slot))
return key
def connect(self, slot):
"""
Connect signal to slot. Slot may be function, instance method
or name of function perviously registred by `slot` decorator.
"""
key = self.key(slot)
if type(slot) == types.FunctionType:
self.__slots[key] = slot
elif type(slot) == types.MethodType:
self.__slots[key] = partial(slot.__func__, slot.__self__)
elif isinstance(slot, basestring):
self.__slots[key] = registred_slots[slot]
def disconnect(self, slot):
"""
Remove slot from signal connetions.
"""
key = self.key(slot)
del self.__slots[key]
def clear(self):
"""
Disconnect all slots from signal.
"""
self.__slots.clear()