latest package

latest is a LaTeX-oriented template engine.

Submodules

latest.config module

config module contains configuration functions and classes for latest package.

latest is completely customizable in its syntax both programmatically and through a configuration file.

Configurations are contained in an object of the class _Config that take an optional keyword argument in his __init__ method to specify the location of the configuration file. If no configuration file is specified, defaults are set in code. The default configuration object config is defined in this module and can be imported to be used elsewhere. I suggest an import statement like

from latest.config import config as Config

Then, you can think of the configuration object as a class with many static methods. To create an alternate configuration object you can use the public function create_config(config_file=None) instead of directly instantiate an object of private class _Config.

Configuration file is found by default (default configuration object look for this location) in ~/.latest/latest.cfg but one can use his own configuration file. Configuration files must be in ini format. Useful sections are:

  • general
  • lang

The section lang of the configuration file is where one can define its own syntax. Available options in lang section are:

  • cmd_entry
  • cmd_exit
  • env_entry
  • env_exit
  • ns_operator
latest.config.create_config(config_file=None)

latest.core module

core contains core functions for templating.

latest.core.parse_options(options, config=<latest.config._Config object>)

Parses a string of options.

Parameters:options (str) – the string that defines options.
Returns:the dictionary with options as key-value pairs.
Return type:dict
latest.core.eval_code(code, context, config=<latest.config._Config object>)

Parses and evaluates code converting output to a string.

Parameters:
  • code (str) – the code to be evaluated.
  • context (dict) – the context to be evaluated in.
  • config (config._Config) – configuration object.
Returns:

the output converted to a string.

Return type:

str

Raises:
  • CodeError – raised if a SyntaxError is raised by the builtin eval() function.
  • ContextError – raised when the context names do not match code names and a NameError is raised by the builtin eval() function.
latest.core.eval_ns(path, context, config=<latest.config._Config object>)

Translates a path to a list of context dictionaries, a.k.a namespace.

A namespace is a branch of a main context dictionary.

The following rules apply:

  • if the path specified points to a scalar it is first converted to a dict with the ‘_value’ key set to the scalar and a one-element list filled by the dict is returned
  • if the path specified points to a vector the vector is returned by the elements are treated according to these rules
  • if the path specified points to a tensor (dict or object) a one-element list filled with the tensor is returned
Parameters:
  • path (str) – the path for the namespace.
  • context (dict) – the main context object.
  • config (config._Config) – configuration object.
Returns:

the namespace, a list of context dictionaries.

Return type:

list

latest.core.eval_cmd(code, context, config=<latest.config._Config object>, ns=None, join_items='')

Evaluate a latest command.

A command is a latest directive to execute code within a namespace and output a string. The command directive specify a code island. If the namespace is a list of context dictionary the code island is evaluated against every context and the results are joined (concatenated) with a special string, which by default is specified as the join_items attribute of the configuration object.

Parameters:
  • code (str) – the code to be executed.
  • context (dict) – the global context.
  • config (config._Config) – configuration object.
  • ns (str) – the namespace to be executed in.
  • join_items (str) – the string used to join the results from the contexts in the namespace.
Returns:

the output of the code executed within the namespace and converted to string. If the namespace target a list of contexts the code is evaluated for every context and the results are concatenated by the string defined in the join_items attribute of the configuration object or in the keyword argument join_items.

Return type:

str

latest.core.eval_expr(expression, context, config=<latest.config._Config object>)

Evaluate a latest expression.

An expression is a string of plain text with eventual code islands (latest commands) in between. The evaluation proceeds evaluating code islands and then concatenating the results with the fragments of plain text.

Parameters:
  • expression (str) – the expression to be evaluated.
  • context (dict) – the context to be evaluated in.
  • config (config._Config) – configuration object.
Returns:

the output obtained concatenating the plain text fragments with the output of code islands evaluation process.

Return type:

str

latest.core.eval_env(content, namespace, context, config=<latest.config._Config object>, join_items='')

Evaluate a latest environment.

An environment is a section of a template to be executed within a namespace.

Parameters:
  • content (str) – the content of the environment to be evaluated.
  • namespace (str) – the namespace within the global context to be evaluated in.
  • context (dict) – the global context.
  • config (config._Config) – configuration object.
  • join_items (str) – the string used to join the results from the evaluation process over the expression inside the environment against the contexts within the namespace.
Returns:

the output obtained evaluating the expression within the namespace. If the namespace targets a list of context dictionaries, the expression is evaluated against every context and the results are joined with a special string which by default is specified in the join_items attribute of the configuration object.

Return type:

str

latest.core.eval_latest(code, context, config=<latest.config._Config object>)

Evaluates an entire latest code/template.

Parameters:
  • code (str) – the latest formatted template code.
  • context (dict) – the global context.
  • config (config._Config) – configuration object.
Returns:

the evaluated document.

Return type:

str

latest.exceptions module

exceptions module contains exceptions classes defined for latest package.

exception latest.exceptions.LatestError

Bases: Exception

Base class for all latest exceptions.

exception latest.exceptions.CodeError

Bases: latest.exceptions.LatestError

Exception raised when bad syntax code is parsed in a template.

exception latest.exceptions.ContextError

Bases: latest.exceptions.LatestError

Exception raised when context dictionary doesn’t match names required by a template.

latest.shortcuts module

shortcuts module contains shortcut functions built upon core functionality of latest package.

latest.shortcuts.render(template_filename, data_filename, config=<latest.config._Config object>, data_fmt=None)

Render a template in a file within a context defined by a json or yaml formatted data file.

Parameters:
  • template_filename (str) – the path of the template file.
  • data_filename (str) – the path of the data .yaml file.
  • data_fmt (str) – format of data file; accepted: json, yaml (or yml).
  • config (config._Config) – configuration object.
Returns:

the output of the evaluation process as defined by latest core functions.

Return type:

str

latest.util module

util module contains utility functions for latest package.

latest.util.is_scalar(obj)
latest.util.is_vector(obj)
latest.util.is_tensor(obj)
latest.util.path(location)
latest.util.select(path, data, sep='/')
latest.util.getopt(parser, section, key, default)
latest.util.get_supported_data_fmts()
latest.util.guess_data_fmt(filename, default)
latest.util.load_json(filename)
latest.util.load_yaml(filename)
latest.util.load_data(filename, data_fmt, default_data_fmt)
latest.util.split(string, pattern)