DrInfy / sharpy-sc2

Python framework for rapid development of Starcraft 2 AI bots
MIT License
69 stars 28 forks source link

Add mechanism for requesting manager by name or type #54

Closed merfolk closed 4 years ago

merfolk commented 4 years ago

sharpy provides a mechanism for registering new managers by overriding KnowledgeBot.configure_managers(self) -> Optional[List[ManagerBase]]).

The additional managers are then passed to Knowledge.pre_start(...)

Sharpy's internal managers are available through hard coded properties in Knowledge. However, there's no easy mechanism for accessing the added managers.

Describe the solution you'd like

Save all managers to eg. a Dict instead of a List in Knowledge.

Add a new method to Knowledge and/or KnowledgeBot, that can be used to get an added manager by eg. EITHER it's name as a string OR the manager type.

Using type as key has the benefit of some "compile-time" checks. It prevents adding multiple instances of the same manager (probably not needed).

Using string as key has the con that errors are only detected at runtime, resulting in a longer feedback loop. It has the added benefit of allowing multiple instances of the same manager to be added (probably not needed).

Also, please update relevant Wiki docs, including at least Extending Managers.

Additional information

It's important to preserve the order of sharpy's own managers, because the order affects how they are updated.

Dictionaries preserve insertion order. Note that updating a key does not affect the order. Keys added after deletion are inserted at the end.

Changed in version 3.7: Dictionary order is guaranteed to be insertion order. This behavior was an implementation detail of CPython from 3.6.

source: https://docs.python.org/3.7/library/stdtypes.html#dict.values