Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __setitem__(self, key, value):
if isinstance(value, six.string_types) or not (
getattr(value, "encode", None) and callable(value.encode)
):
raise ScrapbookException("Can't register object without 'encode' method.")
if isinstance(value, six.string_types) or not (
getattr(value, "decode", None) and callable(value.decode)
):
raise ScrapbookException("Can't register object without 'decode' method.")
return self._encoders.__setitem__(key, value)
def __setitem__(self, key, value):
if not (getattr(value, "translate", None) and callable(value.translate)):
raise ScrapbookException(
"Can't register object without 'translate' method."
)
if not (getattr(value, "load", None) and callable(value.translate)):
raise ScrapbookException("Can't register object without 'load' method.")
return self._translators.__setitem__(key, value)
def __setitem__(self, key, value):
if isinstance(value, six.string_types) or not (
getattr(value, "encode", None) and callable(value.encode)
):
raise ScrapbookException("Can't register object without 'encode' method.")
if isinstance(value, six.string_types) or not (
getattr(value, "decode", None) and callable(value.decode)
):
raise ScrapbookException("Can't register object without 'decode' method.")
return self._encoders.__setitem__(key, value)
----------
name : str
name of scrap object
new_name : str
replacement name for scrap
raise_error : bool
indicator for if the resketch should print a message or error on missing snaps
unattached : bool
indicator for rendering without making the display recallable as scrapbook data
"""
# Avoid circular imports
from .api import _prepare_ipy_data_format, _prepare_ipy_display_format
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
def __setitem__(self, key, value):
if not (getattr(value, "translate", None) and callable(value.translate)):
raise ScrapbookException(
"Can't register object without 'translate' method."
)
if not (getattr(value, "load", None) and callable(value.translate)):
raise ScrapbookException("Can't register object without 'load' method.")
return self._translators.__setitem__(key, value)
def translate_data(self, storage_type, scrap):
"""
Finds the register for the given storage_type and translates the scrap into
an object of the translator output type.
Parameters
----------
storage_type: str
Name of the mime subtype parsed by the translator.
scrap: obj
Object to be converted to JSON or string format for storage in an output
"""
translator = self._translators.get(storage_type)
if not translator:
raise ScrapbookException(
'No translator found for "{}" data type!'.format(storage_type)
)
return translator.translate(scrap)
# -*- coding: utf-8 -*-
class ScrapbookException(ValueError):
"""Raised when an exception is encountered when operating on a notebook."""
class ScrapbookMissingEncoder(ScrapbookException):
"""Raised when no encoder is found to tranforming data"""
class ScrapbookDataException(ScrapbookException):
"""Raised when a data translation exception is encountered"""
def __init__(self, message, data_errors=None):
super(ScrapbookDataException, self).__init__(message)
self.data_errors = data_errors
# -*- coding: utf-8 -*-
class ScrapbookException(ValueError):
"""Raised when an exception is encountered when operating on a notebook."""
class ScrapbookMissingEncoder(ScrapbookException):
"""Raised when no encoder is found to tranforming data"""
class ScrapbookDataException(ScrapbookException):
"""Raised when a data translation exception is encountered"""
def __init__(self, message, data_errors=None):
super(ScrapbookDataException, self).__init__(message)
self.data_errors = data_errors
def load_data(self, storage_type, scrap):
"""
Finds the register for the given storage_type and loads the scrap into
a JSON or string object.
Parameters
----------
storage_type: str
Name of the mime subtype parsed by the translator.
scrap: obj
Object to be converted from JSON or string format to the original value.
"""
loader = self._translators.get(storage_type)
if not loader:
raise ScrapbookException(
'No translator found for "{}" data type!'.format(storage_type)
)
return loader.load(scrap)