Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
f-strings and termcolor
#6
(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 ... Cry

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 ... Think

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).

[Image: S32I8x.png]
cprint(f'{"dangerous".upper():~^20}', 'green', attrs=['reverse', 'blink'], end='')
Dance 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
Reply


Messages In This Thread
f-strings and termcolor - by newbieAuggie2019 - Sep-21-2019, 03:37 PM
RE: f-strings and termcolor - by ndc85430 - Sep-21-2019, 03:42 PM
RE: f-strings and termcolor - by newbieAuggie2019 - Sep-21-2019, 04:34 PM
RE: f-strings and termcolor - by ThomasL - Sep-21-2019, 05:08 PM
RE: f-strings and termcolor - by newbieAuggie2019 - Sep-22-2019, 03:52 AM
RE: f-strings and termcolor - by snippsat - Sep-21-2019, 07:32 PM
RE: f-strings and termcolor - by santarosajoe - Jul-28-2020, 02:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 820 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,820 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Finding multiple strings between the two same strings Slither 1 2,550 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  colorama/termcolor not returning coloured lines kapilan15 3 6,885 Jan-14-2019, 12:57 PM
Last Post: buran
  lists, strings, and byte strings Skaperen 2 4,265 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020