A Python dict that can report which keys you did not use
1–10 of 45 posts
Re: A Python dict that can report which keys you did not use
#2This can be used to evaluate the migration quality and spot what can be improved.
https://github.com/xwiki-contrib/confluence/blob/7a95bf96787...
Re: A Python dict that can report which keys you did not use
#3Re: A Python dict that can report which keys you did not use
#4Re: A Python dict that can report which keys you did not use
#5Re: A Python dict that can report which keys you did not use
#6I think if you feel like you need this then it's a bit of a red flag and you should be using Pydantic or `dataclass` instead, then your IDE can statically tell you which fields you don't access (among many other benefits). Dicts are mainly for when you don't know the keys up front.
Re: A Python dict that can report which keys you did not use
#7If you're inheriting from dict to extend its behavior, there are a lot of side effects with that, and it's recommended to use https://docs.python.org/3/library/collections.html#collectio... instead.
> The need for this class has been partially supplanted by the ability to subclass directly from dict; however, this class can be easier to work with because the underlying dictionary is accessible as an attribute.
Sounds like (unless you need the dict as a separate data member) this class is a historical artefact. Unless there's some other issue you know of not mentioned in the documentation?
Re: A Python dict that can report which keys you did not use
#8Just a heads up, this fails to track usage of get and setdefault . The ability to iterate over dicts makes the whole question rather murky.
Another way to get data out would be to use the new | operator (i.e. x = {} | y essentially copies dictionary x to y) or the update method or ** unpacking operator (e.g. x = {**y}). But maybe those come under the umbrella of iterating as you mentioned.
Re: A Python dict that can report which keys you did not use
#9I think if you feel like you need this then it's a bit of a red flag and you should be using Pydantic or `dataclass` instead, then your IDE can statically tell you which fields you don't access (among many other benefits). Dicts are mainly for when you don't know the keys up front.
That is, it isn't just knowing whether or not the data is ever used. It is useful to know if it was used in this specific run. And often times, seeing what parts of the data was not used is a good clue as to what went wrong. At the least, you can use it to rule out what code was not hit.
Re: A Python dict that can report which keys you did not use
#10Just a heads up, this fails to track usage of get and setdefault . The ability to iterate over dicts makes the whole question rather murky.
I didn't know about the setdefault method, and wouldn't have guessed it lets you read a value. Interesting, thanks. Another way to get data out would be to use the new | operator (i.e. x = {} | y essentially copies dictionary x to y) or the update method or ** unpacking operator (e.g. x = {**y}). But maybe those come under the umbrella of iterating as you mentioned.