landlab.core.messages¶
Print user messages formatted landlab-style.
This module provides functions for printing nicely-formatted messages to the user. Messages are formatted in a particular style so that all of landlab messages will have a similar look. Anytime landlab prints something for an end-user to see, this module should be used.
This module also provides convenience functions for print particular types of messages. Warning and error messages, for instance.
Oftentimes when writing code we may need to print a lengthy message for the user. This may result in code that looks like the following.
>>> message = (
... "Lorem ipsum dolor sit amet, consectetur "
... "adipiscing elit, sed do eiusmod tempor "
... "incididunt ut labore et dolore magna aliqua. "
... "Ut enim ad minim veniam, quis nostrud exercitation "
... "ullamco laboris nisi ut aliquip ex ea commodo "
... "consequat."
... )
Printing this message string would result in one long line that would, most likely, extend beyond the user’s terminal and be difficult to read. One solution would be to join the lines by line separators but then that would result in a bunch of really short lines.
To help with this, landlab provides a set of functions with the most basic being format_message.
>>> from landlab.core.messages import format_message
>>> print(format_message(message))
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat.
landlab also provides functions for printing warning and error messages.
>>> from landlab.core.messages import warning_message
>>> message = (
... "Lorem ipsum dolor sit amet, consectetur\n"
... "adipiscing elit, sed do eiusmod tempor\n"
... "incididunt ut labore et dolore magna aliqua."
... )
>>> print(warning_message(message))
.. warning::
<BLANKLINE> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
>>> from landlab.core.messages import error_message >>> print(error_message(message))
Error
<BLANKLINE> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Another common design pattern used in landlab is to allow the user, usually through a keyword, to control what happens if a particular assertion fails. For instance, the user may want the code to raise an error, or print a warning message, or do nothing at all. The assert_or_print function should be used in these cases.
>>> from landlab.core.messages import assert_or_print
>>> dt = 1e6
>>> assert_or_print(dt < 1, "Unstable time step!", onerror="pass")
>>> assert_or_print(dt < 1, "Unstable time step!", onerror="warn")
...
Warning
Unstable time step!
>>> assert_or_print(dt < 1, "Unstable time step!", onerror="raise")
Traceback (most recent call last):
...
AssertionError
- assert_or_print(cond, msg=None, onerror='raise', file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)[source]¶
Make an assertion printing a message if it fails.
Specify an action to take if an assertion fails, depending on the values of onerror. onerror must be one of:
“pass”: do nothing if the assertion passes or fails.
“warn”: print a warning message if the assertion fails.
“error”: print an error message and raise an AssertionError on failure.
- Parameters:
cond (expression) – An expression to test.
msg (str, optional) – Message to print if the condition is not met.
onerror ({'pass', 'warn', 'raise'}, optional) – What to do if the condition evaluates to False.
file (file_like, optional) – File-like object where the message is printed.
Examples
>>> from landlab.core.messages import assert_or_print
>>> assert_or_print(True, "Lorem ipsum", onerror="pass") >>> assert_or_print(False, "Lorem ipsum", onerror="pass")
>>> assert_or_print(True, "Lorem ipsum", onerror="warn") >>> assert_or_print(False, "Lorem ipsum", onerror="warn")
>>> assert_or_print(True, "Lorem ipsum", onerror="raise") >>> assert_or_print(False, "Lorem ipsum", onerror="raise") Traceback (most recent call last): ... AssertionError
- deprecation_message(msg=None, **kwds)[source]¶
Create a deprecation message, landlab-style.
- Parameters:
msg (str, optional) – Warning message.
- Returns:
The formatted warning message.
- Return type:
Examples
>>> from landlab.core.messages import deprecation_message >>> print(deprecation_message("Dictumst vestibulum rhoncus est pellentesque.")) DEPRECATION WARNING =================== Dictumst vestibulum rhoncus est pellentesque.
>>> print( ... deprecation_message( ... "Dictumst vestibulum rhoncus est pellentesque.", ... use="Lorem ipsum dolor sit amet", ... ) ... ) DEPRECATION WARNING =================== Dictumst vestibulum rhoncus est pellentesque.
Example
Lorem ipsum dolor sit amet
- error_message(msg=None, **kwds)[source]¶
Create an error message, landlab-style.
- Parameters:
msg (str, optional) – Warning message.
- Returns:
The formatted warning message.
- Return type:
Examples
>>> from landlab.core.messages import error_message >>> print(error_message("Dictumst vestibulum rhoncus est pellentesque."))
Error
<BLANKLINE> Dictumst vestibulum rhoncus est pellentesque.
- format_message(msg, header=None, footer=None, linesep='\n')[source]¶
Format a message, landlab-style.
Create a nicely formatted message that splits paragraphs, dedents paragraphs, and wraps text at 70 characters. Optionally, add a header and footer to the resulting message.
- Parameters:
- Returns:
The formatted message.
- Return type:
Examples
>>> from landlab.core.messages import format_message >>> text = ''' ... Lorem ipsum dolor sit amet, consectetur ... adipiscing elit, sed do eiusmod tempor ... incididunt ut labore et dolore magna aliqua. ... ... Pharetra pharetra massa massa ultricies mi ... quis hendrerit. ... ... Dictumst vestibulum rhoncus est pellentesque. ... Sed viverra tellus in hac habitasse platea ... dictumst vestibulum rhoncus.''' >>> print(format_message(text)) Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Pharetra pharetra massa massa ultricies mi quis hendrerit. Dictumst vestibulum rhoncus est pellentesque. Sed viverra tellus in hac habitasse platea dictumst vestibulum rhoncus.
- indent_and_wrap(content, indent='')[source]¶
Indent and wrap some text.
Lines are first dedented to remove common leading whitespace, then indented according to the value of indent, and then wrapped at 70 characters (indenting if necessary with subsequent indent being twice indent).
Note that when looking for common whitespace, the first line is ignored.
- Parameters:
content (str) – The content to wrap.
- Returns:
The content properly wrapped and indented.
- Return type:
Examples
>>> from landlab.core.messages import indent_and_wrap >>> content = '''@book{knuth1998art, ... title={The art of computer programming: sorting and searching}, ... author={Knuth, Donald Ervin}, ... volume={3}, ... year={1998}, ... publisher={Pearson Education} ... }''' >>> print(indent_and_wrap(content)) @book{knuth1998art, title={The art of computer programming: sorting and searching}, author={Knuth, Donald Ervin}, volume={3}, year={1998}, publisher={Pearson Education} }
- split_paragraphs(msg, linesep='\n')[source]¶
Split text into paragraphs.
Split a block of text into paragraphs. A paragraph is defined as adjacent new-line characters (possibly separated by some whitespace).
- Parameters:
- Returns:
List of paragraphs.
- Return type:
Examples
>>> from landlab.core.messages import split_paragraphs >>> text = ''' ... Pharetra pharetra massa massa ultricies mi quis hendrerit. ... ... Dictumst vestibulum rhoncus est pellentesque. ... ''' >>> split_paragraphs(text, linesep="\n") ['Pharetra pharetra massa massa ultricies mi quis hendrerit.', 'Dictumst vestibulum rhoncus est pellentesque.']
>>> text = ''' ... Pharetra pharetra massa massa ultricies mi quis hendrerit. ... Dictumst vestibulum rhoncus est pellentesque. ... ''' >>> len(split_paragraphs(text, linesep="\n")) 1
- warning_message(msg=None, **kwds)[source]¶
Create a warning message, landlab-style.
- Parameters:
msg (str, optional) – Warning message.
- Returns:
The formatted warning message.
- Return type:
Examples
>>> from landlab.core.messages import warning_message >>> print(warning_message("Dictumst vestibulum rhoncus est pellentesque."))
Warning
<BLANKLINE> Dictumst vestibulum rhoncus est pellentesque.