Warning

You are reading the documentation related to the development version. Go here if you are looking for the documentation of the stable release.

API Configuration

Many options of the API can be set up. Let’s first import it in the usual way:

[1]:
import spectrochempy as scp
  SpectroChemPy's API - v.0.6.9.dev9
© Copyright 2014-2024 - A.Travert & C.Fernandez @ LCS

General information

General information on the API can be obtained the following variables:

[2]:
print(f"   copyright : {scp.copyright}")
print(f"     version : {scp.version}")
print(f"     release : {scp.release}")
print(f"     license : {scp.license}")
print(f"         url : {scp.url}")
print(f"release_date : {scp.release_date}")
print(f"     authors : {scp.authors}")
print(f"contributors : {scp.contributors}")
print(f" description : {scp.description}")
   copyright : 2014-2024 - A.Travert & C.Fernandez @ LCS
     version : 0.6.9.dev9
     release : 0.6.9.dev9
     license : CeCILL-B license
         url : https://www.spectrochempy.fr
release_date : 2024-03-06
     authors : C. Fernandez & A. Travert
contributors : A. Ait Blal, W. Guérin, M. Mailänder
 description : SpectroChemPy is a framework for processing, analysing and modelling Spectroscopic data for Chemistry with Python.

Loglevel

During the execution, the API can display, besides the expected output, various messages categorized according to their criticality:

Loglevel / criticality

use

DEBUG / 10

diagnose problems on the running process or help developers

INFO / 20

general information on the running process

WARNING / 30 (default)

a condition might cause a problem WRT expected

behaviour

ERROR / 40

wrong argument/commands or bug

CRITICAL / 50

could lead to a system crash

Not all this information is always necessary and the level of information displayed by SpectroChemPy can be tuned using the command scp.set_loglevel() with the rule that only information having a criticality larger than that passed to the set_loglevel() function will be shown.

For instance, the DEBUG level can be triggered by using one of the equivalent instructions

scp.set_loglevel('DEBUG')
scp.set_loglevel(10)

The current loglevel can be obtained with the scp.get_loglevel() function.

The following instruction prints the current loglevel

[3]:
print(f"Default: {scp.get_loglevel()}")  # print the current loglevel
Default: 30

It yields 30 the numerical value corresponding to the WARNING level. Now, the next instruction lowers the loglevel to "INFO" :

[4]:
scp.set_loglevel(scp.INFO)

We see that the API then delivers the INFO message: "changed default log_level to INFO" .

And finally, the next instructions reset the loglevel to "WARNING" level (default), and print it. As seen below, no message "changed default log_level to ..." is delivered

[5]:
scp.set_loglevel("WARNING")  # reset to default
print(f"New loglevel: {scp.get_loglevel()}")
New loglevel: 30

It is also possible to issue such messages in scripts. In the cell below, we set the loglevel to INFO and try to print two types of messages:

[6]:
scp.set_loglevel("INFO")
scp.info_("this is an info message!")
scp.debug_("this is a debug message!")
 this is an info message!

As expected, only the info message was displayed.

If we change the loglevel to DEBUG , then the two messages will be printed:

[7]:
scp.set_loglevel(scp.DEBUG)
scp.info_("this is an info message!")
scp.debug_("this is a debug message!")
 this is an info message!
 DEBUG | this is a debug message!

Finally, we come back to the standard level of message for the rest of the Tutorial – in this case neither DEBUG nor INFO messages will be printed.

[8]:
scp.set_loglevel(scp.WARNING)

scp.info_("this is an info message!")
scp.debug_("this is a debug message!")

Error handling

If something goes wrong with during a cell execution, a traceback is displayed.

For instance, the object or method toto does not exist in the API, so an error (ImportError ) is generated when trying to import this from the API.

Here we catch the error with a conventional try-except structure

[9]:
try:
    from spectrochempy import toto
except ImportError as e:
    scp.error_(ImportError, "OOPS, THAT'S AN IMPORT ERROR! : %s" % e)
 ERROR | ImportError: OOPS, THAT'S AN IMPORT ERROR! : cannot import name 'toto' from 'spectrochempy' (/home/runner/micromamba/envs/scpy_docs/lib/python3.10/site-packages/spectrochempy/__init__.py)

The error will stop the execution if not caught.

This is a basic behavior of python : one way to avoid stopping the execution without displaying a message is :

[10]:
try:
    from spectrochempy import toto  # noqa: F811, F401
except Exception:
    pass