def to_graph_objs(self, caller=True): """Change any nested collections to subclasses of PlotlyDict/List. Procedure: 1. Attempt to convert all entries to a subclass of PlotlyDict. 2. Call `to_graph_objects` on each of these entries. """ for index, entry in enumerate(self): if isinstance(entry, (PlotlyDict, PlotlyList)): if not isinstance(entry, NAME_TO_CLASS['Annotation']): raise exceptions.PlotlyListEntryError( obj=self, index=index, notes="The entry could not be converted into an " "Annotation object because it was already a " "different kind of graph object.", ) elif isinstance(entry, dict): obj = get_class_instance_by_name('Annotation') for k, v in list(entry.items()): obj[k] = v self[index] = obj else: raise exceptions.PlotlyListEntryError( obj=self, index=index, notes=( "The entry could not be converted into an Annotation " "object because it was not a dictionary." ), ) super(Annotations, self).to_graph_objs(caller=caller)
def _value_to_graph_object(self, index, value, _raise=True): """ Attempt to change the given value into a graph object. If _raise is False, this won't raise. If the entry can't be converted, `None` is returned, meaning the caller should ignore the value or discard it as a failed conversion. :param (dict) value: A dict to be converted into a graph object. :param (bool) _raise: If False, ignore bad values instead of raising. :return: (PlotlyBase|None) The graph object or possibly `None`. """ if not isinstance(value, dict): if _raise: path = self._get_path() + (index, ) raise exceptions.PlotlyListEntryError(self, path) else: return items = graph_reference.ARRAYS[self._name]['items'] for i, item in enumerate(items, 1): try: return GraphObjectFactory.create(item, _raise=_raise, _parent=self, _parent_key=index, **value) except exceptions.PlotlyGraphObjectError: if i == len(items) and _raise: raise
def __init__(self, *args, **kwargs): _raise = kwargs.get('_raise', True) if self._name is None: self.__dict__['_name'] = kwargs.pop('_name', None) self.__dict__['_parent'] = kwargs.get('_parent') self.__dict__['_parent_key'] = kwargs.get('_parent_key') if self._name is None: raise exceptions.PlotlyError( "PlotlyList is a base class. It's shouldn't be instantiated.") if args and isinstance(args[0], dict): note = ("Just like a `list`, `{name}` must be instantiated " "with a *single* collection.\n" "In other words these are OK:\n" ">>> {name}()\n" ">>> {name}([])\n" ">>> {name}([dict()])\n" ">>> {name}([dict(), dict()])\n" "However, these don't make sense:\n" ">>> {name}(dict())\n" ">>> {name}(dict(), dict())".format( name=self._get_class_name())) raise exceptions.PlotlyListEntryError(self, [0], notes=[note]) super(PlotlyList, self).__init__() for index, value in enumerate(list(*args)): value = self._value_to_graph_object(index, value, _raise=_raise) if isinstance(value, PlotlyBase): self.append(value)
def to_graph_objs(self, caller=True): # TODO TODO TODO! check logic! """Change any nested collections to subclasses of PlotlyDict/List. Procedure: 1. Attempt to convert all entries to a subclass of PlotlyTrace. 2. Call `to_graph_objects` on each of these entries. """ for index, entry in enumerate(self): if isinstance(entry, PlotlyDict): self[index] = get_class_instance_by_name( entry.__class__.__name__, entry) elif isinstance(entry, dict): if 'type' not in entry: # assume 'scatter' if not given entry['type'] = 'scatter' try: obj_name = KEY_TO_NAME[entry['type']] except KeyError: raise exceptions.PlotlyDataTypeError(obj=self, index=index) obj = get_class_instance_by_name(obj_name) for k, v in list(entry.items()): obj[k] = v self[index] = obj if not isinstance(self[index], PlotlyTrace): # Trace ONLY!!! raise exceptions.PlotlyListEntryError( obj=self, index=index, notes=( "The entry could not be converted into a PlotlyTrace " "object (e.g., Scatter, Heatmap, Bar, etc)."), ) super(Data, self).to_graph_objs(caller=caller)
def _value_to_graph_object(self, index, value, _raise=True): if not isinstance(value, dict): if _raise: notes = ['Entry should subclass dict.'] path = self._get_path() + (index, ) raise exceptions.PlotlyListEntryError(self, path, notes=notes) else: return item = value.get('type', 'scatter') if item not in graph_reference.ARRAYS['data']['items']: if _raise: path = self._get_path() + (0, ) raise exceptions.PlotlyDataTypeError(self, path) return GraphObjectFactory.create(item, _raise=_raise, _parent=self, _parent_key=index, **value)
def to_graph_objs(self, caller=True): """Change any nested collections to subclasses of PlotlyDict/List. Procedure: 1. Attempt to convert all entries to a subclass of PlotlyDict. 2. Call `to_graph_objects` on each of these entries. """ for index, entry in enumerate(self): if isinstance(entry, PlotlyDict): try: entry.to_graph_objs(caller=False) except exceptions.PlotlyGraphObjectError as err: err.add_to_error_path(index) err.prepare() raise # re-raise current exception else: raise exceptions.PlotlyListEntryError(obj=self, index=index, entry=entry)
def __init__(self, *args): super(PlotlyList, self).__init__(*args) if args and isinstance(args[0], dict): raise exceptions.PlotlyListEntryError( obj=self, index=0, notes="Just like a `list`, `{name}` must be instantiated with " "a *single* collection.\n" "In other words these are OK:\n" ">>> {name}()\n" ">>> {name}([])\n" ">>> {name}([dict()])\n" ">>> {name}([dict(), dict()])\n" "However, these don't make sense:\n" ">>> {name}(dict())\n" ">>> {name}(dict(), dict())" "".format(name=self.__class__.__name__)) self.validate() if self.__class__.__name__ == 'PlotlyList': warnings.warn("\nThe PlotlyList class is a base class of " "list-like graph_objs.\nIt is not meant to be a " "user interface.")