This release is a pre-release and may not be stable for production use.
The Python module spkpb
I beg your pardon for my english...
English is not my native language, so be nice if you notice misunderstandings, misspellings, or grammatical errors in my documents and codes.
About spkpb
This module proposes two classes and one function that simplify the writing of programs which have to be verbose about a process on files and directories, and that have to emit informations, warnings and errors.
-
Speaker, thespkofspkpb, has methods tho print informations on a terminal and/or in a log file. -
Problems, thepbofspkpb, allows to indicate and store warnings, "criticals" and errors. -
The function
timestampadds time stamps in a log file without printing anything in the terminal.
The following tutorial will starts with the hard way to work with the spkpb tools and finishes with more programmer-friendly tools.
Using directly the API - Default mode
Python code
Let's consider the following Python file where Path is a class proposed by the module pathlib. You have to know that the values of the arguments what are "stringified" (this allows to use either standard strings or advanced classes by defining your own __str__ method for the resume output of problems if you need it).
from spkpb import *
speaker = Speaker(logfile = Path('mylog.log'))
problems = Problems(speaker)
problems.new_warning(
what = Path('one/strange/file.txt'),
info = "some strange behaviors."
)
problems.new_error(
what = Path('one/bad/file.txt'),
info = "bad things appear."
)
speaker.recipe(
NL,
'One basic showcase.',
FORTERM,
{VAR_STEP_INFO: 'ONLY FOR THE TERMINAL OUPUT!',
VAR_LEVEL : 1},
FORLOG,
{VAR_STEP_INFO: 'ONLY IN THE LOG FILE!',
VAR_LEVEL : 1},
)
problems.resume()
The terminal output
Launching our Python code from a terminal, we will see the following output.
1) [ #1 ] WARNING: some strange behaviors.
2) [ #2 ] ERROR: bad things appear.
One basic showcase.
* ONLY FOR THE TERMINAL OUPUT!
---------------
1 WARNING FOUND
---------------
Look at the log file and/or above for details.
* "one/strange/file.txt"
+ 1 warning.
See #.: [1].
-------------
1 ERROR FOUND
-------------
Look at the log file and/or above for details.
* "one/bad/file.txt"
+ 1 error.
See #.: [2].
The content of the log file mylog.log
Launching our Python code, mylog.log will have the following content (just note that the resume is more verbose than the one in a terminal).
1) [ #1 ] WARNING: some strange behaviors.
2) [ #2 ] ERROR: bad things appear.
One basic showcase.
* ONLY IN THE LOG FILE!
---------------
1 WARNING FOUND
---------------
* one/strange/file.txt
+ See [ #.1 ] : some strange behaviors.
-------------
1 ERROR FOUND
-------------
* one/bad/file.txt
+ See [ #.2 ] : bad things appear.
Using directly the API - Silent mode
Let's modify a little our first code (the ellipsis indicate lines unchanged).
from spkpb import *
speaker = Speaker(
logfile = Path('mylog.log'),
silent = True
)
...
The use of the argument silent asks to prints only the summaries of problems (that is useful for short processes with no need to be verbose). The terminal and the log file will show the following same verbose resume.
---------------
1 WARNING FOUND
---------------
* one/strange/file.txt
+ Some strange behaviors.
-------------
1 ERROR FOUND
-------------
* one/bad/file.txt
+ Bad things appear.
Time stamp in the log file
The following code show how to use timestamp such as to add time stamps in the log file.
from spkpb import *
speaker = Speaker(logfile = Path('mylog.log'))
timestamp(
speaker = speaker,
kind = 'start 1',
)
timestamp(
speaker = speaker,
kind = 'start 2',
with_NL = False,
)
timestamp(
speaker = speaker,
kind = 'start 3',
)
This will add the following lines in the log file mylog.log without printing anything in the terminal.
-----------------------------------------
START 1 TIME STAMP: 2021-08-09 (00:40:02)
-----------------------------------------
-----------------------------------------
START 2 TIME STAMP: 2021-08-09 (00:40:02)
-----------------------------------------
-----------------------------------------
START 3 TIME STAMP: 2021-08-09 (00:40:02)
-----------------------------------------
A ready-to-use communicating class
We have seen hard use of the API of spkpb. Indeed you can heritate the class BaseCom to do things easily: see the following code and outputs.
Python code
from spkpb import *
project = BaseCom(
Problems(
Speaker(logfile = Path('mylog.log'))
)
)
project.timestamp(kind = 'start')
project.new_warning(
what = Path('one/strange/file.txt'),
info = "some strange behaviors."
)
print(f'>>>>>>>> sucess = {project.success}')
project.new_error(
what = Path('one/bad/file.txt'),
info = "bad things appear."
)
print(f'>>>>>>>> sucess = {project.success}')
project.recipe(
NL,
'One basic showcase.',
FORTERM,
{VAR_STEP_INFO: 'ONLY FOR THE TERMINAL OUPUT!',
VAR_LEVEL : 1},
FORLOG,
{VAR_STEP_INFO: 'ONLY IN THE LOG FILE!',
VAR_LEVEL : 1},
)
project.resume()
project.recipe(NL)
project.timestamp(kind = 'end')
The terminal output
1) [ #1 ] WARNING: some strange behaviors.
>>>>>>>> sucess = True
2) [ #2 ] ERROR: bad things appear.
>>>>>>>> sucess = False
One basic showcase.
* ONLY FOR THE TERMINAL OUPUT!
---------------
1 WARNING FOUND
---------------
Look at the log file or above for details.
* one/strange/file.txt
+ 1 warning.
See #.: [1].
-------------
1 ERROR FOUND
-------------
Look at the log file or above for details.
* one/bad/file.txt
+ 1 error.
See #.: [2].
The content of the log file mylog.log
---------------------------------------
START TIME STAMP: 2021-08-10 (11:40:02)
---------------------------------------
1) [ #1 ] WARNING: some strange behaviors.
2) [ #2 ] ERROR: bad things appear.
One basic showcase.
* ONLY IN THE LOG FILE!
---------------
1 WARNING FOUND
---------------
* one/strange/file.txt
+ See [ #.1 ] : some strange behaviors.
-------------
1 ERROR FOUND
-------------
* one/bad/file.txt
+ See [ #.2 ] : bad things appear.
-------------------------------------
END TIME STAMP: 2021-08-10 (11:40:02)
-------------------------------------
Reset the management of problems
What we want...
A communicating process can be restarted several times. This needs to reset every informations stored and displayed. To acheive that, the classes BaseCom, Problems and Speaker all have a method reset. The following weird example shows how this method works.
Python code
from spkpb import *
project = BaseCom(
Problems(
Speaker(logfile = Path('mylog.log'))
)
)
project.new_warning(
what = Path('one/strange/file.txt'),
info = "some strange behaviors."
)
project.reset()
project.new_error(
what = Path('one/bad/file.txt'),
info = "bad things appear."
)
project.resume()
The terminal output
1) [ #1 ] WARNING: some strange behaviors.
1) [ #1 ] ERROR: bad things appear.
-------------
1 ERROR FOUND
-------------
Look at the log file or above for details.
* one/bad/file.txt
+ 1 error.
See #.: [1].
Who has chosen this stupid example? :-)
The content of the log file mylog.log
1) [ #1 ] ERROR: bad things appear.
-------------
1 ERROR FOUND
-------------
* one/bad/file.txt
+ See [ #.1 ] : bad things appear.
Metadata
Release files for spkpb 0.1.0b0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| spkpb-0.1.0b0.tar.gz | 28.2 kB | Without provenance | Details for spkpb-0.1.0b0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| spkpb-0.1.0b0-py3-none-any.whl | Python 3 | none | any | Details for spkpb-0.1.0b0-py3-none-any.whl |
Total release size: 58.1 kB
Release files / spkpb-0.1.0b0.tar.gz
| Download URL | spkpb-0.1.0b0.tar.gz |
|---|---|
| Size | 28.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
866327ef4f1e32767ee23254508a4b05ae7033cdbbb5fa91e5c6c1d8312b02ba
|
|
BLAKE2b-256 checksum How to use checksums |
1f8c896490a50a0818b0f259149f4c387341b643cf43d167d9e397a84b59a04c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.6 CPython/3.8.11 Darwin/20.6.0
|
Release files / spkpb-0.1.0b0-py3-none-any.whl
| Download URL | spkpb-0.1.0b0-py3-none-any.whl |
|---|---|
| Size | 29.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f4837aa01cee2664bf0d71bdcb6f43fd274b689c9ce6d15324aff392764e7ad2
|
|
BLAKE2b-256 checksum How to use checksums |
13274b0d8d8f9e0b509cc42f59e7d7f1c1790312866bbb7b6ab96203b7ad3ad9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.6 CPython/3.8.11 Darwin/20.6.0
|
Security for spkpb 0.1.0b0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.