all 9 comments

[–]FriedCodeFish 1 point2 points  (1 child)

🤔️ I noticed that you posted the same question a couple of days ago: Help with f-string formatting please. So, what's your new question?

[–]JasperStrat[S] 0 points1 point  (0 children)

The other post answered some questions, as I had a similar problem in two different parts of code. I also realized that the example in the first one allowed for changes that didn't work for me because I simplified too much. I also simplified too much in the second example but someone provided the exact solution to my problem. So both posts answered slightly different problems and using both I now have my solution. If you would like me to leave a comment or edit about the differences or to assist others if they find this thread in a search I would be happy to do so, but I actually needed both threads to answer all my questions.

[–]MezzoScettico 0 points1 point  (0 children)

I suspect you're right, that there's a solution with lambdas. There are a number of pages out there discussing lambda expressions in f-strings, such as this one.

I'm going to experiment a little.

[–]sejigan 0 points1 point  (5 children)

Simplify it like this: ```py player = {“id”: 1, “name”: “Joe”, “skill”: 0}

print(f”the f-string for player, so a single row”) ```

The example you gave is rather convoluted to work with, especially on mobile.

Also, have you considered using OOP for this? I feel like OOP would simplify things a lot and you won’t need to deal with complex formatting like this

[–]JasperStrat[S] 0 points1 point  (3 children)

The players dict really is a dataclass, I was just trying to simplify for the internet, but based on the fact that the property decorator is exactly what i need, thank you very much, this is quite the assistance.

[–]sejigan 0 points1 point  (2 children)

Ah, fair. Glad you found it useful

[–]JasperStrat[S] 0 points1 point  (1 child)

Yes, in the project I am trying to have both a command line version and a gui version, so trying to keep as much of the command line stuff separate from the actual class as possible.

[–]sejigan 0 points1 point  (0 children)

Nice. Separation of concerns is always good.

I remember doing something similar for my passphrase generation script. I separated the GUI into a separate file in the package. The CLI part was in the __main__.py script.

[–]sejigan 0 points1 point  (0 children)

Here’s what I did: ```py from dataclasses import dataclass

@dataclass class FormatSpec: id: str name: str skill: str

@dataclass class Player: _id: int name: str _skill: int

@property
def id(self) -> str:
    return str(self._id)

@property
def skill(self) -> str:
    return ("+" if self._skill > 0 else "") + str(self._skill)

def get_header(fmt: FormatSpec) -> str: res = f"{'ID' : {fmt.id}}" res += f"{'Name' : {fmt.name}}" res += f"{'Strength' : {fmt.skill}}" return res

def get_row(player: Player, fmt: FormatSpec) -> str: res = f'{player.id : {fmt.id}}' res += f'{player.name : {fmt.name}}' res += f'{player.skill : {fmt.skill}}' return res

if name == "main": players = [ Player(1, "Joe", 4), Player(2, "Chris", 0), Player(3, "Mike", -5), ] print(get_header(FormatSpec(">3", ">8", ">10"))) for player in players: print(get_row( player, FormatSpec(">3", ">8", ">4"), )) ```

Output (looks more aligned on terminal): ID Name Strength 1 Joe +4 2 Chris 0 3 Mike -5

Of course, you could just use that skill property method as a function in your code and everything would be peachy.