view pylearn/shared/layers/util.py @ 860:bf2f71084d59

removed useless print
author James Bergstra <bergstrj@iro.umontreal.ca>
date Tue, 03 Nov 2009 15:24:27 -0500
parents 580087712f69
children 93b8373c6735
line wrap: on
line source

"""A few little internal functions"""
import logging

def update_locals(obj, dct):
    if 'self' in dct:
        del dct['self']
    obj.__dict__.update(dct)

def LogFn(f):
    def rval(*args):
        f(' '.join(str(a) for a in args))
    return staticmethod(rval)

def add_logging(cls, name=None, level=None):
    """ Add logging functions to a class: self._debug, self._info, self._warn, self._warning,
    self._error.

    All of these functions has the same signature.  They accept a variable number of positional
    arguments, and print them all casted to string (and concatenated with a ' '.)

    :type name: str
    :param name: the name of the logger.

    :type level: None, str, type(logging.INFO)
    :param level: a logging level (e.g. logging.INFO), or the name of a logging level (e.g
    'INFO').  If level is None, then this function doesn't set the logging level.

    """
    if name is None:
        name = "layers.%s" % cls.__name__
    cls._logger = logging.getLogger(name)
    if level:
        try:
            level = getattr(logging, level)
        except:
            pass
        cls._logger.setLevel(level)

    cls._debug = LogFn(cls._logger.debug)
    cls._info = LogFn(cls._logger.info)
    cls._warn = cls._warning = LogFn(cls._logger.warn)
    cls._error = LogFn(cls._logger.error)
    cls._critical = LogFn(cls._logger.critical)
    cls._fatal = LogFn(cls._logger.fatal)