What's the alternative to nested dictionaries?
I'm a complete rookie, but I've been trying to teach myself how to use python by automating character creation in my favorite tabletop rpgs. It's been fun so far but I feel like I'm at the point now where "I don't know what I don't know," you know?
So table top rpgs have a lot of tables that you roll on, so I've got a lot of lists and i'm making ample use of random. Then I discovered dictionaries, and that they can have lists, and heck a single key could have a list of lists! ... and this is where I start to think I've gone astray.
It's becoming harder to understand how my data is structured, and it's awful to try and make sense of. I'm sure there is a structure more suitable to what I'm doing but I don't even know what what I'm doing is called, so I don't know how to find a better solution.
If I haven't lost you, I'd love a little expert guidance here. Even just, "Look into <blank>". I am willing to dig in myself, just need a sign post.
Thanks
Posted Jun 15, 2021, 5:03 AM UTC
A dataclass is a good choice. if you want to store data.
If you want to learn more about tables pandas dataframes are really fast and surprisingly easy to use.
Tbh this seems more confusing the more nested dictionaries there are.
Why don't you post a short example of how your data is structured currently. Maybe I can explain how this would work for you better.
I used an approach with dataclasses to model the damage system in pathfinder 2e and make a combat simulator so I could find optimal builds and skill to use. Factoring different weapon types, skills, whether you have a pet attacking, flanking, melee vs range and dual wield options and a variety of different combat strategies.
Lol I’m not the OP, I just don’t think this would help the OP answer his question. It may be another solution but doesn’t answer the question.
The question was, what's a simpler way of storing my role playing game data than using nested dictionaries.
Using some sort of class, a dataclass specifically is simple, easy to use, and fits the bill of exactly what OP was asking for.
OP:
Try to imagine a series of complex nested dictionary with class, race, attributes, weapons, weapon types, damage types, ability scores, feats.
It's going to be a mess as nested dictionaries.
But as a series of classes? It all becomes very easy to keep in order and maintain.
[deleted]
Thanks for the tip, I'll look into dataclass. Is it very different from a regular class?
No not at all, it's just a type of class. You can add methods to it like a regular class. Or you could just use a regular class if you're more comfortable with that.
I like to think of dataclasses as "easy classes" because there's a lot less work to do to get a lot of benefit.
vs:
With an actual class you start getting into areas like "what is self", why
__init__(). =)I find for teaching purposes dataclasses are easier to understand than learning all bout classes.
btw, what does your data look like?
If you want to post your code to git and link it here we can take a look and possibly help you with some ideas. Some of us here have worked on rpg code in the past.
[removed]