Skip to main content

Convenient Parameter Access

·288 words·2 mins

Internet woes. Can’t connect to the wifi here for some reason so I’m having to use my phone to look up docs. Not the first time I had this problem with this laptop, oof. I should download at least Python and Pygame docs locally. Or fix the connection issue. I might have a USB wifi adapter I could easily try out.

Looks like using TypedDict for my Parameters class has some limitations. I can’t add any other methods to that class. I was hoping to have some utility functions for getting all the values that begin with a certain string or, convert tuples to pygame.Vector2.

I’m’a have a lot of variables and the names got long really quickly.

For example:

parameters['root_segment_pos_linear_term_factor']
parameters['root_segment_pos_linear_term_offset']
parameters['root_segment_pos_constant_term_offset']

It would be nice to be able to do something like this:

p = parameters.namespace('root_segment_pos')
p['linear_term_factor']
p['linear_term_offset']
p['constant_term_offset']

So do I wrap the dict or do I just have some utility functions that live beside it? If I wrap it, I’ll surely lose the key autocomplete. I could also create new classes extending TypedDict to represent the groups of parameters that are related. That would restrict me to groupings I included at creation time, I can’t choose my groups as I need them but… I’m probably creating both at once anyway. Seems like it’d be a lot of extra code, though.

Could go a different route entirely:

prefix = 'root_segment_pos_'
parameters[f"{prefix}linear_term_factor']
parameters[f"{prefix}linear_term_offset']
parameters[f"{prefix}constant_term_offset']

Bit ugly but it takes zero effort.

If I do a helper function:

p = subdict(parameters, 'root_segment_pos')
p['linear_term_factor']
p['linear_term_offset']
p['constant_term_offset']

Ended up with subdict(). It’s pretty straightforward and cleans things up nicely.

Now, to figure out how to place a bunch of sliders and labels and have them update their corresponding parameters.