You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Something like collections.defaultdict but where the factory needs to be contextual (because different levels may have different stores.
What happens if I do:
s[1][2][3][4] =val
and s[1] doesn't have the 2 key?
Maybe this:
s[1][2] = EmptyStoreType2()
s[1][2][3] = EmptyStoreType3()
s[1][2][3][4] = val
and now non of the stores above are empty anymore.
Some code to think of
# NOTE: perhaps use glom and/or remap?fromtypingimportMappingfromi2importmk_sentineldefis_a_mapping(store, prev_path):
returnisinstance(store, Mapping)
defmk_a_dict(prev_path): # should we include current_key too?returndict()
def_split_path(
store, path: tuple, store_to_explore_further=is_a_mapping, prev_path=()
):
"""Separate part of the path that has a value in store and the part that doesn't"""ifnotpath: # path is emptyreturnprev_path, (), ()
# else:key, *next_path=pathval=store.get(key, NoSuchKey)
ifvalisNoSuchKey:
# if key doesn't exist, everything from there on is the store-invalid partreturnprev_path, (key,) +next_pathelse:
# if key doesn't exist, everything from there on is the store-invalid partifstore_to_explore_further(val, prev_path):
return_advance_to_first_non_existing_key(
val,
next_path,
store_to_explore_further,
prev_path=prev_path+ (key,)
):
else:
returnprev_path, key, next_pathNoSuchKey=mk_sentinel('NoSuchKey')
defdefault_store(
store,
path: tuple,
store_to_explore_further=is_a_mapping,
mk_new_store=mk_a_dict# factory part
):
# k = k.split(store.path_sep) (if k is string)prev_path, problem_path=_advance_to_first_non_existing_key(
store, path, store_to_explore_further, mk_new_store
)
current_path=prev_pathcurrent_store=store[current_path]
forkeyinproblem_path:
current_path=current_path+ (key,)
current_store[key] =mk_new_store(current_path)
current_store=current_store[key]
The text was updated successfully, but these errors were encountered:
Random Notes
Possibly related:
Old dict path get stuff
glom
remap
What we (might) want
Something like
collections.defaultdict
but where the factory needs to be contextual (because different levels may have different stores.What happens if I do:
and
s[1]
doesn't have the2
key?Maybe this:
and now non of the stores above are empty anymore.
Some code to think of
The text was updated successfully, but these errors were encountered: