MCPcopy Create free account
hub / github.com/Materials-Consortia/optimade-python-tools / convert

Method convert

optimade/adapters/base.py:79–107  ·  view source on GitHub ↗

Convert OPTIMADE entry to desired format. Parameters: format (str): Type or format to which the entry should be converted. Raises: AttributeError: If `format` can not be found in `_type_converters` or `_common_converters`. Returns: The c

(self, format: str)

Source from the content-addressed store, hash-verified

77 return self._entry
78
79 def convert(self, format: str) -> Any:
80 """Convert OPTIMADE entry to desired format.
81
82 Parameters:
83 format (str): Type or format to which the entry should be converted.
84
85 Raises:
86 AttributeError: If `format` can not be found in `_type_converters` or `_common_converters`.
87
88 Returns:
89 The converted entry according to the desired format or type.
90
91 """
92 if (
93 format not in self._type_converters
94 and format not in self._common_converters
95 ):
96 raise AttributeError(
97 f"Non-valid entry type to convert to: {format}\nValid entry types: "
98 f"{tuple(self._type_converters.keys()) + tuple(self._common_converters.keys())}"
99 )
100
101 if self._converted.get(format, None) is None:
102 if format in self._type_converters:
103 self._converted[format] = self._type_converters[format](self.entry)
104 else:
105 self._converted[format] = self._common_converters[format]()
106
107 return self._converted[format]
108
109 @classmethod
110 def ingest_from(cls, data: Any, format: str | None = None) -> Any:

Callers 8

__getattr__Method · 0.95
test_convertFunction · 0.80
test_two_way_conversionFunction · 0.80
test_convertFunction · 0.80

Calls 1

getMethod · 0.45

Tested by 7

test_convertFunction · 0.64
test_two_way_conversionFunction · 0.64
test_convertFunction · 0.64