Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Env variable for faster Python imports (#1175) #1388

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Next Next commit
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'.
dbogdanov committed Dec 1, 2023
commit 235ae3c93f8812aea7e5b034a7565d91a7be2534
21 changes: 17 additions & 4 deletions src/python/essentia/standard.py
Original file line number Diff line number Diff line change
@@ -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
20 changes: 16 additions & 4 deletions src/python/essentia/streaming.py
Original file line number Diff line number Diff line change
@@ -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