Package jazzparser :: Package utils :: Module base
[hide private]
[frames] | no frames]

Module base

source code

Miscellaneous utility functions.

A load of common utilities used in various places in the codebase and not specific to any formalism, tagger, etc.


Author: Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>

Classes [hide private]
  ExecutionTimer
Very simple class to wrap up a common method for measuring the execution time of some code.
  OptionalImportError
Functions [hide private]
 
check_directory(filename, is_dir=False)
Given path to a file or directory, checks that the directory (or that containing the file) exists and creates it if not.
source code
 
group_pairs(inlist, none_final=False, none_initial=False) source code
 
exception_tuple(str_tb=False)
Returns a tuple containing information about the currently raised exception.
source code
 
load_class(full_name)
Given the full python path to a class, imports the class dynamically and returns it.
source code
 
load_optional_package(full_name, dependency_name=None, task=None)
Given the full python path to a package, imports it if possible.
source code
 
load_from_optional_package(package_name, object_name, dependency_name=None, task=None)
Works in the same way as load_optional_package, but corresponds to doing a from X import Y.
source code
 
abstractmethod(fn, cls=None)
Decorator to make a method abstract.
source code
Variables [hide private]
  __package__ = 'jazzparser.utils'
Function Details [hide private]

check_directory(filename, is_dir=False)

source code 

Given path to a file or directory, checks that the directory (or that containing the file) exists and creates it if not. This is useful to put in before you try creating or outputing to a file.

Parameters:
  • is_dir (bool) - in general, we assume the input name is a file and we want to check its directory. Set is_dir=True if the input is in fact a directory
Returns:
True if the directory was created, False if it already existed.

exception_tuple(str_tb=False)

source code 

Returns a tuple containing information about the currently raised exception. This is (type,value,traceback).

Parameters:
  • str_tb (bool) - format the traceback as a string instead of returning it as an object. You'll need to do this if you want to pickle the result.

load_optional_package(full_name, dependency_name=None, task=None)

source code 

Given the full python path to a package, imports it if possible. If the import fails, raises an OptionalImportError. This is designed for loading packages that come from optional dependencies, so that a sensible error message can uniformly be displayed.

Only works with absolute paths, because relative paths would be relative to this function's module. Relative paths are explicitly disabled.

Parameters:
  • dependency_name (string) - a readable name for the package being loaded.
  • task (string) - a string identifying the task you're loading it for. This is for readable error output.

load_from_optional_package(package_name, object_name, dependency_name=None, task=None)

source code 

Works in the same way as load_optional_package, but corresponds to doing a from X import Y. Note that X and Y do not correspond directly to package_name and object_name, though. Part of X may be found in object_name: this is so that the import of the optional package can be tested before we try loading anything from it, which may generate unrelated import errors, even if the optional package is installed correctly.

E.g. instead of doing:

from nltk.model.ngram import NgramModel

we would do:

load_from_optional_package('nltk', 'model.ngram.NgramModel')

so that we can distinguish between errors loading 'nltk' due to its not being installed and errors importing 'nltk.model.ngram.NgramModel' due to bugs in the module, incorrect class name, etc.

Parameters:
  • package_name - Python path to the optional package root
  • object_name - remainder of the Python path to the object to be returned.

abstractmethod(fn, cls=None)

source code 

Decorator to make a method abstract. Just raises a NotImplementedError when the method is called.