Every line of 'replace key in dictionary python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.
45 def replace_keys(original, mapping): 46 """Recursively replace any keys in original with their values in mappnig. 47 48 :param original: The dictionary to replace keys in. 49 :param mapping: A dict mapping keys to new keys. 50 51 :returns: Original with keys replaced via mapping. 52 """ 53 return original if not type(original) in (dict, list) else { 54 mapping.get(k, k): { 55 dict: lambda: replace_keys(v, mapping), 56 list: lambda: [replace_keys(vi, mapping) for vi in v] 57 }.get(type(v), lambda: v)() for k, v in original.iteritems()}
102 def _find_key(d, key): 103 if not d: 104 return None 105 for k, v in d.items()[:]: 106 if k == key: 107 return d 108 else: 109 result = _find_key(v, key) 110 if result is not None: 111 return result
5 def _find_dict_in_list(list_of_dict, key, val): 6 """Given a list of dictionaries, return the first dictionary that has the 7 given key:value pair. 8 """ 9 try: 10 for d in list_of_dict: 11 if (key, val) in d.items(): 12 return d 13 except TypeError: 14 pass
11 def dict_key_str(line): 12 13 keys = """bands bestEffort bias collection color connectedness eeObject eightConnected format gain gamma 14 geometry groupField groupName image iterations kernel labelBand leftField magnitude max maxDistance 15 maxOffset maxPixels maxSize minBucketWidth min name normalize opacity palette patchWidth 16 radius reducer referenceImage region rightField scale selectors shown sigma size source 17 strokeWidth threshold units visParams width""".split() 18 for key in keys: 19 if ":" in line and key in line: 20 line = line.replace(key + ":", "'" + key + "':") 21 return line
71 def replace_device_key(list_device: list, replace_key: dict): 72 """This method will iterate through the data and replace the data that matches against keys in the replace_keys 73 collection. 74 75 It will convert data like: 76 77 { 78 "field_144": { 79 "value": "00:0a:95:9d:68:16", 80 "name": "MAC Address" 81 } 82 } 83 84 to {"MAC_Address": "00:0a:95:9d:68:16"} 85 """ 86 for key, value in replace_key.items(): 87 for device in list_device: 88 if device.get(key, False): 89 if device.get(value) is not None: 90 number = 2 91 while device.get(f"{value}_{number}") is not None: 92 number += 1 93 value = f"{value}_{number}" 94 device[value] = device.pop(key) 95 return list_device
73 def dictRemove(dct, value): 74 try: 75 del dct[value] 76 except KeyError: 77 pass
15 def __setitem__(self, key, value): 16 if '-' in key: 17 key = key.replace('-', '_') 18 19 if isinstance(value, dict) and not isinstance(value, AttributeDict): 20 value = AttributeDict(value) 21 dict.__setitem__(self, key, value)
41 @register(txt_filters) 42 def dictreplace(env, code, app, cmdcfg, cfg): 43 env['replace_re'] = re.compile(cmdcfg[0]) 44 env['replace_to'] = cmdcfg[1] 45 code.append(' s = replace_to.format(**replace_re.match(s).groupdict())')
482 def replace(self, key, new_container): 483 dict.__setitem__(self, key, new_container)