本文整理汇总了Python中weakref.WeakKeyDictionary.get方法的典型用法代码示例。如果您正苦于以下问题:Python WeakKeyDictionary.get方法的具体用法?Python WeakKeyDictionary.get怎么用?Python WeakKeyDictionary.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weakref.WeakKeyDictionary
的用法示例。
在下文中一共展示了WeakKeyDictionary.get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ShapeManager
# 需要导入模块: from weakref import WeakKeyDictionary [as 别名]
# 或者: from weakref.WeakKeyDictionary import get [as 别名]
class ShapeManager( object ):
def __init__( self, canvas ):
self.canvasItemToPartMap = WeakKeyDictionary()
self.canvasItemToShapeMap = WeakKeyDictionary()
self.canvas = canvas
def associateCanvasItemWithShape( self, canvasItem, shape ):
self.canvasItemToShapeMap[ canvasItem ] = ref( shape )
def associateCanvasItemWithPart( self, canvasItem, part ):
self.canvasItemToPartMap[ canvasItem ] = mrt
def getPartByCanvasItem( self, canvasItem ):
return self.canvasItemToPartMap.get( canvasItem, None )
def getShapeByCanvasItem( self, canvasItem ):
r = self.canvasItemToShapeMap.get( canvasItem, None )
if r != None:
return r()
return None
def createComplexShape( self, molds, modelObject = None ):
return ComplexShape( self, self.canvas, molds, modelObject )
def createConnector( self, modelObject = None ):
return Connector( self, self.canvas, modelObject )
示例2: SettingsObjectID
# 需要导入模块: from weakref import WeakKeyDictionary [as 别名]
# 或者: from weakref.WeakKeyDictionary import get [as 别名]
class SettingsObjectID(object):
"""
Simple descriptor used for SettingsObject subclasses which have dynamic ids.
"""
def __init__(self, type):
self.type = type
self.values = WeakKeyDictionary()
self.oldvalues = WeakKeyDictionary()
self.dirty = WeakKeyDictionary()
def __get__(self, obj, objtype):
if obj is None:
return self
try:
return self.values[obj]
except KeyError:
raise AttributeError("SettingsObject ID has not been defined")
def __set__(self, obj, value):
if not isinstance(value, self.type):
value = self.type(value)
# check whether the old value is the same as the new value
if obj in self.values and self.values[obj] == value:
return
self.dirty[obj] = obj in self.values
self.oldvalues[obj] = self.values.get(obj, value)
self.values[obj] = value
def isdirty(self, obj):
"""
Returns True if the ID has been changed on the specified configuration
object.
"""
return self.dirty.get(obj, False)
def clear_dirty(self, obj):
"""
Clears the dirty flag for the ID on the specified configuration object.
"""
del self.dirty[obj]
self.oldvalues[obj] = self.values[obj]
def get_old(self, obj):
return self.oldvalues.get(obj, None)
def undo(self, obj):
if obj in self.oldvalues:
self.dirty.pop(obj, None)
self.values[obj] = self.oldvalues[obj]
示例3: __init__
# 需要导入模块: from weakref import WeakKeyDictionary [as 别名]
# 或者: from weakref.WeakKeyDictionary import get [as 别名]
class PropertyDescriptor:
''' Base Property Descriptor
'''
def __init__(self):
self.data = WeakKeyDictionary()
def __get__(self, instance, type=None):
return self.data.get(instance)
def __set__(self, instance, value):
self.data[instance] = value
def __delete__(self, instance):
if self.data.get(instance):
del self.data[instance]
示例4: Property
# 需要导入模块: from weakref import WeakKeyDictionary [as 别名]
# 或者: from weakref.WeakKeyDictionary import get [as 别名]
class Property(object):
"""property
"""
def __init__(self, default=None):
self.default = default
self.data = WeakKeyDictionary()
def __get__(self, instance, owner):
if instance is None:
return self
if instance in self.data:
return self.data.get(instance, self.default)
else:
return self.default
def __set__(self, instance, value):
if issubclass(type(value), dict):
prop_name = None
for prop in instance.properties():
prop_descriptor = getattr(type(instance), prop)
if prop_descriptor == self:
prop_name = prop
if prop_name not in ['images']:
print("warning : type %s property %s is set to value %s" % (type(instance).__name__, prop_name, value))
self.data[instance] = value
示例5: Field
# 需要导入模块: from weakref import WeakKeyDictionary [as 别名]
# 或者: from weakref.WeakKeyDictionary import get [as 别名]
class Field(object):
"""
Basic field class. Used to store values.
"""
datatype = DataTypes.string
def __init__(self, default=None, label=None, help_text=None, scope=None):
if getattr(self, 'value', None) is None:
self.value = default
self.label = label
self.help_text = help_text
self.scope = scope
self.default = default
self.data = WeakKeyDictionary()
def __get__(self, obj, obj_type):
if obj is None:
return self
return self.from_json(self.data.get(obj, self.default))
def __set__(self, obj, value):
self.data[obj] = self.to_json(value)
@classmethod
def from_json(cls, value):
return value
@classmethod
def to_json(cls, value):
return value
示例6: Type
# 需要导入模块: from weakref import WeakKeyDictionary [as 别名]
# 或者: from weakref.WeakKeyDictionary import get [as 别名]
class Type(object):
"""
Specifies of which type the property has to be
"""
def __init__(self, typ, default=None, cons=None):
self.default = default
self.cons = cons
self.data = WeakKeyDictionary()
self.type = typ
def __get__(self, instance, owner):
if instance in self.data:
return self.data.get(instance, self.default)
else:
return self.default
def __set__(self, instance, value):
if value:
if isinstance(value, self.type):
self.data[instance] = value
elif self.type in [str, int, float, bool]:
self.data[instance] = self.type(value)
elif self.cons:
self.data[instance] = self.cons(value)
else:
raise ValueError("Value has wrong type! {} != {}".format(type(value), self.type))
示例7: FrameRegistry
# 需要导入模块: from weakref import WeakKeyDictionary [as 别名]
# 或者: from weakref.WeakKeyDictionary import get [as 别名]
class FrameRegistry(object):
"""
This class implements a method to keep track of wrapped frames. In short,
Qt will give us frames that are QWebFrames, and we want to only deal with
WebFrames that we control. This registry will either wrap a given frame,
or return the previously-wrapped one from the registry.
"""
def __init__(self, klass, *args, **kwargs):
self._registry = WeakKeyDictionary()
self.klass = klass
self.args = args
self.kwargs = kwargs
def wrap(self, frame):
if frame is None:
return None
existing = self._registry.get(frame)
if existing is not None:
return existing
# Create web frame, passing the underlying value, and ourselves.
new = self.klass(frame, self, *self.args, **self.kwargs)
self._registry[frame] = new
return new
def clear(self):
self._registry.clear()
示例8: NonNegative
# 需要导入模块: from weakref import WeakKeyDictionary [as 别名]
# 或者: from weakref.WeakKeyDictionary import get [as 别名]
class NonNegative(object):
"""A descriptor that forbids negative values"""
def __init__(self, default):
self.default = default
self.data = WeakKeyDictionary()
def __get__(self, instance, owner):
# we get here when someone calls x.d, and d is a NonNegative instance
# instance = x
# owner = type(x)
return self.data.get(instance, self.default)
def __set__(self, instance, value):
# we get here when someone calls x.d = val, and d is a NonNegative instance
# instance = x
# value = val
print "NonNegative descriptor getter:\n \
self = {},\n \
instance = {},\n \
value = {}".format(
self, instance, value
)
if value < 0:
raise ValueError("Negative value not allowed: %s" % value)
self.data[instance] = value
示例9: Strucutre
# 需要导入模块: from weakref import WeakKeyDictionary [as 别名]
# 或者: from weakref.WeakKeyDictionary import get [as 别名]
class Strucutre(INodeInvokerListener):
'''
The general structure.
'''
__slots__ = ('structNodes', 'typesPaths')
def __init__(self):
'''
Construct the structure.
'''
self.structNodes = WeakKeyDictionary()
self.typesPaths = WeakKeyDictionary()
def onInvokerChange(self, node, old, new):
'''
@see: INodeInvokerListener.onInvokerChange
'''
self.structNodes.pop(node, None)
self.typesPaths.pop(node, None)
# ----------------------------------------------------------------
def process(self, node, invoker):
'''
Process the structure for the provided node and invoker.
@param node: Node
The node to process for.
@param invoker: Invoker
The invoker to process.
@return: tuple(list[TypeProperty], @see: StructNode.process)|None
The list of path property types.
'''
assert isinstance(node, Node), 'Invalid node %s' % node
structNode = self.structNodes.get(node)
if structNode is None: structNode = self.structNodes[node] = StructNode()
assert isinstance(structNode, StructNode)
data = structNode.process(invoker)
if data is None: return
typesPath = self.typesPaths.get(node)
if typesPath is None: typesPath = self.typesPaths[node] = (propertyTypesOf(node, invoker),)
return typesPath + data
示例10: DbiDriver
# 需要导入模块: from weakref import WeakKeyDictionary [as 别名]
# 或者: from weakref.WeakKeyDictionary import get [as 别名]
class DbiDriver(object):
def __init__(self, app_name, connection_data, after_create_connection=None):
self._app_name = app_name
self.__connect_parameters = self.getConnectionParameters(connection_data)
self.__connections = WeakKeyDictionary()
self.__after_create_connection = after_create_connection
def getConnectionParameters(self, connection_data):
return [], connection_data
def createConnection(self):
args, kwargs = self.__connect_parameters
return Connection(self.package, *args, **kwargs)
def _getConnection(self):
thread = threading.currentThread()
return self.__connections.get(thread)
def getConnection(self):
thread = threading.currentThread()
connection = self.__connections.get(thread)
if connection is None:
connection = self.__connections[thread] = self.createConnection()
if self.__after_create_connection:
self.__after_create_connection(self)
return connection
def closeConnection(self):
thread = threading.currentThread()
connection = self.__connections.get(thread)
if connection is not None:
del self.__connections[thread]
try:
connection.close()
except Exception, e:
print "* failed to close connection: %s: %s" % (e.__class__.__name__, e)
示例11: ObjectDescriptor
# 需要导入模块: from weakref import WeakKeyDictionary [as 别名]
# 或者: from weakref.WeakKeyDictionary import get [as 别名]
class ObjectDescriptor(object):
def __init__(self):
self.data = WeakKeyDictionary()
def __get__(self, instance, owner):
# print "(__get__) instance: %s, owner: %s" % (instance, owner)
return self.data.get(instance)
def __set__(self, instance, value):
# print "(__set__) instance: %s, value: %s" % (instance, value)
self.data[instance] = value
示例12: __init__
# 需要导入模块: from weakref import WeakKeyDictionary [as 别名]
# 或者: from weakref.WeakKeyDictionary import get [as 别名]
class Subs:
"""a 'reusable' property"""
def __init__(self, default=None):
self.default = normalize_subs_input(default)
self.data = WeakKeyDictionary()
def __get__(self, instance, owner):
return self.data.get(instance, self.default)
def __set__(self, instance, value):
self.data[instance] = normalize_subs_input(value)
示例13: Grade
# 需要导入模块: from weakref import WeakKeyDictionary [as 别名]
# 或者: from weakref.WeakKeyDictionary import get [as 别名]
class Grade(object):
def __init__(self):
self._values = WeakKeyDictionary()
def __get__(self, instance, instance_type):
if instance is None: return self
return self._values.get(instance, 0)
def __set__(self, instance, value):
if not (0 <= value <= 100):
raise ValueError('Grade must be between 0 and 100')
self._values[instance] = value
示例14: NonNegative
# 需要导入模块: from weakref import WeakKeyDictionary [as 别名]
# 或者: from weakref.WeakKeyDictionary import get [as 别名]
class NonNegative(object):
"""A descriptor that forbids negative values"""
def __init__(self,default):
self.default= default
self.data= WeakKeyDictionary()
def __get__(self, instance, owner):
return self.data.get(instance,self.default)
def __set__(self, instance, value):
if value<0:
raise ValueError("Negative value not allowed: %s" % value)
self.data[instance]=value
示例15: Descriptor
# 需要导入模块: from weakref import WeakKeyDictionary [as 别名]
# 或者: from weakref.WeakKeyDictionary import get [as 别名]
class Descriptor(object):
def __init__(self, default=None):
self.data = WeakKeyDictionary()
self.default = default
self.callbacks = WeakKeyDictionary()
def __get__(self, instance, owner):
print "****",instance
if instance is None:
return self
return self.data.get(instance, self.default)
def __set__(self, instance, value):
for callback in self.callbacks.get(instance, []):
callback(value)
self.data[instance] = value
def add_callback(self, instance, callback):
if instance not in self.callbacks:
self.callbacks[instance] = []
self.callbacks[instance].append(callback)