X(), for low level debugging.
Project description
X(), for low level debugging.
Latest release 20240630: X(msg,*args): handle mismatched message and format arguments.
X() is my function for low level ad hoc debug messages.
It takes a message and optional format arguments for use with %.
It is presented here in its own module for reuse:
from cs.x import X
...
X("foo: x=%s, a=%r", x, a)
It normally writes directly to sys.stderr but accepts an optional
keyword argument file to specify a different filelike object.
The following globals further tune its behaviour,
absent the file= parameter:
X_default_colour: if set, messages will be ANSI coloured usingcs.ansi_colour.colouriseX_discard: if true then discard the message. Otherwise write the message tosys.stderr.X_discard's default value isnot sys.stderr.isatty().X_logger: if notNonethen log a warning to that logger.X_via_tty: if true then a pathname to which to append messages.
The following environment variables affect the initial values of the globals:
$CS_X_COLOUR: this setsX_default_colour.$CS_X_LOGGER: if present, an empty value setsX_loggerto the root logger and a nonempty value names a logger.$CS_X_VIA_TTY: if missing or empty,X_via_ttywill be false. Otherwise, if$CS_X_VIA_TTYhas a nonempty value which is a full path to an existing filesystem object (typically a tty) then is will be used forX_via_tty, otherwiseX_via_ttywill be set to'/dev/tty'. This is handy for getting debugging out of test suites, which often divertsys.stderr.
Function X(msg, *args, **kw)
Unconditionally write the message msg.
If there are positional arguments after msg,
format msg using %-expansion with those arguments.
Keyword arguments:
file: optional keyword argument specifying the output file.colour: optional text colour. If specified, surround the message with ANSI escape sequences to render the text in that colour.
If file is not None, write to it unconditionally.
Otherwise, the following globals are consulted in order:
X_logger: if notNonethen log a warning to that loggerX_via_tty: if true then append the message to the path it containsX_discard: if true then discard the message Otherwise write the message tosys.stderr.
X_logger is None by default.
X_via_tty is initialised from the environment variable $CS_X_VIA_TTY.
X_discard is true unless sys.stderr.isatty() is true.
Function Y(msg, *a, **kw)
Wrapper for X() rendering in yellow.
Release Log
Release 20240630: X(msg,*args): handle mismatched message and format arguments.
Release 20240316: Fixed release upload artifacts.
Release 20240201: Doc updates.
Release 20231129: New $CS_X_LOGGER environment variable to direct messages to a logger.
Release 20230331: Move the open-tty-for-append hacks into cs.gimmicks.open_append, fix unclosed file handle.
Release 20230218: Accomodate idiotic Linux /dev/tty open semantics.
Release 20221118: Tweak for open of /dev/tty, still not properly resolved.
Release 20220918:
- Drop Xtty(), obsolete.
- X(): special handling for unseeking output files - I've had a Linux system moan about opening /dev/tty for append.
Release 20211208: X_via_tty: now accept full path to a tty in $CS_X_VIA_TTY to aid messaging to other terminals.
Release 20210123: X: honour new $CS_X_COLOUR environment variable setting the default colour, default uncoloured.
Release 20201227:
New Y() which calls X(...,colour=yellow) - I now often go from cs.x import Y as X.
Release 20201102:
- Set X_via_tty if $CS_X_VIA_TTY.
- Put X() into builtins if $CS_X_BUILTIN.
Release 20181231:
- X: trivial ANSI colour support via new
colourkeyword argument. - New global X_discard, False unless sys.stderr.isatty.
Release 20180726: doco improvements
Release 20170902: Move X() into its own module, used for ad hoc debugging everywhere.
Release 20170707.3: tweak DISTINFO
Release 20170707.2: Doc tweak.
Release 20170707.1: Added README.
Release 20170707: Separate X() out into new module cs.x for cheap import.
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.x-20240630.tar.gz.
File metadata
- Download URL: cs.x-20240630.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e16631e78be4f411499287ac23cf09784b8e1f8291911e228bcf094d9dbd906d
|
|
| MD5 |
dea6f6752f3d9db55e93e94c6bc342ab
|
|
| BLAKE2b-256 |
a0b752dd257f63b8de8ceb844f3147071df3c2693b291bdba68ab581309c77b9
|
File details
Details for the file cs.x-20240630-py3-none-any.whl.
File metadata
- Download URL: cs.x-20240630-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a29702f22d694c49ad523776a5676ef57b8f22620bc274d87d59c6df67f0641
|
|
| MD5 |
85fdbe63c5d81f46c031ea105ab5907a
|
|
| BLAKE2b-256 |
c0166269362c289b85fbb5f0dcbcf810d8aa6a98cd64aa46b5fc3e91101843da
|