def _load_module(self): """ Add variables and functions to the config dictionary, via the python module (located in the same directory as the Yaml config file). This function enriches the variables dictionary The python module must contain the following hook: declare_env(env): "Declare environment for jinja2 templates for markdown" env.variables['a'] = 5 @env.macro def bar(x): ... @env.macro def baz(x): ... @env.filter def foobar(x): ... """ config = self.conf # determine the package name, from the filename: python_module = self.config['module_name'] repackage.add(self.project_dir) try: module = importlib.import_module(python_module) trace("Found external Python module '%s' in:" % python_module, self.project_dir) # execute the hook, passing the template decorator function function_found = False if hasattr(module, 'define_env'): module.define_env(self) function_found = True if hasattr(module, 'declare_variables'): module.declare_variables(self.variables, self.macro) trace("You are using declare_variables() in the python " "module '%s'. Prefer the define_env() function " "(see documentation)!" % python_module) function_found = True if not function_found: raise NameError("No valid function found in module '%s'" % config_file) except ImportError: if python_module == DEFAULT_MODULE_NAME: # do not do anything if there is no main module pass else: raise ImportError("Macro plugin could not find custom '%s' " "module in '%s'." % (python_module, self.project_dir))
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE SOFTWARE CODE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Howto: Write variables in Azure Devops # To set pipeline variables in scripts: #print ('##vso[task.setvariable variable=esml_environment]dev_from_python') #print('##vso[task.setvariable variable=secret.Sauce;issecret=true]crushed tomatoes with garlic') #print (f'##vso[task.setvariable variable=esml_environment]{p.dev_test_prod}') # END example. Where you can use this to set ESMProject.dev_test_prod = args.esml_env """ import repackage #repackage.add("../../esml/common/") repackage.add("../../azure-enterprise-scale-ml/esml/common/") import azureml.core from azureml.core.authentication import AzureCliAuthentication from esml import ESMLProject print("SDK Version:", azureml.core.VERSION) p = ESMLProject.get_project_from_env_command_line() # Alt A) if(p is None): # Alt B) Just for DEMO purpose..its never None p = ESMLProject() # B)= Reads from CONFIG instead - To control this, use GIT-branching and .gitignore on "active_dev_test_prod.json" for each environment print("DEMO MLOPS FOLDER settings - remove this after you copies this folder)") # remove this after you copies this folder print("ESML environment (dev, test or prod): {}".format(p.dev_test_prod)) p.describe() cli_auth = AzureCliAuthentication() ws, config_name = p.authenticate_workspace_and_write_config(cli_auth) # Authenticat to the current environment (dev,test, prod) and WRITES config.json | Use CLI auth if MLOps
def load_variables(variables, config): """ Add the template functions, via the python module located in the same directory as the Yaml config file. The python module must contain the following hook: declare_variables(variables, macro): variables['a'] = 5 @macro def bar(x): .... @macro def baz(x): .... """ def macro(v, name=''): """ Registers a variable as a macro in the template, i.e. in the variables dictionary: macro(myfunc) Optionally, you can assign a different name: macro(myfunc, 'funcname') You can also use it as a decorator: @macro def foo(a): return a ** 2 More info: https://stackoverflow.com/questions/6036082/call-a-python-function-from-jinja2 """ name = name or v.__name__ variables[name] = v return v # determine the package name, from the filename: python_module = config.get('python_module') or DEFAULT_MODULE_NAME # get the directory of the yaml file: config_file = config['config_file_path'] yaml_dir = os.path.dirname(config_file) # print("Found yaml directory: %s" % yaml_dir) # that's the directory of the package: repackage.add(yaml_dir) try: module = importlib.import_module(python_module) print("Found module '%s'" % python_module) # execute the hook, passing the template decorator function module.declare_variables(variables, macro) except ModuleNotFoundError: print("No module found.")