From 235ae3c93f8812aea7e5b034a7565d91a7be2534 Mon Sep 17 00:00:00 2001 From: Dmitry Bogdanov Date: Fri, 1 Dec 2023 19:47:10 +0100 Subject: [PATCH] Env variable for faster Python imports (#1175) New `ESSENTIA_PYTHON_NODOC` env variable for faster Python package imports. Skip loading an instance of each algorithm to populate __doc__ and __struct__ fields in its wrapper class, when this varaible equals to 'True', 'true' or '1'. --- src/python/essentia/standard.py | 21 +++++++++++++++++---- src/python/essentia/streaming.py | 20 ++++++++++++++++---- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/python/essentia/standard.py b/src/python/essentia/standard.py index ea0e7d059..15f184852 100644 --- a/src/python/essentia/standard.py +++ b/src/python/essentia/standard.py @@ -22,15 +22,28 @@ import sys as _sys from ._essentia import keys as algorithmNames, info as algorithmInfo from copy import copy +from os import getenv + + +# Whether to skip loading algorithms for reading their metadata (faster import). +ESSENTIA_PYTHON_NODOC = getenv('ESSENTIA_PYTHON_NODOC', False) +ESSENTIA_PYTHON_NODOC = (ESSENTIA_PYTHON_NODOC == 'True' or + ESSENTIA_PYTHON_NODOC == 'true' or + ESSENTIA_PYTHON_NODOC == '1') # given an essentia algorithm name, create the corresponding class def _create_essentia_class(name, moduleName = __name__): essentia.log.debug(essentia.EPython, 'Creating essentia.standard class: %s' % name) - _algoInstance = _essentia.Algorithm(name) - _algoDoc = _algoInstance.getDoc() - _algoStruct = _algoInstance.getStruct() - del _algoInstance + + if not ESSENTIA_PYTHON_NODOC or name == "FrameCutter": + _algoInstance = _essentia.Algorithm(name) + _algoDoc = _algoInstance.getDoc() + _algoStruct = _algoInstance.getStruct() + del _algoInstance + else: + _algoDoc = None + _algoStruct = None class Algo(_essentia.Algorithm): __doc__ = _algoDoc diff --git a/src/python/essentia/streaming.py b/src/python/essentia/streaming.py index e8e458fa9..0fdbec29e 100644 --- a/src/python/essentia/streaming.py +++ b/src/python/essentia/streaming.py @@ -21,6 +21,14 @@ import sys as _sys from . import common as _c from ._essentia import skeys as algorithmNames, sinfo as algorithmInfo +from os import getenv + + +# Whether to skip loading algorithms for reading their metadata (faster import). +ESSENTIA_PYTHON_NODOC = getenv('ESSENTIA_PYTHON_NODOC', False) +ESSENTIA_PYTHON_NODOC = (ESSENTIA_PYTHON_NODOC == 'True' or + ESSENTIA_PYTHON_NODOC == 'true' or + ESSENTIA_PYTHON_NODOC == '1') # Used as a place-holder for sources and sinks, implements the right shift # operator @@ -139,10 +147,14 @@ def totalProduced(self): def _create_streaming_algo(givenname): essentia.log.debug(essentia.EPython, 'Creating essentia.streaming class: %s' % givenname) - _algoInstance = _essentia.StreamingAlgorithm(givenname) - _algoDoc = _algoInstance.getDoc() - _algoStruct = _algoInstance.getStruct() - del _algoInstance + if not ESSENTIA_PYTHON_NODOC or givenname == 'FrameCutter': + _algoInstance = _essentia.StreamingAlgorithm(givenname) + _algoDoc = _algoInstance.getDoc() + _algoStruct = _algoInstance.getStruct() + del _algoInstance + else: + _algoDoc = None + _algoStruct = None class StreamingAlgo(_essentia.StreamingAlgorithm): __doc__ = _algoDoc