I am trying to understand the usage of _nameToLevel
and _levelToName
methods available in logging. I tried to read up using documentation (https://docs.python.org/3.8/library/logging.html) but was not really able to find any information with any good example.
Next attempt was to understand by going into logging.__init__.py
RITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0
_levelToName = {
CRITICAL: 'CRITICAL',
ERROR: 'ERROR',
WARNING: 'WARNING',
INFO: 'INFO',
DEBUG: 'DEBUG',
NOTSET: 'NOTSET',
}
_nameToLevel = {
'CRITICAL': CRITICAL,
'FATAL': FATAL,
'ERROR': ERROR,
'WARN': WARNING,
'WARNING': WARNING,
'INFO': INFO,
'DEBUG': DEBUG,
'NOTSET': NOTSET,
}
Due to the underscore use the intention of these dictionaries to be private and use getLevelName
to access.
But I see code bases using from logging import _levelToName, _nameToLevel
. Trying this on Python REPL is fine.
Python 3.8.10 (default, May 19 2021, 11:01:55)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from logging import _nameToLevel, _levelToName
>>>
But when you try to do this in editor (PyCharm) you ended up getting following even with PyCharm is accurately setup to point to the Conda environment
Cannot find reference '_levelToName' in '__init__.pyi'
Cannot find reference '_nameToLevel' in '__init__.pyi'
Questions:
- Why is PyCharm is complaining?
- A usecase / example of usage?