The ideal thing is to have a schema to make a dynamic import of files in the same folder. This code can run in the file __init__ from a folder - and as soon as you import that folder as a package, all the files .py of it are read.
Traditionally, the way to import a module from a string (i.e., a data, in contrast to the module name typed directly into the program as a parameter for the command import) is with the call __import__. Historical questions, however, its use is more complicated than it needs to be -with a single parameter it ends up returning the root package of the string you passed (in your example, the package data)
Then, in a generic way, it is possible to make an import that brings everything you have in the directory. In your case, as you want to make the imported classes available in a data structure (a list), it might be worth doing something more targeted:
from importlib import import_module
from pathlib import Path
def import_creatures(package_name):
root_package = import_module("package_name")
package_path = Path(root_package.__file__).parent
creature_classes = []
for source_file in package_path.glob("*.py"):
module = import_module(source_file)
name = source_file.stem
# Tenta importar a classe se tiver nome comecando com letra maiuscula também
cls = getattr(module, name, getattr(module, name.title(), None))
if not cls:
continue
creature_classes.append(cls)
return creature_classes
MONSTERS = import_creatures("data.creatures.monster_list")
This code always tries to get the class that has the same name as the file, as in your example - but it is also possible to insert the module (taking module.__dict__.items() for example), and take all objects that are subclass of a specific class - if all of yours inherit from "Monster" for example - then you can quietly have more than one monster definition per file, and still have freedom to name the classes.
Changing the subject -and this game? Will be released? Is in public repository? We can contribute?? :-)
Another note, like the first package of your import name is data, your project is probably not yet structured itself as a Python package that can be installed - and it will only work if you run the main program from the folder where it is (and then, in that folder there is the folder data, but that for Python is an independent package of your project).
Ideally, your project directory has a __init__.py, and you work from a folder above, where with a file setup.py minimum, you can type pip install --editable . so that your game is seen as "all". In this case, the Imports will change to meujogo.data.creatures.... , or a "." in the front - .data.creatures... - but the whole project gets a "feeling" of a single product - it’s much cooler.
The setup.py you need is something like:
from setuptools import setup
setup(
name='route_optimizer',
version='0.1',
author='Thomas Caio',
py_packages=['meujogo'],
install_requires=[],
)
Why is each dictionary set in separate files? Could you set them all in the same file?
– Woss
Could, but I wanted to leave it separate to facilitate the modifications and not to be a single giant file.
– Thomas Caio