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

Method generate_from_json

py/generate.py:295–312  ·  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

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