inform


Nameinform JSON
Version 1.33 PyPI version JSON
download
home_pageNone
Summaryprint & logging utilities for communicating with user
upload_time2024-12-12 05:14:06
maintainerNone
docs_urlNone
authorKen Kundert
requires_python>=3.6
licenseNone
keywords inform logging printing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage
            Inform — Print & Logging Utilities
==================================

|downloads| |build status| |coverage| |rtd status| |pypi version| |anaconda version| |python version|

:Author: Ken Kundert
:Version: 1.33
:Released: 2024-12-11

A package that provides specialized print functions that are used when 
communicating with the user. It allows you to easily print attractive, 
informative, and consistent error messages.  For example:

.. code-block:: python

    >> from inform import display, warn, error
    >> display(
    ..     'Display is like print'
    ..     'except that it supports logging and can be disabled.'
    ..     sep=', ')
    Display is like print, except that it supports logging and can be disabled.

    >> warn('warnings get a header that is printed in yellow.')
    warning: warnings get a header that is printed in yellow.

    >> error('errors get a header that is printed in red.')
    error: errors get a header that is printed in red.

Inform also provides logging and output control.

In addition, Inform provides a powerful generic exception that can be used 
directly as a general purpose exception, or can be subclassed to produce 
powerful specialized exceptions.  Inform exceptions are unique in that they keep 
all of the named and unnamed arguments so they can be used when reporting 
errors.

You can find the documentation on `ReadTheDocs
<https://inform.readthedocs.io>`_. You can download and install the latest
stable version of the code from `PyPI <https://pypi.python.org>`_ using::

    pip3 install inform

You can find the latest development version of the source code on
`Github <https://github.com/KenKundert/inform>`_.


Introduction
------------

This package defines a collection of *print* functions that have different 
roles.  These functions are referred to as *informants* and are described below 
in the Informants section. They include include *log*, *comment*, *codicil*, 
*narrate*, *display*, *output*, *notify*, *debug*, *warn*, *error*, *fatal* and 
*panic*.

With the simplest use of the program, you simply import the informants you need 
and call them (they take the same arguments as Python's built-in *print* 
function):

.. code-block:: python

    >>> from inform import display
    >>> display('ice', 9)
    ice 9

For more control of the informants, you can import and instantiate the Inform 
class yourself along with the desired informants.  This gives you the ability to 
specify options:

.. code-block:: python

    >>> from inform import Inform, display, error
    >>> Inform(logfile=False, prog_name=False)
    <...>
    >>> display('hello')
    hello
    >>> error('file not found.', culprit='data.in')
    error: data.in: file not found.

An object of the Inform class is referred to as an informer (not to be confused 
with the print functions, which are  referred to as informants). Once 
instantiated, you can use the informer to change various settings, terminate the 
program, or return a count of the number of errors that have occurred.

.. code-block:: python

    >>> from inform import Inform, error
    >>> informer = Inform(prog_name="prog")
    >>> error('file not found.', culprit='data.in')
    prog error: data.in: file not found.
    >>> informer.errors_accrued()
    1

You can create your own informants:

.. code-block:: python

    >>> from inform import Inform, InformantFactory

    >>> verbose1 = InformantFactory(output=lambda m: m.verbosity >= 1)
    >>> verbose2 = InformantFactory(output=lambda m: m.verbosity >= 2)
    >>> with Inform(verbosity=0):
    ...     verbose1('First level of verbosity.')
    ...     verbose2('Second level of verbosity.')

    >>> with Inform(verbosity=1):
    ...     verbose1('First level of verbosity.')
    ...     verbose2('Second level of verbosity.')
    First level of verbosity.

    >>> with Inform(verbosity=2):
    ...     verbose1('First level of verbosity.')
    ...     verbose2('Second level of verbosity.')
    First level of verbosity.
    Second level of verbosity.

The argument *verbosity* is not an explicitly supported argument to Inform.  In 
this case Inform simply saves the value and makes it available as an attribute, 
and it is this attribute that is queried by the lambda function passed to the 
InformantFactory when creating the informants.


Exception
---------
An exception, *Error*, is provided that takes the same arguments as an 
informant.  This allows you to catch the exception and handle it if you like.  
The exception provides the *report* and *terminate* methods that processes the 
exception as an error or fatal error if you find that you can do nothing else 
with the exception:

.. code-block:: python

    >>> from inform import Inform, Error

    >>> Inform(prog_name='myprog')
    <...>
    >>> try:
    ...     raise Error('must not be zero.', culprit='naught')
    ... except Error as e:
    ...     e.report()
    myprog error: naught: must not be zero.

*Error* also provides get_message() and get_culprit() methods, which return the 
message and the culprit. You can also cast the exception to a string to get 
a string that contains both the message and the culprit formatted so that it can 
be shown to the user.

Any keyword arguments provided will be available in *e.kwargs*, but certain 
keyword arguments are reserved by inform (see above).

One common approach to using *Error* is to pass all the arguments that make up 
the error message as unnamed arguments and then assemble them into the message 
by providing a template.  In that way the arguments are directly available to 
the handler if needed. For example:

.. code-block:: python

    >>> from inform import Error, did_you_mean

    >>> known_names = 'alpha beta gamma delta epsilon'.split()
    >>> name = 'alfa'

    >>> try:
    ...     if name not in known_names:
    ...         raise Error(name, template="name '{}' is not defined.")
    ... except Error as e:
    ...     candidates = did_you_mean(e.args[0], known_names)
    ...     e.report(codicil = f"Did you mean {candidates}?")
    myprog error: name 'alfa' is not defined.
        Did you mean alpha?


Utilities
---------

Several utility functions and classes are provided for your convenience. They 
are often helpful when creating messages.

dedent:
    Dedents a block of text.

indent:
    Indents a block of text.

conjoin:
    Like ''.join(), but allows you to specify a conjunction that is placed 
    between the last two elements, ex:

    .. code-block:: python

        >>> from inform import conjoin
        >>> conjoin(['a', 'b', 'c'])
        'a, b and c'

        >>> conjoin(['a', 'b', 'c'], conj=' or ')
        'a, b or c'

did_you_mean:
    Given a word and list of candidates, returns the candidate that is most 
    similar to the word.

cull:
    Strips items from a collection that have a particular value.

join:
    Combines the arguments in a manner very similar to an informant and returns 
    the result as a string.

fmt:
    Similar to ''.format(), but it can pull arguments from the local scope.

render:
    Recursively convert an object to a string with reasonable formatting.  Has 
    built in support for the base Python types (None, bool, int, float, str, 
    set, tuple, list, and dict).  If you confine yourself to these types, the 
    output of render() can be read by the Python interpreter. Other types are 
    converted to string with repr().

plural:
    Produces either the singular or plural form of a word based on a count.

truth:
    Like plural, but for Booleans.

full_stop:
    Adds a period to the end of the string if needed (if the last character is 
    not a period, question mark or exclamation mark).

title_case:
    Converts the initial letters in the words of a string to upper case while 
    maintaining any letters that are already upper case, such as acronyms.

format_range, parse_range:
    Converts a set of numbers to and from a succinct, readable string that 
    summarizes the set.  For example:

    .. code-block:: python

        >>> from inform import format_range, parse_range

        >>> format_range({1, 2, 3, 5})
        '1-3,5'

        >>> parse_range('1-3,5')
        {1, 2, 3, 5}

columns:
    Distribute array over enough columns to fill the screen.

os_error:
    Generates clean messages for operating system errors.

is_str:
    Returns *True* if its argument is a string-like object.

is_iterable:
    Returns *True* if its argument is iterable.

is_collection:
    Returns *True* if its argument is iterable but is not a string.

is_mapping:
    Returns *True* if its argument is a mapping (are dictionary like).

Color:
    A class is used to add color to text.

Info:
    A utility class that automatically converts all keyword arguments into 
    attributes.

ProgessBar:
    A class that produces an interruptable progress bar.

render_bar:
    Converts generates a text bar whose width is controlled by a normalized 
    value.


.. |build status| image:: https://github.com/KenKundert/inform/actions/workflows/build.yaml/badge.svg
    :target: https://github.com/KenKundert/inform/actions/workflows/build.yaml

.. |downloads| image:: https://pepy.tech/badge/inform/month
    :target: https://pepy.tech/project/inform

.. |rtd status| image:: https://img.shields.io/readthedocs/inform.svg
   :target: https://inform.readthedocs.io/en/latest/?badge=latest

.. |coverage| image:: https://img.shields.io/coveralls/KenKundert/inform.svg
    :target: https://coveralls.io/r/KenKundert/inform

.. |pypi version| image:: https://img.shields.io/pypi/v/inform.svg
    :target: https://pypi.python.org/pypi/inform

.. |anaconda version| image:: https://anaconda.org/conda-forge/inform/badges/version.svg
    :target: https://anaconda.org/conda-forge/inform

.. |python version| image:: https://img.shields.io/pypi/pyversions/inform.svg
    :target: https://pypi.python.org/pypi/inform/




            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "inform",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": "inform, logging, printing",
    "author": "Ken Kundert",
    "author_email": "inform@nurdletech.com",
    "download_url": "https://files.pythonhosted.org/packages/b6/1b/ec699af8ada43c6428c71771256b4d3d56828d97577d6e67994ee994a7d7/inform-1.33.tar.gz",
    "platform": null,
    "description": "Inform \u2014 Print & Logging Utilities\n==================================\n\n|downloads| |build status| |coverage| |rtd status| |pypi version| |anaconda version| |python version|\n\n:Author: Ken Kundert\n:Version: 1.33\n:Released: 2024-12-11\n\nA package that provides specialized print functions that are used when \ncommunicating with the user. It allows you to easily print attractive, \ninformative, and consistent error messages.  For example:\n\n.. code-block:: python\n\n    >> from inform import display, warn, error\n    >> display(\n    ..     'Display is like print'\n    ..     'except that it supports logging and can be disabled.'\n    ..     sep=', ')\n    Display is like print, except that it supports logging and can be disabled.\n\n    >> warn('warnings get a header that is printed in yellow.')\n    warning: warnings get a header that is printed in yellow.\n\n    >> error('errors get a header that is printed in red.')\n    error: errors get a header that is printed in red.\n\nInform also provides logging and output control.\n\nIn addition, Inform provides a powerful generic exception that can be used \ndirectly as a general purpose exception, or can be subclassed to produce \npowerful specialized exceptions.  Inform exceptions are unique in that they keep \nall of the named and unnamed arguments so they can be used when reporting \nerrors.\n\nYou can find the documentation on `ReadTheDocs\n<https://inform.readthedocs.io>`_. You can download and install the latest\nstable version of the code from `PyPI <https://pypi.python.org>`_ using::\n\n    pip3 install inform\n\nYou can find the latest development version of the source code on\n`Github <https://github.com/KenKundert/inform>`_.\n\n\nIntroduction\n------------\n\nThis package defines a collection of *print* functions that have different \nroles.  These functions are referred to as *informants* and are described below \nin the Informants section. They include include *log*, *comment*, *codicil*, \n*narrate*, *display*, *output*, *notify*, *debug*, *warn*, *error*, *fatal* and \n*panic*.\n\nWith the simplest use of the program, you simply import the informants you need \nand call them (they take the same arguments as Python's built-in *print* \nfunction):\n\n.. code-block:: python\n\n    >>> from inform import display\n    >>> display('ice', 9)\n    ice 9\n\nFor more control of the informants, you can import and instantiate the Inform \nclass yourself along with the desired informants.  This gives you the ability to \nspecify options:\n\n.. code-block:: python\n\n    >>> from inform import Inform, display, error\n    >>> Inform(logfile=False, prog_name=False)\n    <...>\n    >>> display('hello')\n    hello\n    >>> error('file not found.', culprit='data.in')\n    error: data.in: file not found.\n\nAn object of the Inform class is referred to as an informer (not to be confused \nwith the print functions, which are  referred to as informants). Once \ninstantiated, you can use the informer to change various settings, terminate the \nprogram, or return a count of the number of errors that have occurred.\n\n.. code-block:: python\n\n    >>> from inform import Inform, error\n    >>> informer = Inform(prog_name=\"prog\")\n    >>> error('file not found.', culprit='data.in')\n    prog error: data.in: file not found.\n    >>> informer.errors_accrued()\n    1\n\nYou can create your own informants:\n\n.. code-block:: python\n\n    >>> from inform import Inform, InformantFactory\n\n    >>> verbose1 = InformantFactory(output=lambda m: m.verbosity >= 1)\n    >>> verbose2 = InformantFactory(output=lambda m: m.verbosity >= 2)\n    >>> with Inform(verbosity=0):\n    ...     verbose1('First level of verbosity.')\n    ...     verbose2('Second level of verbosity.')\n\n    >>> with Inform(verbosity=1):\n    ...     verbose1('First level of verbosity.')\n    ...     verbose2('Second level of verbosity.')\n    First level of verbosity.\n\n    >>> with Inform(verbosity=2):\n    ...     verbose1('First level of verbosity.')\n    ...     verbose2('Second level of verbosity.')\n    First level of verbosity.\n    Second level of verbosity.\n\nThe argument *verbosity* is not an explicitly supported argument to Inform.  In \nthis case Inform simply saves the value and makes it available as an attribute, \nand it is this attribute that is queried by the lambda function passed to the \nInformantFactory when creating the informants.\n\n\nException\n---------\nAn exception, *Error*, is provided that takes the same arguments as an \ninformant.  This allows you to catch the exception and handle it if you like.  \nThe exception provides the *report* and *terminate* methods that processes the \nexception as an error or fatal error if you find that you can do nothing else \nwith the exception:\n\n.. code-block:: python\n\n    >>> from inform import Inform, Error\n\n    >>> Inform(prog_name='myprog')\n    <...>\n    >>> try:\n    ...     raise Error('must not be zero.', culprit='naught')\n    ... except Error as e:\n    ...     e.report()\n    myprog error: naught: must not be zero.\n\n*Error* also provides get_message() and get_culprit() methods, which return the \nmessage and the culprit. You can also cast the exception to a string to get \na string that contains both the message and the culprit formatted so that it can \nbe shown to the user.\n\nAny keyword arguments provided will be available in *e.kwargs*, but certain \nkeyword arguments are reserved by inform (see above).\n\nOne common approach to using *Error* is to pass all the arguments that make up \nthe error message as unnamed arguments and then assemble them into the message \nby providing a template.  In that way the arguments are directly available to \nthe handler if needed. For example:\n\n.. code-block:: python\n\n    >>> from inform import Error, did_you_mean\n\n    >>> known_names = 'alpha beta gamma delta epsilon'.split()\n    >>> name = 'alfa'\n\n    >>> try:\n    ...     if name not in known_names:\n    ...         raise Error(name, template=\"name '{}' is not defined.\")\n    ... except Error as e:\n    ...     candidates = did_you_mean(e.args[0], known_names)\n    ...     e.report(codicil = f\"Did you mean {candidates}?\")\n    myprog error: name 'alfa' is not defined.\n        Did you mean alpha?\n\n\nUtilities\n---------\n\nSeveral utility functions and classes are provided for your convenience. They \nare often helpful when creating messages.\n\ndedent:\n    Dedents a block of text.\n\nindent:\n    Indents a block of text.\n\nconjoin:\n    Like ''.join(), but allows you to specify a conjunction that is placed \n    between the last two elements, ex:\n\n    .. code-block:: python\n\n        >>> from inform import conjoin\n        >>> conjoin(['a', 'b', 'c'])\n        'a, b and c'\n\n        >>> conjoin(['a', 'b', 'c'], conj=' or ')\n        'a, b or c'\n\ndid_you_mean:\n    Given a word and list of candidates, returns the candidate that is most \n    similar to the word.\n\ncull:\n    Strips items from a collection that have a particular value.\n\njoin:\n    Combines the arguments in a manner very similar to an informant and returns \n    the result as a string.\n\nfmt:\n    Similar to ''.format(), but it can pull arguments from the local scope.\n\nrender:\n    Recursively convert an object to a string with reasonable formatting.  Has \n    built in support for the base Python types (None, bool, int, float, str, \n    set, tuple, list, and dict).  If you confine yourself to these types, the \n    output of render() can be read by the Python interpreter. Other types are \n    converted to string with repr().\n\nplural:\n    Produces either the singular or plural form of a word based on a count.\n\ntruth:\n    Like plural, but for Booleans.\n\nfull_stop:\n    Adds a period to the end of the string if needed (if the last character is \n    not a period, question mark or exclamation mark).\n\ntitle_case:\n    Converts the initial letters in the words of a string to upper case while \n    maintaining any letters that are already upper case, such as acronyms.\n\nformat_range, parse_range:\n    Converts a set of numbers to and from a succinct, readable string that \n    summarizes the set.  For example:\n\n    .. code-block:: python\n\n        >>> from inform import format_range, parse_range\n\n        >>> format_range({1, 2, 3, 5})\n        '1-3,5'\n\n        >>> parse_range('1-3,5')\n        {1, 2, 3, 5}\n\ncolumns:\n    Distribute array over enough columns to fill the screen.\n\nos_error:\n    Generates clean messages for operating system errors.\n\nis_str:\n    Returns *True* if its argument is a string-like object.\n\nis_iterable:\n    Returns *True* if its argument is iterable.\n\nis_collection:\n    Returns *True* if its argument is iterable but is not a string.\n\nis_mapping:\n    Returns *True* if its argument is a mapping (are dictionary like).\n\nColor:\n    A class is used to add color to text.\n\nInfo:\n    A utility class that automatically converts all keyword arguments into \n    attributes.\n\nProgessBar:\n    A class that produces an interruptable progress bar.\n\nrender_bar:\n    Converts generates a text bar whose width is controlled by a normalized \n    value.\n\n\n.. |build status| image:: https://github.com/KenKundert/inform/actions/workflows/build.yaml/badge.svg\n    :target: https://github.com/KenKundert/inform/actions/workflows/build.yaml\n\n.. |downloads| image:: https://pepy.tech/badge/inform/month\n    :target: https://pepy.tech/project/inform\n\n.. |rtd status| image:: https://img.shields.io/readthedocs/inform.svg\n   :target: https://inform.readthedocs.io/en/latest/?badge=latest\n\n.. |coverage| image:: https://img.shields.io/coveralls/KenKundert/inform.svg\n    :target: https://coveralls.io/r/KenKundert/inform\n\n.. |pypi version| image:: https://img.shields.io/pypi/v/inform.svg\n    :target: https://pypi.python.org/pypi/inform\n\n.. |anaconda version| image:: https://anaconda.org/conda-forge/inform/badges/version.svg\n    :target: https://anaconda.org/conda-forge/inform\n\n.. |python version| image:: https://img.shields.io/pypi/pyversions/inform.svg\n    :target: https://pypi.python.org/pypi/inform/\n\n\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "print & logging utilities for communicating with user",
    "version": "1.33",
    "project_urls": {
        "changelog": "https://github.com/KenKundert/inform/blob/master/doc/releases.rst",
        "documentation": "https://inform.readthedocs.io",
        "homepage": "https://inform.readthedocs.io",
        "repository": "https://github.com/kenkundert/inform"
    },
    "split_keywords": [
        "inform",
        " logging",
        " printing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "daad20fc8106f76c554017f50c6c33650a3d6bf65dcc5a9fcca6b9a83b47eee6",
                "md5": "3d909bb8bfa62e1af2846b314fee3e35",
                "sha256": "3b0c20e0750e58dc6c8cabf7fc6d91cc82cff9f00a71346483b45d5ac3d6556f"
            },
            "downloads": -1,
            "filename": "inform-1.33-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3d909bb8bfa62e1af2846b314fee3e35",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 41585,
            "upload_time": "2024-12-12T05:14:03",
            "upload_time_iso_8601": "2024-12-12T05:14:03.797320Z",
            "url": "https://files.pythonhosted.org/packages/da/ad/20fc8106f76c554017f50c6c33650a3d6bf65dcc5a9fcca6b9a83b47eee6/inform-1.33-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b61bec699af8ada43c6428c71771256b4d3d56828d97577d6e67994ee994a7d7",
                "md5": "1a06c0c67767a9f209adf2a80cd8be4a",
                "sha256": "23637b561da3fbfac6be0206bc1ecf61273164eadbdb0e12fd58ab428c652be4"
            },
            "downloads": -1,
            "filename": "inform-1.33.tar.gz",
            "has_sig": false,
            "md5_digest": "1a06c0c67767a9f209adf2a80cd8be4a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 43160,
            "upload_time": "2024-12-12T05:14:06",
            "upload_time_iso_8601": "2024-12-12T05:14:06.398208Z",
            "url": "https://files.pythonhosted.org/packages/b6/1b/ec699af8ada43c6428c71771256b4d3d56828d97577d6e67994ee994a7d7/inform-1.33.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-12 05:14:06",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "KenKundert",
    "github_project": "inform",
    "travis_ci": true,
    "coveralls": true,
    "github_actions": true,
    "tox": true,
    "lcname": "inform"
}
        
Elapsed time: 0.37746s