earwax.mixins module¶
Provides various mixin classes for used with other objects.
-
class
earwax.mixins.
DismissibleMixin
(dismissible: bool = True)¶ Bases:
object
Make any
Level
subclass dismissible.Variables: dismissible – Whether or not it should be possible to dismiss this level. -
dismiss
() → None¶ Dismiss the currently active level.
By default, when used by
earwax.Menu
andearwax.Editor
, this method is called when the escape key is pressed, and only ifself.dismissible
evaluates toTrue
.The default implementation simply calls
pop_level()
on the attachedearwax.Game
instance, and announces the cancellation.
-
-
class
earwax.mixins.
DumpLoadMixin
¶ Bases:
object
A mixin that allows any object to be dumped to and loaded from a dictionary.
It is worth noting that only instance variables which have type hints (and thus end up in the
__annotations__
dictionary) will be dumped and loaded.Also, any instance variables whose name starts with an underscore (_) will be ignored.
To dump an instance, use the
dump()
method, and to load, use theload()
constructor.The
__allowed_basic_types__
list holds all the types which will be dumped without any modification.By default, the only collection types that are allowed are
list
, anddict
.If you wish to exclude attributes from being dumped or loaded, create a
__excluded_attributes__
list, and add all names there.-
dump
() → Dict[str, Any]¶ Dump this instance as a dictionary.
-
classmethod
from_file
(f: TextIO, *args) → Any¶ Return an instance from a file object.
Parameters: - f – A file which has already been opened.
- args – Extra positional arguments to pass to the
load
constructor.
-
classmethod
from_filename
(filename: pathlib.Path, *args) → Any¶ Load an instance from a filename.
Parameters: filename – The path to load from.
-
get_dump_value
(type_: Type[CT_co], value: Any) → Any¶ Get a value for dumping.
Parameters: value – The value that is present on the instance.
-
classmethod
get_load_value
(expected_type: Type[CT_co], value: Any) → Any¶ Return a loaded value.
In the event that the dumped value represents a instance of
earwax.mixins.DumpLoadValue
, the dictionary must have been returned byearwax.mixins.DumpLoadMixin.dump()
, so it contains both the dumped value, and the type annotation.This prevents errors with Union types representing multiple subclasses.
If the type of the provided value is found in the
__allowed_basic_types__
list, it will be returned as-is. This is also true if the value is an enumeration value.If the type of the provided value is
list
, then each element will be passed through this method and a list of the loaded values returned.If the type of the value is
dict
, one of two things will occur:- If
expected_type
is also a dict, then the given value will have - its keys and values loaded with this function.
- If
- If
expected_type
is also a subclass of earwax.mixins.DumpLoadMixin
, then it will be loaded with that class’sload
method.
- If
- If neither of these things are true,
RuntimeError
will be raised.
Parameters: - expected_type – The type from the
__annotations__
dictionary. - value – The raw value to load.
-
classmethod
load
(data: Dict[str, Any], *args) → Any¶ Load and return an instance from the provided data.
It is worth noting that only keys that are also found in the
__attrs_attrs__
list, or__annotations__
dictionary, and not found in the__excluded_attribute_names__
list will be loaded. All others are ignored.Parameters: - data – The data to load from.
- args – Extra positional arguments to pass to the constructor.
-
save
(filename: pathlib.Path) → None¶ Write this object to the provided filename.
Parameters: filename – The path to the file to dump to.
-
-
class
earwax.mixins.
RegisterEventMixin
¶ Bases:
pyglet.event.EventDispatcher
Allow registering and binding events in one function.
-
register_and_bind
(func: EventType) → EventType¶ Register and bind a new event.
This is the same as:
level.register_event_type('f') @level.event def f() -> None: pass
Parameters: func – The function whose name will be registered, and which will be bound to this instance.
-
register_event
(func: EventType) → str¶ Register an event type from a function.
This function uses
func.__name__
to register an event type, eliminating possible typos in event names.Parameters: func – The function whose name will be used.
-
-
class
earwax.mixins.
TitleMixin
(title: Union[str, TitleFunction])¶ Bases:
object
Add a title to any
Level
subclass.Variables: title – The title of this instance.
If this value is a callable, it should return a string which will be used as the title.
-
get_title
() → str¶ Return the proper title of this object.
If
self.title
is a callable, its return value will be returned.
-