-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When assigning, how to generate a different missing
structure based on path type?
#224
Comments
missing
structure based on path type?missing
structure based on path type?
that's a great suggestion, providing arguments to missing |
Here to bump this issue and check if any progress has been made on this? Otherwise is there another way we can assign between lists and dicts on current version? |
@Kwill3 we haven't really done anything with this. I think everyone's open to the idea, though. I guess the next step in the conversation is to ask what arguments would be useful? We could pass the current path, the current glom scope (possibly too technical for day-to-day use), just the name of the current field, etc.? A really cool thing would be to create one's own spec type (forked from Assign) and experiment, if you'd like to help advance the feature. Docs on spec type creation are here: https://glom.readthedocs.io/en/latest/custom_spec_types.html |
Here's a suggestion: Usagefrom glom import glom, Assign, MATCH
result = glom(target_dict, Assign("a.b.0.c", value, missing=MATCH)) Codemutation.py: MATCH = "match" https://github.com/mahmoud/glom/blob/master/glom/mutation.py#L173 except PathAccessError as pae:
if not self.missing:
raise
remaining_path = self._orig_path[pae.part_idx + 1:]
# NEW CODE STARTS HERE
if self.missing == MISSING_MATCH:
# Naive idea about how to figure out this path level's type. The maintainers will have
# more context about wether this is adequate.
this_path_level = self._orig_path[pae.part_idx]
# AFAIK glom only supports two types with path notation.
# Alternatively we could look at the parent target or parent dest's type
missing = list if this_path_level.isnumeric() else dict
else:
missing = self.missing
val = scope[glom](missing(), Assign(remaining_path, val, missing=self.missing), scope) |
Starting from an empty dict, I might make assignments like this:
The result I expect is the following:
although I understand that it could also be interpreted as
This means that I want the
missing
function to create a list for the missing structure "a", and a dict for the missing structure "a.0".Unfortunately,
missing
isn't called with any arguments that would allow me to return a list or a dict depending on whether the next path part is an int or a string.Is there some other way to do what I want?
The text was updated successfully, but these errors were encountered: