mirror of
https://github.com/wassname/ray.git
synced 2026-07-16 11:21:10 +08:00
[tune] Unflattened lookup for ProgressReporter (#9525)
Co-authored-by: Kai Fricke <kai@anyscale.com>
This commit is contained in:
@@ -2,7 +2,7 @@ import copy
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
from collections import defaultdict
|
||||
from collections import defaultdict, deque, Mapping, Sequence
|
||||
from threading import Thread
|
||||
|
||||
import numpy as np
|
||||
@@ -216,6 +216,27 @@ def flatten_dict(dt, delimiter="/"):
|
||||
return dt
|
||||
|
||||
|
||||
def unflattened_lookup(flat_key, lookup, delimiter="/", default=None):
|
||||
"""
|
||||
Unflatten `flat_key` and iteratively look up in `lookup`. E.g.
|
||||
`flat_key="a/0/b"` will try to return `lookup["a"][0]["b"]`.
|
||||
"""
|
||||
keys = deque(flat_key.split(delimiter))
|
||||
base = lookup
|
||||
while keys:
|
||||
key = keys.popleft()
|
||||
try:
|
||||
if isinstance(base, Mapping):
|
||||
base = base[key]
|
||||
elif isinstance(base, Sequence):
|
||||
base = base[int(key)]
|
||||
else:
|
||||
raise KeyError()
|
||||
except KeyError:
|
||||
return default
|
||||
return base
|
||||
|
||||
|
||||
def _to_pinnable(obj):
|
||||
"""Converts obj to a form that can be pinned in object store memory.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user