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.7.1 © Copyright 2014-2025 - 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-2025 - A.Travert & C.Fernandez @ LCS
version : 0.7.1
release : 0.7.1
license : CeCILL-B license
url : https://www.spectrochempy.fr
release_date : 2025-02-25
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 |
---|---|
|
diagnose problems on the running process or help developers |
|
general information on the running process |
|
a condition might cause a problem WRT expected |
behaviour |
|
|
wrong argument/commands or bug |
|
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, f"OOPS, THAT'S AN IMPORT ERROR! : {e}")
ERROR | ImportError: OOPS, THAT'S AN IMPORT ERROR! : cannot import name 'toto' from 'spectrochempy' (/home/runner/work/spectrochempy/spectrochempy/src/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]:
from contextlib import suppress
with suppress(Exception):
from spectrochempy import toto # noqa: F401