Add for_contents() which runs a function on an object's contents.

This commit is contained in:
Ahmed Charles 2015-10-24 00:54:57 +00:00 committed by Griatch
parent 8a66fc40a9
commit 53227cda3e

View file

@ -452,6 +452,24 @@ class DefaultObject(with_metaclass(TypeclassBase, ObjectDB)):
if sessions:
sessions[0].msg(text=text, session=sessions, **kwargs)
def for_contents(self, func, exclude=None, **kwargs):
"""
Runs a function on every object contained within this one.
Args:
func (callable): Function to call.
exclude (list, optional): A list of object not to call the function on.
Kwargs:
Keyword arguments will be passed to the function for all objects.
"""
contents = self.contents
if exclude:
exclude = make_iter(exclude)
contents = [obj for obj in contents if obj not in exclude]
for obj in contents:
func(obj, **kwargs)
def msg_contents(self, message, exclude=None, from_obj=None, **kwargs):
"""
Emits a message to all objects inside this object.