MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / generate_from_json

Method generate_from_json

py/generate.py:294–311  ·  view source on GitHub ↗

Generate the code that creates an instance from a JSON dict named `dict_`.

(self, dict_)

Source from the content-addressed store, hash-verified

292 return code
293
294 def generate_from_json(self, dict_):
295 """Generate the code that creates an instance from a JSON dict named `dict_`."""
296 if self.items:
297 if self.items.ref:
298 py_ref = ref_to_python(self.items.ref)
299 expr = f"[{py_ref}.from_json(i) for i in {dict_}['{self.name}']]"
300 else:
301 cons = CdpPrimitiveType.get_constructor(self.items.type, "i")
302 expr = f"[{cons} for i in {dict_}['{self.name}']]"
303 else:
304 if self.ref:
305 py_ref = ref_to_python(self.ref)
306 expr = f"{py_ref}.from_json({dict_}['{self.name}'])"
307 else:
308 expr = CdpPrimitiveType.get_constructor(self.type, f"{dict_}['{self.name}']")
309 if self.optional:
310 expr = f"{expr} if '{self.name}' in {dict_} else None"
311 return expr
312
313
314@dataclass

Callers 4

generate_class_codeMethod · 0.45
generate_from_jsonMethod · 0.45
generate_returnMethod · 0.45
generate_codeMethod · 0.45

Calls 2

ref_to_pythonFunction · 0.85
get_constructorMethod · 0.80

Tested by

no test coverage detected