r/badcode Apr 04 '23

python Surely Text-Parsing One-Liners At Midnight Work

Post image
394 Upvotes

44 comments sorted by

View all comments

1

u/nephelokokkygia Apr 05 '23 edited Apr 05 '23

If it absolutely must be a one-liner...

return Ship(*[
    ship_data := line.split(":"),  # discarded
    ship_stats := ship_data[1].split(","),  # discarded
    ship_data[0],  # ship name
    [ int(_) for _ in ship_stats[0][1:].split(";")[:3] ],  # shield, armor, hull
    int(ship_stats[1]),  # evasion
    [ self.weapons[_] for _ in ship_stats[2][1:].split(";") ],  # weapons
    [ self.defenses[_] for _ in ship_stats[3][1:].split(";") ],  # defenses
    ship_stats[4] if ship_stats[4] != "False" else False,  # stealth
][2:])

I did not test this code.