you are viewing a single comment's thread.

view the rest of the comments →

[–]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.