Return the dict element whose numeric key is closest to a target.
552327
def get_nearest_by_numeric_key(data: dict, key: int) -> Any:
    """"""
    return data.get(key, data[min(data.keys(), key=lambda k: abs(k - key))])
1148027
def get_member(row, key):
    '''  '''
    (target, tkey, tvalue) = dict_crawl(row, key)
    if target:
        return tvalue
    return None
600866
function closest (el, selector, scope) {
  scope = scope || document.documentElement

  // walk up the dom
  while (el && el !== scope) {
    if (matches(el, selector)) return el
    el = el.parentNode
  }

  // check scope for match
  return matches(el, selector) ? el : null
}
734285
function closest (el, sel) {
  if (!el) return
  if (matches(el, sel)) {
    return el
  } else {
    return closest(el.parentNode, sel)
  }
}
177789
def _get_target_nearest(self):
        """"""
        reps_query = """
             SELECT DISTINCT ON(g2.cartodb_id)
                 g1.cartodb_id As origin_id,
                 g2.the_geom,
                 g2.cartodb_id + {maxorigin} as cartodb_id,
                 g2.the_geom_webmercator
             FROM {origin_table} As g1, {target_table} As g2
             ORDER BY g2.cartodb_id, g1.the_geom <-> g2.the_geom
        """.format(
            maxorigin=self.origins.index.max(),
            origin_table=self.origin_table,
            target_table=self.target_table
        )

        nearest_reps = self.context.query(
            reps_query,
            decode_geom=True
        )

        nearest_reps = gpd.GeoDataFrame(nearest_reps, geometry='geometry')
        init_labels = nearest_reps['origin_id'].values

        # update with new information
        self.targets['labels'] = init_labels
        logging.info('nearest targets retrieved')

        return nearest_reps
770057
def _get_key_location(self, key) -> (int, int):
        
        key = int(key)
        if key == 0:
            return 1, 0
        remainder = key % self.chunkSize
        addend = ChunkedFileStore.firstChunkIndex
        chunk_no = key - remainder + addend if remainder \
            else key - self.chunkSize + addend
        offset = remainder or self.chunkSize
        return chunk_no, offset
476887
def nearest(self, idx):
        """"""
        hi = self.after(idx)
        lo = self.before(idx)
        if hi is None:
            return lo
        if lo is None:
            return hi
        if abs(hi - idx) < abs(lo - idx):
            return hi
        return lo
984923
def get_item_key(self, item):
        """"""
        try:
            ret = item[item['key']]
        except KeyError:
            logger.error("No 'key' available in {}".format(item))
        if isinstance(ret, list):
            return ret[0]
        else:
            return ret
1237085
function closestNumber(arr, num) {
  return arr.reduce((prev, curr) => (Math.abs(curr - num) < Math.abs(prev - num)) ? curr : prev);
}
1120605
def get_closest_readlength(estimated_readlength):
    # type: (int) -> int
    """"""

    readlengths = [36, 50, 75, 100]
    differences = [abs(r - estimated_readlength) for r in readlengths]
    min_difference = min(differences)
    index_of_min_difference = [i
                               for i, d in enumerate(differences)
                               if d == min_difference][0]

    return readlengths[index_of_min_difference]
1348706
private int getKeyIndex(int key) {
    reindexItems();

    int index = Arrays.binarySearch(sortedKeys, key);
    if (index >= 0) {
      int prevIndex = index - 1;
      while (prevIndex >= 0 && sortedKeys[prevIndex] == key) {
        index = prevIndex;
        prevIndex = index - 1;
      }
      return index;
    } else {
      return -1;
    }
  }
1342376
def _get_hit_nearest_ref_end(self, hits):
        ''''''
        nearest_to_end = hits[0]
        for hit in hits[1:]:
            if hit.ref_coords().end > nearest_to_end.ref_coords().end:
                nearest_to_end = hit
        return nearest_to_end
666126
public List<Map.Entry<K, Float>> nearest(K key)
    {
        return nearest(key, 10);
    }
1121839
async def status_by_coordinates(
            self, latitude: float, longitude: float) -> dict:
        """"""
        return await self.nearest_by_coordinates(latitude, longitude)
1089350
def min(self, key=None):
        
        if key is None:
            return self.reduce(min)
        return self.reduce(lambda a, b: min(a, b, key=key))
1152370
def _next_lowest_integer(group_keys):
    
    try: #TODO Replace with max default value when dropping compatibility with Python < 3.4
        largest_int= max([ int(val) for val in group_keys if _is_int(val)])
    except:
        largest_int= 0
    return largest_int + 1
1187538
def _lookup_element(lst, key):
    '''
    
    '''
    if not lst:
        return {}
    for ele in lst:
        if not ele or not isinstance(ele, dict):
            continue
        if ele.keys()[0] == key:
            return ele.values()[0]
    return {}