Easy context prefixes for messages.
Project description
Dynamic message prefixes providing execution context.
Latest release 20250613: Pfx.prefixify: no longer prefixify internal newlines, makes the message unreadable.
The primary facility here is Pfx,
a context manager which maintains a per thread stack of context prefixes.
There are also decorators for functions.
This stack is used to prefix logging messages and exception text with context.
Usage is like this:
from cs.pfx import Pfx
...
def parser(filename):
with Pfx(filename):
with open(filename) as f:
for lineno, line in enumerate(f, 1):
with Pfx(lineno):
if line_is_invalid(line):
raise ValueError("problem!")
info("line = %r", line)
This produces log messages like:
datafile: 1: line = 'foo\n'
and exception messages like:
datafile: 17: problem!
which lets one put just the relevant complaint in exception and log messages and get useful calling context on the output. This does make for wordier logs and exceptions but used with a little discretion produces far more debuggable results.
The main items supplied by this module are:
Pfx: a context manager for wrapping code in an addition prefix context@pfx: a decorator for a function to wrap calls to the function@pfx_method: like@pfxbut for instance methodspfx_call(func,*a,**kw): callfunc(*a,**kw)inside a prefix contextpfxprint(): callprint()with a leading prefix
Short summary:
Pfx: A context manager to maintain a per-thread stack of message prefixes.pfx: General purpose @pfx for generators, methods etc.pfx_call: Callfunc(*a,**kw)within an enclosingPfxcontext manager reciting the function name and arguments.pfx_iter: Wrapper for iterables to prefix exceptions withtag.pfx_method: Decorator to provide aPfxcontext for an instance method prefixing classname.methodname.PfxCallInfo: Subclass of Pfx to insert current function and caller into messages.pfxprint: Callprint()with the current prefix.PfxThread: Factory function returning aThreadwhich presents the current prefix as context.prefix: Return the current Pfx prefix.PrePfx: Push a temporary value for Pfx._state._ur_prefix to enloundenify messages.unpfx: Strip the leading prefix from the stringsusing the prefix delimitersep(default fromDEFAULT_SEPARATOR:': ').XP: Variation oncs.x.Xwhich prefixes the message with the current Pfx prefix.XX: Trite wrapper forXP()to transiently insert a leading prefix string.
Module contents:
Pfx.__init__(self, mark, *args, **kwargs):
Initialise a new Pfx instance.
Parameters:
mark: message prefix stringargs: if not empty, apply to the prefix string with%absolute: optional keyword argument, defaultFalse. If true, this message forms the base of the message prefixes; earlier prefixes will be suppressed.loggers: which loggers should receive log messages.print: if true, print themarkon entry to thewithsuite. This may be abool, implyingprint()ifTrue, a callable which works likeprint(), or a file-like object which implies usingprint(...,file=print).
Note:
the mark and args are only combined if the Pfx instance gets used,
for example for logging or to annotate an exception.
Otherwise, they are not combined.
Therefore the values interpolated are as they are when the Pfx is used,
not necessarily as they were when the Pfx was created.
If the args are subject to change and you require the original values,
apply them to mark immediately, for example:
with Pfx('message %s ...' % (arg1, arg2, ...)):
This is a bit more expensive as it incurs the formatting cost
whenever you enter the with clause.
The common usage is:
with Pfx('message %s ...', arg1, arg2, ...):
Pfx.critical(self, msg, *args, **kwargs):
Emit a critical log message.
Pfx.debug(self, msg, *args, **kwargs):
Emit a debug log message.
Pfx.error(self, msg, *args, **kwargs):
Emit an error log message.
Pfx.exception(self, msg, *args, **kwargs):
Log an exception message to this Pfx's loggers.
Pfx.info(self, msg, *args, **kwargs):
Emit an info log message.
Pfx.log(self, level, msg, *args, **kwargs):
Log a message at an arbitrary log level to this Pfx's loggers.
Pfx.loggers:
Return the loggers to use for this Pfx instance.
Pfx.logto(self, new_loggers):
Define the Loggers anew.
Pfx.partial(self, func, *a, **kw):
Return a function that will run the supplied function func
within a surrounding Pfx context with the current mark string.
This is intended for deferred call facilities like
Later and futures.
Pfx.prefixify(text):
Return text with the current prefix prepended.
Return text unchanged if it is not a string.
Pfx.prefixify_exception(e):
Modify the supplied exception e with the current prefix.
The original value of some .attr is preserved as .{attr}_without_prefix.
Return True if modified, False if unable to modify.
Pfx.push(msg, *a):
A new Pfx(msg,*a) onto the Thread stack.
Pfx.scope(msg=None, *a):
Context manager to save the current Thread's stack state
and to restore it on exit.
This is to aid long suites which progressively add Pfx context
as the suite progresses, example:
for item in items:
with Pfx.scope("item %s", item):
db_row = db.get(item)
Pfx.push("db_row = %r", db_row)
matches = db.lookup(db_row.category)
if not matches:
continue
Pfx.push("%d matches", len(matches):
... etc etc ...
Pfx.umark:
Return the unicode message mark for use with this Pfx.
This is used by Pfx._state.prefix to compute the full prefix.
Pfx.warning(self, msg, *args, **kwargs):
Emit a warning log message.
-
pfx(*da, **dkw): General purpose @pfx for generators, methods etc.Parameters:
func: the function or generator function to decoratemessage: optional prefix to use instead of the function namemessage_args: optional arguments to embed in the preifx using%
Example usage:
@pfx def f(....): .... -
pfx_call(func, *a, **kw): Callfunc(*a,**kw)within an enclosingPfxcontext manager reciting the function name and arguments.Example:
>>> import os >>> pfx_call(os.rename, "oldname", "newname") -
pfx_iter(tag, iterable): Wrapper for iterables to prefix exceptions withtag. -
pfx_method(*da, **dkw): Decorator to provide aPfxcontext for an instance method prefixing classname.methodname.If
use_stris true (defaultFalse) usestr(self)instead ofclassname.If
with_argsis true (defaultFalse) include the specified arguments in thePfxcontext. Ifwith_argsisTrue, this includes all the arguments. Otherwisewith_argsshould be a sequence of argument references: anintspecifies one of the positional arguments and a string specifies one of the keyword arguments.Examples:
class O: # just use "O.foo" @pfx_method def foo(self, .....): .... # use the value of self instead of the class name @pfx_method(use_str=True) def foo2(self, .....): .... # include all the arguments @pfx_method(with_args=True) def foo3(self, a, b, c, *, x=1, y): .... # include the "b", "c" and "x" arguments @pfx_method(with_args=[1,2,'x']) def foo3(self, a, b, c, *, x=1, y): .... -
class PfxCallInfo(Pfx): Subclass of Pfx to insert current function and caller into messages. -
pfxprint(*a, print_func=None, **kw): Callprint()with the current prefix.The optional keyword parameter
print_funcprovides an alternative function to the builtinprint(). -
PfxThread(target, **kw): Factory function returning aThreadwhich presents the current prefix as context. -
PrePfx(tag, *args): Push a temporary value for Pfx._state._ur_prefix to enloundenify messages. -
unpfx(s, sep=None): Strip the leading prefix from the stringsusing the prefix delimitersep(default fromDEFAULT_SEPARATOR:': ').This is a simple hack to support reporting error messages which have had a prefix applied, and fails accordingly if the base message itself contains the separator.
-
XP(msg, *args, **kwargs): Variation oncs.x.Xwhich prefixes the message with the current Pfx prefix. -
XX(prepfx, msg, *args, **kwargs): Trite wrapper forXP()to transiently insert a leading prefix string.Example:
XX("NOTE!", "some message")
Release Log
Release 20250613: Pfx.prefixify: no longer prefixify internal newlines, makes the message unreadable.
Release 20250308: Pfx.prefixify_exception: preserve the original str(e) as e._
Release 20241208: Pfx.prefixify_exception: record the original message as {attr}_without_prefix, useful in warnings which themselves recite the prefix.
Release 20240630: @pfx_method: fix access to class name when the target is a class.
Release 20240412: Pfx.prefixify_exception: bugfix prefixification of sequences.
Release 20240326:
- Pfx.prefixify_exception: sanity check the types of the prefixed attributes, aids debugging.
- Pfx.prefixify_exception: do not modify .message if it is not a string.
Release 20230604: @pfx_method: handle methods with no name (generally a misuse of the decorator).
Release 20230331: PfxThread: use HasThreadState.Thread, make target mandatory.
Release 20221118: pkg_tags: cs.py.func: update PyPI release: set pypi.release='20221118' [IGNORE]
Release 20220918:
- Drop _PfxThreadState.raise_needs_prefix, supplant with more reliable special exception attribute.
- Pfx.exit: include more detail in (suppressed) "message not prefixed" message.
- Pfx.exit: more elaborate logic for exc_value.args.
Release 20220523: Pfx.umask: promote self.mark directly to ustr.
Release 20220429:
- New Pfx.scope() context manager and Pfx.push(msg,*a) nonindenting Pfx push.
- pfxprint: new optional print_func keyword parameter.
Release 20220227:
- Pfx.prefixify: change OSError.args action: prefixify the first string.
- XP: use DEFAULT_SEPARATOR on both paths.
Release 20211031: Pfx.prefixify_exception: skip attributes which are None.
Release 20210913: Pfx: do not fiddle with LookupError.args[0], it is the key.
Release 20210906:
- New pfxprint which calls print() with the current prefix.
- Pfx.prefixify_exception: catch unexpected OSError.args value and report.
- @pfx: use pfx_call if there is no presupplied message argument.
Release 20210801: Bugfix for @pfx.
Release 20210731:
- Pfx.exit: special handling for some exception types.
- New pfx_call(func,*a,**kw) function to concisely wrap single function calls.
Release 20210717: @pfx_method: new optional decorator argument "with_args" to include some or all arguments in the Pfx context.
Release 20201227:
- Pfx: new print=False option to issue a print() or other call on entry to the with-suite eg with Pfx(....,print=verbose).
- Pfx: print= now also accepts a file-like object.
Release 20201105: @pfx: bugfix for generator functions.
Release 20201025:
- Refactor @pfx using @cs.deco.contextdecorator.
- New unpfx() function, a crude hack to strip an applpied cs.pfx prefix from a string.
- XP: just shim cs.x.X as callers expect, toss dubious extra functionality.
- exception(): plumb keyword arguments.
Release 20200517:
- @pfx: handle normal functions and also generators, improve behaviour with the wrapped docstring.
- @pfx_method: @pfx for methods.
- @pfxtag obsoleted by new @pfx.
Release 20191004:
@pfx_method: new optional use_str parameter to use str(self) instead of type(self).name; now requires @cs.deco.decorator
Release 20190905:
- Pfx.exit: simplify prefixify_exc() logic, prefixify all suitable attributes.
- New @pfx_method decorator for instance methods.
Release 20190607: Pfx.exit improved exception attribute handling.
Release 20190403: Debugging aid: Pfx.umark: emit stack traceback on format conversion error.
Release 20190327:
- @pfx: set name on the wrapper function.
- Bugfix some references to the internal prefixify function.
Release 20190324: Pfx.exit: apply the prefix to all the standard attributes where present, improves some message behaviour for some exception types.
Release 20181231: Bugfix for an infinite regress.
Release 20181109:
- Update @contextmanager formalism to use try/finally for the cleanup phase.
- New decorator @gen to manage Pfx state across generator iterations; pretty clunky.
- Better fallback handling.
- Some docstring updates.
Release 20170910: Slight linting.
Release 20170903.1: corrections to the module docstring
Release 20170903: Initial release for PyPI.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cs_pfx-20250613.tar.gz.
File metadata
- Download URL: cs_pfx-20250613.tar.gz
- Upload date:
- Size: 12.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59c55a90783547a97c939f4a6636d21717684da7f3bfbe881eaf1833672cd7c7
|
|
| MD5 |
37920ecc4a2acd484a9283e0594c2c48
|
|
| BLAKE2b-256 |
28492194d2b1d772ffa766207ea13e63bd8c6968b8ee1688deed5ece904788c2
|
File details
Details for the file cs_pfx-20250613-py2.py3-none-any.whl.
File metadata
- Download URL: cs_pfx-20250613-py2.py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06de4f05699dcff8754c0daabbd8aa227f263cf5fa654ebff541856b3b3e690f
|
|
| MD5 |
e0d8df3832d806d12faaa64188d16cab
|
|
| BLAKE2b-256 |
ff05c711cea897d8c92ef9b7aff0d20e8e0c1899729c4dcd1ec8fe9dbd60bb14
|