Skip to main content

X(), for low level debugging.

Project description

X(), for low level debugging.

Latest release 20230218: Accomodate idiotic Linux /dev/tty open semantics.

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_logger: if not None then log a warning to that logger
  • X_via_tty: if true then a pathname to which to append messages
  • X_discard: if true then discard the message Otherwise write the message to sys.stderr.

If the environment variable $CS_X_VIA_TTY is empty, X_via_tty will be false. Otherwise, if $CS_X_VIA_TTY has a nonempty value which is a full path to an existing filesystem object (typically a tty) then is will be used for X_via_tty, otherwise X_via_tty will be set to '/dev/tty'. This is handy for getting debugging out of test suites, which often divert sys.stderr.

X_discard's default value is not sys.stderr.isatty().

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 not None then log a warning to that logger
  • X_via_tty: if true then append the message to the path it contains
  • X_discard: if true then discard the message Otherwise write the message to sys.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 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 colour keyword 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cs.x-20230218.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cs.x-20230218-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file cs.x-20230218.tar.gz.

File metadata

  • Download URL: cs.x-20230218.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for cs.x-20230218.tar.gz
Algorithm Hash digest
SHA256 6bc9f06d9a96eb1a644f6c3470953c89fd0f8bd7d13ceb9b2d29b2d3bb668163
MD5 f781adf701d3b29423162ae383891d40
BLAKE2b-256 1641e2364964fff820ae286a404aa2263089632641f77faae99e0819f9c0801d

See more details on using hashes here.

File details

Details for the file cs.x-20230218-py3-none-any.whl.

File metadata

  • Download URL: cs.x-20230218-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for cs.x-20230218-py3-none-any.whl
Algorithm Hash digest
SHA256 a70f05210496417266bc4f65a0e1bc232dbdba64b81b1a1009b2885652710d9a
MD5 20c9ea18171342675fa5c2d28d26648d
BLAKE2b-256 5550fe6cd7e1829eb4a60e597fbf4e63deb97d5230dd1f1dc94f86da34ab1a11

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page