0

I have a list and I'm trying to fill it with bracketed elements. In it's simplest form, my issue is that I want example=([]) to become example=([('a','b'),('c','d')]).

More explicitly, I'm trying to turn the runnable snippet of code below into a function. But I can't get the list called text to fill properly. Here is the code working:

# import prompt toolkit
from prompt_toolkit import print_formatted_text
from prompt_toolkit.formatted_text import FormattedText
from prompt_toolkit.styles import Style
# My palette
my_palette = {"my_pink": '#ff1493', "my_blue": '#0000ff',}
# The text.
text = FormattedText([('class:my_pink', 'Hello '),('class:my_blue', 'World')])
# Style sheet
style = Style.from_dict(my_palette)
# Print
print_formatted_text(text, style=style)

This my attempt at creating a function, that at one point turns *args into list elements:

def col(*args):
    """Should take `col['word', 'colour']` and return the word in that colour."""
    text = FormattedText([])
    for a in args:
        text_template = ("class:" + str(a[1]) + "', '" + str(a[0]))
        text_template = text_template.replace("'", "")
        text.append(text_template)
    print(text) # Shows what is going on in the `text` variable (nothing good).
    style = Style.from_dict(my_palette)
    print_formatted_text(text, style=style)

The function would be run with something like this:

col(["Hello", 'my_pink'], ["World", 'my_blue'])

The text variable should look like the first example of text, but brackets are missing and the commas are in strings so it looks like this:

text = FormattedText([('class:my_pink, Hello ', 'class:my_blue', 'World'])

Rather than like this:

text = FormattedText([('class:my_pink', 'Hello '), ('class:my_blue', 'World')])

I tried further manipulation, using variations of the following:

text = format(', '.join('({})'.format(i) for i in text))

But honestly I cant understand how I making such a pigs ear of a simple problem. I have tried a lot of 'jammy' solutions but none work and I would like a pythonic one.

Solebay Sharp
  • 519
  • 7
  • 24
  • why do you think `text` is a `deque`? – juanpa.arrivillaga Feb 09 '22 at 02:42
  • I thought that `([])` meant it was a deque. I'm not familiar with the syntax. – Solebay Sharp Feb 09 '22 at 02:51
  • 1
    Anyway, it isn't clear from your description what you are trying to do. It seems like `FormattedText` is [just a subclass of `list`](https://github.com/prompt-toolkit/python-prompt-toolkit/blob/6ac867a58e4a8989e80bbbeedf1a6b86659427af/prompt_toolkit/formatted_text/base.py#L119), in any case, you want to append **a tuple**. `text_template = ("class:" + str(a[1]) + "', '" + str(a[0]))` creates **a string**. You just want `text_template = "class: a[1]", "a[0]"` – juanpa.arrivillaga Feb 09 '22 at 02:52
  • No? That doesn't mean a deque at all. That is just the `__repr__` of that class – juanpa.arrivillaga Feb 09 '22 at 02:52
  • And remove `text_template = text_template.replace("'", "")`. – juanpa.arrivillaga Feb 09 '22 at 02:53
  • And `text.append((text_template))` should just be `text.append(text_template)`, although, those two are exactly equivalent. – juanpa.arrivillaga Feb 09 '22 at 02:53
  • @juanpa.arrivillaga although my solution was terrible and my understanding of the problem was equally bad if not worse, I could hardly have been much clearer on what I wanted to achieve, surely? Yes I tried to use a string at one point but I was running out of ideas. – Solebay Sharp Feb 09 '22 at 02:57
  • 2
    No, it wasn't very clear. To put it clearly, you were trying to create a tuple with two strings. Which wasn't immediately obvious. In any case, I think you need to understand the difference between *string* and *source code*. You can use literals *in source code* like `('foo', 'bar')`, which creates a tuple with two strings. But that *tuple doesn't have any brackets*, or *commas*. Those are aspects of the *source code*, not the actual object*. That is what was confusing about trying to read your question – juanpa.arrivillaga Feb 09 '22 at 02:59
  • Well that I can understand. In my mind, that line 'has' brackets/commas. Even if they are not in the strings. I wouldn't know how else to describe the fact that it didn't 'look' correct. This is why I included an example of what I wanted and what I had. Thank you. – Solebay Sharp Feb 09 '22 at 03:08

1 Answers1

4

You can use list comprehension and f-string:

def col(*args):
    """Should take `col['word', 'colour']` and return the word in that colour."""
    text = FormattedText([(f"class:{a[1]}", str(a[0])) for a in args])
    print(text) # Shows what is going on in the `text` variable (nothing good).
    style = Style.from_dict(my_palette)
    print_formatted_text(text, style=style)
Tianshu Wang
  • 630
  • 2
  • 9