Python 2: function alias or macro -
i wondering if there way create macros or aliases functions in python 2.7.
example: trying use logging module , create aliases/macros functions logging.debug, logging.info, logging.error, etc. if use functions in place want log, works fine. if try create 'alias' function wrapper this:
def debuglog(message): logging.debug(message) ... line number reporting no longer works intended, line reported states location of wrapper , not actual log, isn't real use.
i did find solution:
import logging logging import info infolog logging import debug debuglog logging import error errorlog .... ... not suitable me since create own logging severity:
logging.addlevelname(60, "normal") ... , i'd create alias/macro normallog(message)=logging.log(60, message) if it's possible? couldn't find comprehensive in python docs or online.
you can use functools.partial:
import functools import logging normallog = functools.partial(logging.log, 60) it works pretty well:
normallog("hey!!") level 60:root:hey!! partial binds arguments function calls , return partial object (a callable object holds necesary information), can use in addlevelname method:
activatelevel = functools.partial(logging.addlevelname, 60, "normal") activatelevel() here have live working example, notice log line reported.
Comments
Post a Comment