Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def decode(self, scrap, **kwargs):
"""
Finds the register for the given encoder and translates the scrap's data
from a string or JSON type to an object of the encoder output type.
Parameters
----------
scrap: Scrap
A partially filled in scrap with data that needs decoding
"""
# Run validation on encoded data
scrap_to_payload(scrap)
loader = self._encoders.get(scrap.encoder)
if not loader:
raise ScrapbookMissingEncoder(
'No encoder found for "{}" encoder type!'.format(scrap.encoder)
)
return loader.decode(scrap, **kwargs)
Parameters
----------
scrap: Scrap
A partially filled in scrap with data that needs encoding
"""
encoder = self._encoders.get(scrap.encoder)
if not encoder:
raise ScrapbookMissingEncoder(
'No encoder found for "{data_type}" data type!'.format(
data_type=encoder
)
)
output_scrap = encoder.encode(scrap, **kwargs)
# Run validation on encoded data
scrap_to_payload(output_scrap)
return output_scrap
if name not in self.scraps:
if raise_on_missing:
raise ScrapbookException(
"Scrap '{}' is not available in this notebook.".format(name)
)
else:
ip_display(
"No scrap found with name '{}' in this notebook".format(name)
)
else:
scrap = self.scraps[name]
if new_name:
scrap = scrap._replace(name=new_name)
if scrap.data is not None:
data, metadata = _prepare_ipy_data_format(
scrap.name, scrap_to_payload(scrap), scrap.encoder
)
# Skip saving data for later regluing and remove 'scrapbook'
# from keys, when unattached
if unattached:
metadata = self._strip_scrapbook_metadata(metadata)
ip_display(data, metadata=metadata, raw=True)
if scrap.display is not None:
scrap_data = scrap.display.get("data", {})
scrap_metadata = self._strip_scrapbook_metadata(
scrap.display.get("metadata", {})
)
data, metadata = _prepare_ipy_display_format(
scrap.name, scrap_data, scrap_metadata
)
if unattached:
# Remove 'scrapbook' from keys if we want it unassociated