本文整理汇总了Python中weakref.WeakValueDictionary.setdefault方法的典型用法代码示例。如果您正苦于以下问题:Python WeakValueDictionary.setdefault方法的具体用法?Python WeakValueDictionary.setdefault怎么用?Python WeakValueDictionary.setdefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weakref.WeakValueDictionary
的用法示例。
在下文中一共展示了WeakValueDictionary.setdefault方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: PQ
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import setdefault [as 别名]
class PQ(object):
"""Convenient queue manager."""
table = 'queue'
template_path = os.path.dirname(__file__)
def __init__(self, *args, **kwargs):
self.queue_class = kwargs.pop('queue_class', Queue)
self.params = args, kwargs
self.queues = WeakValueDictionary()
def __getitem__(self, name):
try:
return self.queues[name]
except KeyError:
return self.queues.setdefault(
name, self.queue_class(name, *self.params[0], **self.params[1])
)
def close(self):
self[''].close()
def create(self):
queue = self['']
with open(os.path.join(self.template_path, 'create.sql'), 'r') as f:
sql = f.read()
with queue._transaction() as cursor:
cursor.execute(sql, {'name': Literal(queue.table)})
示例2: PQ
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import setdefault [as 别名]
class PQ(object):
"""Convenient queue manager."""
table = 'queue'
def __init__(self, *args, **kwargs):
self.params = args, kwargs
self.queues = WeakValueDictionary()
def __getitem__(self, name):
try:
return self.queues[name]
except KeyError:
return self.queues.setdefault(
name, Queue(name, *self.params[0], **self.params[1])
)
def close(self):
self[''].close()
def create(self):
q = self['']
conn = q._conn()
sql = _read_sql('create')
with transaction(conn) as cursor:
cursor.execute(sql, {'name': Literal(q.table)})
示例3: RiakClient
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import setdefault [as 别名]
#.........这里部分代码省略.........
def bucket(self, name, bucket_type='default'):
"""
Get the bucket by the specified name. Since buckets always exist,
this will always return a
:class:`RiakBucket <riak.bucket.RiakBucket>`.
If you are using a bucket that is contained in a bucket type, it is
preferable to access it from the bucket type object::
# Preferred:
client.bucket_type("foo").bucket("bar")
# Equivalent, but not preferred:
client.bucket("bar", bucket_type="foo")
:param name: the bucket name
:type name: str
:param bucket_type: the parent bucket-type
:type bucket_type: :class:`BucketType <riak.bucket.BucketType>`
or str
:rtype: :class:`RiakBucket <riak.bucket.RiakBucket>`
"""
if not isinstance(name, string_types):
raise TypeError('Bucket name must be a string')
if isinstance(bucket_type, string_types):
bucket_type = self.bucket_type(bucket_type)
elif not isinstance(bucket_type, BucketType):
raise TypeError('bucket_type must be a string '
'or riak.bucket.BucketType')
return self._buckets.setdefault((bucket_type, name),
RiakBucket(self, name, bucket_type))
def bucket_type(self, name):
"""
Gets the bucket-type by the specified name. Bucket-types do
not always exist (unlike buckets), but this will always return
a :class:`BucketType <riak.bucket.BucketType>` object.
:param name: the bucket-type name
:type name: str
:rtype: :class:`BucketType <riak.bucket.BucketType>`
"""
if not isinstance(name, string_types):
raise TypeError('BucketType name must be a string')
if name in self._bucket_types:
return self._bucket_types[name]
else:
btype = BucketType(self, name)
self._bucket_types[name] = btype
return btype
def table(self, name):
"""
Gets the table by the specified name. Tables do
not always exist (unlike buckets), but this will always return
a :class:`Table <riak.table.Table>` object.
:param name: the table name
:type name: str
:rtype: :class:`Table <riak.table.Table>`
"""
示例4: __init__
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import setdefault [as 别名]
#.........这里部分代码省略.........
btype = BucketType(self, name)
self._bucket_types[name] = btype
return btype
def bucket(self, name, bucket_type='default'):
'''
Get the bucket by the specified name. Since buckets always exist,
this will always return a
:class:`Bucket <aioriak.bucket.Bucket>`.
If you are using a bucket that is contained in a bucket type, it is
preferable to access it from the bucket type object::
# Preferred:
client.bucket_type("foo").bucket("bar")
# Equivalent, but not preferred:
client.bucket("bar", bucket_type="foo")
:param name: the bucket name
:type name: str
:param bucket_type: the parent bucket-type
:type bucket_type: :class:`BucketType <aioriak.bucket.BucketType>`
or str
:rtype: :class:`Bucket <aioriak.bucket.Bucket>`
'''
if not isinstance(name, str):
raise TypeError('Bucket name must be a string')
if isinstance(bucket_type, str):
bucket_type = self.bucket_type(bucket_type)
elif not isinstance(bucket_type, BucketType):
raise TypeError('bucket_type must be a string '
'or aioriak.bucket.BucketType')
return self._buckets.setdefault((bucket_type, name),
Bucket(self, name, bucket_type))
async def get_bucket_type_props(self, bucket_type):
'''
Fetches properties for the given bucket-type.
:param bucket_type: the bucket-type whose properties will be fetched
:type bucket_type: BucketType
:rtype: dict
'''
return await self._transport.get_bucket_type_props(bucket_type)
async def set_bucket_type_props(self, bucket_type, props):
'''
Sets properties for the given bucket-type.
:param bucket_type: the bucket-type whose properties will be set
:type bucket_type: BucketType
:param props: the properties to set
:type props: dict
'''
return await self._transport.set_bucket_type_props(bucket_type, props)
async def get_bucket_props(self, bucket):
'''
Fetches bucket properties for the given bucket.
:param bucket: the bucket whose properties will be fetched
:type bucket: Bucket
:rtype: dict
'''
return await self._transport.get_bucket_props(bucket)