sopel.tools.memories#
Thread-safe memory data-structures for Sopel.
Sopel uses lots of threads to manage rules and jobs and other features, and it needs to store shared information safely. This class contains various memory classes that are thread-safe, with some convenience features.
- class sopel.tools.memories.SopelIdentifierMemory(
- *args: MemoryConstructorInput,
- identifier_factory: IdentifierFactory = Identifier,
Special Sopel memory that stores
Identifieras key.This is a convenient subclass of
SopelMemorythat always casts its keys as instances ofIdentifier:>>> from sopel import tools >>> memory = tools.SopelIdentifierMemory() >>> memory['Exirel'] = 'king' >>> list(memory.items()) [(Identifier('Exirel'), 'king')] >>> tools.Identifier('exirel') in memory True >>> 'exirel' in memory True
As seen in the example above, it is possible to perform various operations with both
Identifierandstrobjects, taking advantage of the case-insensitive behavior ofIdentifier.As it works with
Identifier, it accepts an identifier factory. This factory usually comes from abot instance, like in the example of a plugin setup function:def setup(bot): bot.memory['my_plugin_storage'] = SopelIdentifierMemory( identifier_factory=bot.make_identifier, )
Note
Internally, it will try to do
key = self.make_identifier(key), which will raise an exception if it cannot instantiate the key properly:>>> memory[1] = 'error' AttributeError: 'int' object has no attribute 'translate'
New in version 7.1.
Changed in version 8.0: Moved from
toolstotools.memories.The parameter
identifier_factoryhas been added to properly transformstrintoIdentifier. This factory is stored and accessible throughmake_identifier.- copy()#
Get a shallow copy of this
SopelIdentifierMemory.See
dict.copy().
- get( ) Any#
Get the value of
keyfrom thisSopelIdentifierMemory.Takes an optional
defaultvalue, just likedict.get().
- make_identifier#
A factory to transform keys into identifiers.
- pop( ) Any#
Pop the value of
keyfrom thisSopelIdentifierMemory.Takes an optional
defaultvalue, just likedict.pop().
- setdefault( ) Any#
Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.
- update(maybe_mapping=tuple())#
Update this
SopelIdentifierMemorywith key-value pairs.See
dict.update().
- class sopel.tools.memories.SopelMemory(*args)#
A simple thread-safe
dictimplementation.In order to prevent exceptions when iterating over the values and changing them at the same time from different threads, we use a blocking lock in
__setitem__and__contains__.Note
Unlike the
dicton which they are based,SopelMemoryand its derivative types do not accept key-value pairs as keyword arguments at construction time.New in version 3.1: As
Willie.WillieMemoryChanged in version 4.0: Moved to
tools.WillieMemoryChanged in version 6.0: Renamed from
WillieMemorytoSopelMemoryChanged in version 8.0: Moved from
toolstotools.memories
- class sopel.tools.memories.SopelMemoryWithDefault(*args)#
Same as SopelMemory, but subclasses from collections.defaultdict.
Note
Unlike the
defaultdicton which it is based,SopelMemoryWithDefaultdoes not accept key-value pairs as keyword arguments at construction time.New in version 4.3: As
WillieMemoryWithDefaultChanged in version 6.0: Renamed to
SopelMemoryWithDefaultChanged in version 8.0: Moved from
toolstotools.memories