Sep-22-2019, 03:52 AM
(This post was last modified: Sep-22-2019, 04:11 AM by newbieAuggie2019.)
(Sep-21-2019, 05:08 PM)ThomasL Wrote: I would suggest you look into the termcolor package and investigate how cprint() works.Hi!
Thank you for your answer!
The problem is that I'm a newbie, so I understand what a simple print command does, like:
a = 5 b = 7 print(a + b)producing a simple output:
12
so seeing this in the file termcolor.py, that you kindly included:print((colored(text, color, on_color, attrs)), **kwargs)made me get really lost on many sites, trying to understand what *args and **kwargs are and how to handle them ...
When I looked at the file termcolor.py and saw these lines (I left out the ones not suitable for my problem):
from __future__ import print_function import os print('Test attributes:') cprint('Bold grey color', 'grey', attrs=['bold']) cprint('Underline green color', 'green', attrs=['underline']) cprint('Bold underline reverse cyan color', 'cyan', attrs=['bold', 'underline', 'reverse']) print('Test mixing:') cprint('Underline red on grey color', 'red', 'on_grey', ['underline']) cprint('Reversed green on red color', 'green', 'on_red', ['reverse'])where commands in the format:
cprint('Underline green color', 'green', attrs=['underline'])could be simplified to the format:
cprint('Underline red on grey color', 'red', 'on_grey', ['underline'])I thought that I could imitate the expression where
attrs=['underline']
was simplified to ['underline']
and modify it to adapt it to the f-formatting with ['underline']
and ['bold']
(two characteristics, instead of just one), but when I tried some combinations, I got error messages like:Error:SyntaxError: invalid syntax
Error:SyntaxError: keyword argument repeated
Error:TypeError: unhashable type: 'list'
Error:KeyError: 'underline'
and many more ... All the best,
(Sep-21-2019, 07:32 PM)snippsat Wrote: cprint() takes different argument to function, not so much sense with all arguments to try to make it in one line.
I'm beginning to understand and accept that.
(Sep-21-2019, 07:32 PM)snippsat Wrote: The text part can manipulate with f-string, the other arguments to make it all in line dos not make so much sense here.
I think now that it was just a stupid wish of mine to compare the packages on the same ground while at the same time, applying a topic that I wanted to learn more about (f-strings).
cprint(f'{"dangerous".upper():~^20}', 'green', attrs=['reverse', 'blink'], end='')Thank you! I've just learned another interesting bit of Python!
All the best,
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs