Я довольно начинающий разработчик Python, работающий над чужим кодом.Он написал модуль, который зависел от __init__.py
, предоставляя список модулей, доступных для динамического вызова как часть приложения колбы.
В Python 3.3+, если присутствует файл __init__.py
, модули не 'т нашел.Если мы удалим __init__.py
, обнаружение динамического модуля не будет работать.
Мы попытались исправить невозможность найти модуль, следуя инструкциям в нескольких сообщениях о новом __init__.py
vs sys.path() поведение в 3.3+ и удаление init .py.Это устраняет невозможность найти модуль при импорте, но нарушает динамическое обнаружение.
__init__.py
from os.path import dirname, basename, isfile
import glob
modules = glob.glob(dirname(__file__)+"/*.py")
__all__ = [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]
base_processor.py
class BaseProcessor(object):
"""
This is a base class for all processors that handle API requests from AWS API Gateway
Attributes:
MAX_SIZE The maximum size that elastic search accepts
"""
MAX_SIZE = 214748364
def __init__(self):
super(BaseProcessor, self).__init__()
def getApiConfig(self):
"""
This method defines what this processor handles and is required to be defined so the
dynamic loading of all processors functions correctly. Subclasses should overide it.
"""
raise Exception("Concrete implementation of this BaseProcessor class did not implement the getApiConfig function.")
def process(self, event):
"""
This method is the entry point for all processors and is expected to be
implemented by the sub class. Its base implementation will throw an error
"""
raise Exception("Concrete implementation of this BaseProcessor class did not implement the process function.")
def getFirstAgregationKey(self, results, field_name):
return_results = ""
if field_name in results and len(results[field_name]["buckets"]) > 0:
return_results = results[field_name]["buckets"][0]["key"]
return return_results
Код, который вызываетbase_processor для получения списка доступных процессоров.
processors = []
EventProcessors = BaseProcessor.__subclasses__()
for processor in EventProcessors:
processor_instance = processor()
processors.append(processor_instance.getApiConfig())
При запуске приложения фляги массив процессоров имеет нулевое значение, и ни один из вызовов app.route () не находит процессор для обработки своего вызова.