Skip to content

Commit

Permalink
Env variable for faster Python imports (#1175)
Browse files Browse the repository at this point in the history
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'.
  • Loading branch information
dbogdanov committed Dec 1, 2023
1 parent 0a94448 commit 235ae3c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
21 changes: 17 additions & 4 deletions src/python/essentia/standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions src/python/essentia/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 235ae3c

Please sign in to comment.