James Gardner: Home > Work > Code > BareNecessities > 0.2.3 > API Documentation

BareNecessitites v0.2.3 documentation

API Documentation

BareNeccessities contains functions and classes I use so frequently I wish they were in the standard library (in fact one of them is as of Python 2.6!).

These all work with Python 2.4 and above and may work with earlier versions too.

class bn.AttributeDict
class bn.HTMLFragment

Build up a correctly-escaped HTML string.

>>> fragment=HTMLFragment()
>>> fragment.safe(u'<p>')
>>> fragment.write(u'Hello, my name is <b>James</b>.')
>>> fragment.safe(u'</p>')
>>> print fragment.output
<p>Hello, my name is &lt;b&gt;James &lt;/b&gt;.

Notice that the unsafe characters passed to write() were escaped whereas those passed to safe() were not.

The idea is that you can use HTMLFragment to very efficiently generate fragements of code to be inserted into templates and in doing so avoid a dependency on a full templating language or its associated overheads whilst still generating safe HTML.

exception bn.RelativeImportError
bn.abs_mod_path(current_abs_module_path, up, new_rel_mod_path)
Calculate the absolute module path of the new import from the absolute module path of the current module the number of steps up to where the relatvie path begins and the relative module import path from that directory.
bn.absimport(mod_path, from_=None)
Perform an absolute import of mod_path and return the last module
bn.adjust_path_for_imp(new_abs_mod_path, package_directory)
Adjust the path so that imp doesn’t need to deal with sub-modules
bn.dir_containing_package_root(path_to_package, current_module_filename)
Returns the directory path containing the package’s top __init__.py file based on the value provided for the path to the package and the path and filename of the module the import is occurring from.
bn.handle_from_clause(module, from_)
Takes care of parsing the from_ string and finding the objects in module. If one object is imported it is returned, otherwise a tuple of the requested objects is returned.

Calculates how many directories up the relative import starts and returns:

  • the number of directories up as an integer
  • the module path from the directory where the import starts
bn.module_path_of_current_module(current_module_filename, package_directory, up)
Calculate the absoulte module path of the current module and adjust the value of the up variable if the module filename ends with __init__.py as the module path should be one lower than other files in the same directory.
bn.relimport(module_path, current_module_filename, path_to_package, from_=None)
bn.relpath(path, start='.')
Return a relative version of a path
James Gardner: Home > Work > Code > BareNecessities > 0.2.3 > API Documentation