A generic logger
Project description
sparen
Simple logging library supporting terminal coloring, file saving, and graph plot output.
Table of contents
Install
$ pip3 install sparen
Examples
#----------------------------------------------------------------
# The simple
import sparen
sparen.Log("Hello world!")
#----------------------------------------------------------------
# More options
import json
import argparse
import numpy as np
import sparen
Log = sparen.Log
def fun2():
something = nogood
def fun1():
fun2()
def main():
Log("sparen version: %s" % sparen.__version__)
Log(dir(sparen))
ap = argparse.ArgumentParser(description='Test')
ap.add_argument('--format', '-f', default='error:red,warning:yellow,except:red', type=str, help='Log format')
ap.add_argument('--logfile', '-l', default='', type=str, help='Log file')
_p = vars(ap.parse_args())
if _p['logfile']:
Log.setLogFile(_p['logfile'])
Log("Parameters: " + json.dumps(_p, indent=4, sort_keys=True))
if _p['format']:
Log.addLogFilters(_p['format'])
Log('a', 'b', 'c', 'ok')
Log("WARNING: Don't do that again")
Log("ERROR: Bad thing happened")
try:
fun1()
except Exception as e:
Log('Something went really wrong\n', e)
Log('PLOT\r\n', sparen.plotArray(np.sin(np.linspace(-np.pi * 3, np.pi * 3, 200)) * 10))
if __name__ == '__main__':
main()
Output
[21:10:12] test.py:main(18): sparen version: 0.1.1
[21:10:12] test.py:main(29): Parameters: {
"format": "error:red,warning:yellow,except:red",
"logfile": ""
}
[21:10:12] test.py:main(34): a b c ok
[21:10:12] test.py:main(36): WARNING: Don't do that again
[21:10:12] test.py:main(38): ERROR: Bad thing happened
[21:10:12] test.py:main(43): Something went really wrong
[EXCEPTION] name 'nogood' is not defined
> test.py(11)::fun2() something = nogood
> test.py(14)::fun1() fun2()
> test.py(41)::main() fun1()
[21:10:12] test.py:main(45): PLOT
10 : ... .... ....
9 : .. . . .. .. ..
7 : . .. . .. . ..
5 : . . . . . ..
3 : . . .. . .. .
1 : .. .. . .. . .
0 : . . . . . .
-2 : .. . . . . ..
-4 : .. .. .. .. .. .
-6 : . .. .. .. .. .
-8 : .. .. .. .. .. ..
-10 : .... .... ....
------------------------------------------------------------------------
10^ 20^ 30^ 40^ 50^ 60^ 70^
#----------------------------------------------------------------
# Canvas drawing example
import sparen
Log = sparen.Log
def main():
# First example
canv = sparen.Canvas(width=80, height=25, charset=0)
canv.line(2, 2, 40, 2, '.')
canv.line(77, 2, 77, 12, '.')
canv.rect(4, 4, 20, 10, '.')
canv.line(4, 4, 20, 10, '.')
canv.fillRect(25, 6, 45, 14, '.')
canv.circle(60, 6, 5, '.')
canv.arc(60, 6, 5, 0, 180, '*')
canv.text(60, 6, "2")
canv.rect(10, 15, 50, 23, '.')
canv.textBox(11, 15, 49, 23, 'This is a lot of text just to see how well it fits in to the'
+' specified box, I could go on and on, and I will, because the'
+' point here is to make a really long string and not to not'
+' freak out people that do not like to see a lot of text.')
canv.rect(55, 15, 79, 23, '.')
canv.textBox(55, 15, 79, 23, 'ThisWordIsJustTooLongToFitOnOneLineAndMustBeForcefullySplit.\n'
+' So \n be \n it.')
Log("First Example\n", canv)
# Second example
canv = sparen.Canvas(width=80, height=25, charset=1)
canv.rect(4, 4, 20, 10)
canv.line(2, 6, 25, 6)
canv.line(4, 8, 20, 8)
canv.line(10, 2, 10, 12)
canv.line(16, 4, 16, 10)
canv.rect(44, 4, 60, 10)
canv.line(42, 4, 65, 4)
canv.line(42, 10, 68, 10)
canv.rect(24, 14, 40, 20)
canv.line(24, 12, 24, 22)
canv.line(40, 12, 40, 24)
canv.rect(45, 14, 65, 20)
canv.rect(50, 16, 70, 22)
canv.rect(55, 18, 75, 24)
Log("Second Example\n", canv)
if __name__ == '__main__':
main()
Output
[21:48:26] test.py:test_2(59): First Example
.
...................................... ... . . ... .
. . .
................. . . .
. ... . . . .
. ... . .................... .* 2 ** .
. .. . .................... * * .
. ... . .................... * * .
. ... .................... * * .
................. .................... *** * * *** .
.................... * .
....................
....................
......................................... .........................
. This is a lot of text just to see how . .ThisWordIsJustTooLongTo.
. well it fits in to the specified box, . .FitOnOneLineAndMustBeFo.
. I could go on and on, and I will, . . rcefullySplit. .
. because the point here is to make a . . So .
. really long string and not to not . . be .
. freak out people that do not like to . . it. .
. see a lot of text. . . .
......................................... .........................
[21:48:26] test.py:test_2(59): First Example (character set 2)
·
═══════════════════════════════════════ ··· · · ··· ║
· · ║
╔═══════════════╗ · · ║
║···· ║ · · ║
║ ···· ║ ████████████████████ ·* 2 ** ║
║ ··· ║ ████████████████████ * * ║
║ ····║ ████████████████████ * * ║
║ ·║ ████████████████████ * * ║
╚═══════════════╝ ████████████████████ *** * * *** ║
████████████████████ * ║
████████████████████ ║
████████████████████
╔═══════════════════════════════════════╗ ╔═══════════════════════╗
║ This is a lot of text just to see how ║ ║ThisWordIsJustTooLongTo║
║ well it fits in to the specified box, ║ ║FitOnOneLineAndMustBeFo║
║ I could go on and on, and I will, ║ ║ rcefullySplit. ║
║ because the point here is to make a ║ ║ So ║
║ really long string and not to not ║ ║ be ║
║ freak out people that do not like to ║ ║ it. ║
║ see a lot of text. ║ ║ ║
╚═══════════════════════════════════════╝ ╚═══════════════════════╝
[21:48:26] test.py:test_3(86): Second Example
│
│
┌─────┼─────┬───┐ ──┬───────────────┬─────
│ │ │ │ │ │
──┼─────┼─────┼───┼───── │ │
│ │ │ │ │ │
├─────┼─────┼───┤ │ │
│ │ │ │ │ │
└─────┼─────┴───┘ ──┴───────────────┴────────
│
│ │ │
│ │
┌───────┬───────┐ ├───────────────┤ ┌───────────────────┐
│ │ │ │ │ │ │
│ │ │ │ │ │ ┌──────────────┼────┐
│ │ │ │ │ │ │ │ │
├───────┼───────┤ │ │ │ │ ┌─────────┼────┼────┐
│ │ │ │ │ │ │ │ │ │ │
│ │ │ ├───────────────┤ └────┼────┼─────────┘ │ │
│ │ │ │ │ │ │ │ │
└───────┴───────┘ │ │ └────┼──────────────┘ │
│ │ │
│ └───────────────────┘
References
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
File details
Details for the file sparen-0.2.1-py2.py3-none-any.whl
.
File metadata
- Download URL: sparen-0.2.1-py2.py3-none-any.whl
- Upload date:
- Size: 11.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ccdeed7885fff9bdb13f3e8df5237359337a367129a8833dcc706ada70136d11 |
|
MD5 | b274a92675ffe6101c14040ed8f53343 |
|
BLAKE2b-256 | 6a456ec234c47a2ea69da0e252cafe28c3ca9a52dc8c8ae8a149ee3e667bb8ce |