本文整理匯總了Python中warnings.formatwarning方法的典型用法代碼示例。如果您正苦於以下問題:Python warnings.formatwarning方法的具體用法?Python warnings.formatwarning怎麽用?Python warnings.formatwarning使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類warnings
的用法示例。
在下文中一共展示了warnings.formatwarning方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _showwarning
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import formatwarning [as 別名]
def _showwarning(message, category, filename, lineno, file=None, line=None):
"""
Implementation of showwarnings which redirects to logging, which will first
check to see if the file parameter is None. If a file is specified, it will
delegate to the original warnings implementation of showwarning. Otherwise,
it will call warnings.formatwarning and will log the resulting string to a
warnings logger named "py.warnings" with level logging.WARNING.
"""
if file is not None:
if _warnings_showwarning is not None:
_warnings_showwarning(message, category, filename, lineno, file, line)
else:
s = warnings.formatwarning(message, category, filename, lineno, line)
logger = getLogger("py.warnings")
if not logger.handlers:
logger.addHandler(NullHandler())
logger.warning("%s", s)
示例2: capture
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import formatwarning [as 別名]
def capture(capture_warnings=True, fail=False):
"""
Log exceptions and warnings.
"""
default_warning_format = warnings.formatwarning
try:
if capture_warnings:
warnings.formatwarning = custom_warning_format
logging.captureWarnings(True)
try:
yield
except Exception as e:
logging.exception('caught unhandled excetion')
if fail:
if not isinstance(e, Warning):
raise RuntimeError('application failure')
finally:
if capture_warnings:
warnings.formatwarning = default_warning_format
logging.captureWarnings(False)
示例3: play_note
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import formatwarning [as 別名]
def play_note(self,note_name,note_duration):
"""Plays a single note by creating a 1 note song in song 0
"""
current_song = 0
play_list=[]
noError = True
if noError:
#Need to map ascii to numbers from the dict.
if note_name in self.config.data['midi table']:
play_list.append(self.config.data['midi table'][note_name])
play_list.append(note_duration)
else:
# That note doesn't exist. Plays nothing
# Raise an error so the software knows that the input was bad
play_list.append(self.config.data['midi table'][0])
warnings.formatwarning = custom_format_warning
warnings.warn("Warning: Note '" + note_name + "' was not found in midi table")
#create a song from play_list and play it
self.create_song(current_song,play_list)
self.play(current_song)
示例4: execute
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import formatwarning [as 別名]
def execute(self):
def showwarning(message, category, filename, fileno, file=None, line=None):
msg = warnings.formatwarning(message, category, filename, fileno, line)
self.log.warning(msg.strip())
with warnings.catch_warnings():
warnings.simplefilter("always")
warnings.showwarning = showwarning
try:
return self.run()
except SystemExit:
raise
except:
self.log.critical(traceback.format_exc().strip())
sys.exit(1)
示例5: log_warning
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import formatwarning [as 別名]
def log_warning(msg, typ, script, lineno, file=None, line=None):
if option_set('general', 'debug'):
print('Logging warning "%s"' % str(msg))
warning = {
'type': typ.__name__,
'message': str(msg),
'script': script,
'lineno': lineno
}
# Update object in DB
db = open_or_create_db()
db.update(append("warnings", warning, no_duplicates=True), eids=[RUN_ID])
db.close()
# Done logging, print warning to stderr
sys.stderr.write(warnings.formatwarning(msg, typ, script, lineno, line=line))
示例6: _showwarning
# 需要導入模塊: import warnings [as 別名]
# 或者: from warnings import formatwarning [as 別名]
def _showwarning(message, category, filename, lineno, file=None, line=None):
"""
Implementation of showwarnings which redirects to logging, which will first
check to see if the file parameter is None. If a file is specified, it will
delegate to the original warnings implementation of showwarning. Otherwise,
it will call warnings.formatwarning and will log the resulting string to a
warnings logger named "py.warnings" with level logging.WARNING.
"""
if file is not None:
if _warnings_showwarning is not None:
_warnings_showwarning(message, category, filename, lineno, file, line)
else:
s = warnings.formatwarning(message, category, filename, lineno, line)
logger = getLogger("py.warnings")
if not logger.handlers:
logger.addHandler(NullHandler())
logger.warning(s)