Skip to content

Commit

Permalink
Added figure to_ordered_dict method (#1167)
Browse files Browse the repository at this point in the history
Adds a new BaseFigure method, to_ordered_dict. This builds and returns a representation of the figure as a nested structure of OrderedDict and list instances. The OrderedDict keys are sorted alphabetically. This makes it possible for library users to iterate over the nested structure of a figure in a deterministic order.

This method takes the place of the Figure.get_ordered method in plotly.py < 3.0.0.

See #1158 for some discussion

* Added to_ordered_dict

* Use setitem rather than setattr syntax in graph object constructors

This has less indirection, and seems to work around a strange
nose testing bug with frames.
  • Loading branch information
jonmmease authored Sep 8, 2018
1 parent 7ddd1c9 commit e0e185e
Show file tree
Hide file tree
Showing 577 changed files with 6,276 additions and 5,525 deletions.
3 changes: 2 additions & 1 deletion codegen/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ def __init__(self""")
name_prop = subtype_node.name_property
buffer.write(f"""
_v = arg.pop('{name_prop}', None)
self.{name_prop} = {name_prop} if {name_prop} is not None else _v""")
self['{name_prop}'] = {name_prop} \
if {name_prop} is not None else _v""")

# ### Literals ###
if literal_nodes:
Expand Down
48 changes: 48 additions & 0 deletions plotly/basedatatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2084,6 +2084,54 @@ def to_plotly_json(self):
"""
return self.to_dict()

@staticmethod
def _to_ordered_dict(d, skip_uid=False):
"""
Static helper for converting dict or list to structure of ordered
dictionaries
"""
if isinstance(d, dict):
# d is a dict
result = collections.OrderedDict()
for key in sorted(d.keys()):
if skip_uid and key == 'uid':
continue
else:
result[key] = BaseFigure._to_ordered_dict(
d[key], skip_uid=skip_uid)

elif isinstance(d, list) and d and isinstance(d[0], dict):
# d is a list of dicts
result = [BaseFigure._to_ordered_dict(el, skip_uid=skip_uid)
for el in d]
else:
result = d

return result

def to_ordered_dict(self, skip_uid=True):

# Initialize resulting OrderedDict
# --------------------------------
result = collections.OrderedDict()

# Handle data
# -----------
result['data'] = BaseFigure._to_ordered_dict(self._data,
skip_uid=skip_uid)

# Handle layout
# -------------
result['layout'] = BaseFigure._to_ordered_dict(self._layout)

# Handle frames
# -------------
if self._frame_objs:
frames_props = [frame._props for frame in self._frame_objs]
result['frames'] = BaseFigure._to_ordered_dict(frames_props)

return result

# Static helpers
# --------------
@staticmethod
Expand Down
42 changes: 22 additions & 20 deletions plotly/graph_objs/_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,45 +744,47 @@ def __init__(
# Populate data dict with properties
# ----------------------------------
_v = arg.pop('customdata', None)
self.customdata = customdata if customdata is not None else _v
self['customdata'] = customdata if customdata is not None else _v
_v = arg.pop('customdatasrc', None)
self.customdatasrc = customdatasrc if customdatasrc is not None else _v
self['customdatasrc'
] = customdatasrc if customdatasrc is not None else _v
_v = arg.pop('hoverinfo', None)
self.hoverinfo = hoverinfo if hoverinfo is not None else _v
self['hoverinfo'] = hoverinfo if hoverinfo is not None else _v
_v = arg.pop('hoverinfosrc', None)
self.hoverinfosrc = hoverinfosrc if hoverinfosrc is not None else _v
self['hoverinfosrc'] = hoverinfosrc if hoverinfosrc is not None else _v
_v = arg.pop('hoverlabel', None)
self.hoverlabel = hoverlabel if hoverlabel is not None else _v
self['hoverlabel'] = hoverlabel if hoverlabel is not None else _v
_v = arg.pop('ids', None)
self.ids = ids if ids is not None else _v
self['ids'] = ids if ids is not None else _v
_v = arg.pop('idssrc', None)
self.idssrc = idssrc if idssrc is not None else _v
self['idssrc'] = idssrc if idssrc is not None else _v
_v = arg.pop('legendgroup', None)
self.legendgroup = legendgroup if legendgroup is not None else _v
self['legendgroup'] = legendgroup if legendgroup is not None else _v
_v = arg.pop('marker', None)
self.marker = marker if marker is not None else _v
self['marker'] = marker if marker is not None else _v
_v = arg.pop('name', None)
self.name = name if name is not None else _v
self['name'] = name if name is not None else _v
_v = arg.pop('opacity', None)
self.opacity = opacity if opacity is not None else _v
self['opacity'] = opacity if opacity is not None else _v
_v = arg.pop('r', None)
self.r = r if r is not None else _v
self['r'] = r if r is not None else _v
_v = arg.pop('rsrc', None)
self.rsrc = rsrc if rsrc is not None else _v
self['rsrc'] = rsrc if rsrc is not None else _v
_v = arg.pop('selectedpoints', None)
self.selectedpoints = selectedpoints if selectedpoints is not None else _v
self['selectedpoints'
] = selectedpoints if selectedpoints is not None else _v
_v = arg.pop('showlegend', None)
self.showlegend = showlegend if showlegend is not None else _v
self['showlegend'] = showlegend if showlegend is not None else _v
_v = arg.pop('stream', None)
self.stream = stream if stream is not None else _v
self['stream'] = stream if stream is not None else _v
_v = arg.pop('t', None)
self.t = t if t is not None else _v
self['t'] = t if t is not None else _v
_v = arg.pop('tsrc', None)
self.tsrc = tsrc if tsrc is not None else _v
self['tsrc'] = tsrc if tsrc is not None else _v
_v = arg.pop('uid', None)
self.uid = uid if uid is not None else _v
self['uid'] = uid if uid is not None else _v
_v = arg.pop('visible', None)
self.visible = visible if visible is not None else _v
self['visible'] = visible if visible is not None else _v

# Read-only literals
# ------------------
Expand Down
114 changes: 60 additions & 54 deletions plotly/graph_objs/_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2063,113 +2063,119 @@ def __init__(
# Populate data dict with properties
# ----------------------------------
_v = arg.pop('base', None)
self.base = base if base is not None else _v
self['base'] = base if base is not None else _v
_v = arg.pop('basesrc', None)
self.basesrc = basesrc if basesrc is not None else _v
self['basesrc'] = basesrc if basesrc is not None else _v
_v = arg.pop('cliponaxis', None)
self.cliponaxis = cliponaxis if cliponaxis is not None else _v
self['cliponaxis'] = cliponaxis if cliponaxis is not None else _v
_v = arg.pop('constraintext', None)
self.constraintext = constraintext if constraintext is not None else _v
self['constraintext'
] = constraintext if constraintext is not None else _v
_v = arg.pop('customdata', None)
self.customdata = customdata if customdata is not None else _v
self['customdata'] = customdata if customdata is not None else _v
_v = arg.pop('customdatasrc', None)
self.customdatasrc = customdatasrc if customdatasrc is not None else _v
self['customdatasrc'
] = customdatasrc if customdatasrc is not None else _v
_v = arg.pop('dx', None)
self.dx = dx if dx is not None else _v
self['dx'] = dx if dx is not None else _v
_v = arg.pop('dy', None)
self.dy = dy if dy is not None else _v
self['dy'] = dy if dy is not None else _v
_v = arg.pop('error_x', None)
self.error_x = error_x if error_x is not None else _v
self['error_x'] = error_x if error_x is not None else _v
_v = arg.pop('error_y', None)
self.error_y = error_y if error_y is not None else _v
self['error_y'] = error_y if error_y is not None else _v
_v = arg.pop('hoverinfo', None)
self.hoverinfo = hoverinfo if hoverinfo is not None else _v
self['hoverinfo'] = hoverinfo if hoverinfo is not None else _v
_v = arg.pop('hoverinfosrc', None)
self.hoverinfosrc = hoverinfosrc if hoverinfosrc is not None else _v
self['hoverinfosrc'] = hoverinfosrc if hoverinfosrc is not None else _v
_v = arg.pop('hoverlabel', None)
self.hoverlabel = hoverlabel if hoverlabel is not None else _v
self['hoverlabel'] = hoverlabel if hoverlabel is not None else _v
_v = arg.pop('hovertext', None)
self.hovertext = hovertext if hovertext is not None else _v
self['hovertext'] = hovertext if hovertext is not None else _v
_v = arg.pop('hovertextsrc', None)
self.hovertextsrc = hovertextsrc if hovertextsrc is not None else _v
self['hovertextsrc'] = hovertextsrc if hovertextsrc is not None else _v
_v = arg.pop('ids', None)
self.ids = ids if ids is not None else _v
self['ids'] = ids if ids is not None else _v
_v = arg.pop('idssrc', None)
self.idssrc = idssrc if idssrc is not None else _v
self['idssrc'] = idssrc if idssrc is not None else _v
_v = arg.pop('insidetextfont', None)
self.insidetextfont = insidetextfont if insidetextfont is not None else _v
self['insidetextfont'
] = insidetextfont if insidetextfont is not None else _v
_v = arg.pop('legendgroup', None)
self.legendgroup = legendgroup if legendgroup is not None else _v
self['legendgroup'] = legendgroup if legendgroup is not None else _v
_v = arg.pop('marker', None)
self.marker = marker if marker is not None else _v
self['marker'] = marker if marker is not None else _v
_v = arg.pop('name', None)
self.name = name if name is not None else _v
self['name'] = name if name is not None else _v
_v = arg.pop('offset', None)
self.offset = offset if offset is not None else _v
self['offset'] = offset if offset is not None else _v
_v = arg.pop('offsetsrc', None)
self.offsetsrc = offsetsrc if offsetsrc is not None else _v
self['offsetsrc'] = offsetsrc if offsetsrc is not None else _v
_v = arg.pop('opacity', None)
self.opacity = opacity if opacity is not None else _v
self['opacity'] = opacity if opacity is not None else _v
_v = arg.pop('orientation', None)
self.orientation = orientation if orientation is not None else _v
self['orientation'] = orientation if orientation is not None else _v
_v = arg.pop('outsidetextfont', None)
self.outsidetextfont = outsidetextfont if outsidetextfont is not None else _v
self['outsidetextfont'
] = outsidetextfont if outsidetextfont is not None else _v
_v = arg.pop('r', None)
self.r = r if r is not None else _v
self['r'] = r if r is not None else _v
_v = arg.pop('rsrc', None)
self.rsrc = rsrc if rsrc is not None else _v
self['rsrc'] = rsrc if rsrc is not None else _v
_v = arg.pop('selected', None)
self.selected = selected if selected is not None else _v
self['selected'] = selected if selected is not None else _v
_v = arg.pop('selectedpoints', None)
self.selectedpoints = selectedpoints if selectedpoints is not None else _v
self['selectedpoints'
] = selectedpoints if selectedpoints is not None else _v
_v = arg.pop('showlegend', None)
self.showlegend = showlegend if showlegend is not None else _v
self['showlegend'] = showlegend if showlegend is not None else _v
_v = arg.pop('stream', None)
self.stream = stream if stream is not None else _v
self['stream'] = stream if stream is not None else _v
_v = arg.pop('t', None)
self.t = t if t is not None else _v
self['t'] = t if t is not None else _v
_v = arg.pop('text', None)
self.text = text if text is not None else _v
self['text'] = text if text is not None else _v
_v = arg.pop('textfont', None)
self.textfont = textfont if textfont is not None else _v
self['textfont'] = textfont if textfont is not None else _v
_v = arg.pop('textposition', None)
self.textposition = textposition if textposition is not None else _v
self['textposition'] = textposition if textposition is not None else _v
_v = arg.pop('textpositionsrc', None)
self.textpositionsrc = textpositionsrc if textpositionsrc is not None else _v
self['textpositionsrc'
] = textpositionsrc if textpositionsrc is not None else _v
_v = arg.pop('textsrc', None)
self.textsrc = textsrc if textsrc is not None else _v
self['textsrc'] = textsrc if textsrc is not None else _v
_v = arg.pop('tsrc', None)
self.tsrc = tsrc if tsrc is not None else _v
self['tsrc'] = tsrc if tsrc is not None else _v
_v = arg.pop('uid', None)
self.uid = uid if uid is not None else _v
self['uid'] = uid if uid is not None else _v
_v = arg.pop('unselected', None)
self.unselected = unselected if unselected is not None else _v
self['unselected'] = unselected if unselected is not None else _v
_v = arg.pop('visible', None)
self.visible = visible if visible is not None else _v
self['visible'] = visible if visible is not None else _v
_v = arg.pop('width', None)
self.width = width if width is not None else _v
self['width'] = width if width is not None else _v
_v = arg.pop('widthsrc', None)
self.widthsrc = widthsrc if widthsrc is not None else _v
self['widthsrc'] = widthsrc if widthsrc is not None else _v
_v = arg.pop('x', None)
self.x = x if x is not None else _v
self['x'] = x if x is not None else _v
_v = arg.pop('x0', None)
self.x0 = x0 if x0 is not None else _v
self['x0'] = x0 if x0 is not None else _v
_v = arg.pop('xaxis', None)
self.xaxis = xaxis if xaxis is not None else _v
self['xaxis'] = xaxis if xaxis is not None else _v
_v = arg.pop('xcalendar', None)
self.xcalendar = xcalendar if xcalendar is not None else _v
self['xcalendar'] = xcalendar if xcalendar is not None else _v
_v = arg.pop('xsrc', None)
self.xsrc = xsrc if xsrc is not None else _v
self['xsrc'] = xsrc if xsrc is not None else _v
_v = arg.pop('y', None)
self.y = y if y is not None else _v
self['y'] = y if y is not None else _v
_v = arg.pop('y0', None)
self.y0 = y0 if y0 is not None else _v
self['y0'] = y0 if y0 is not None else _v
_v = arg.pop('yaxis', None)
self.yaxis = yaxis if yaxis is not None else _v
self['yaxis'] = yaxis if yaxis is not None else _v
_v = arg.pop('ycalendar', None)
self.ycalendar = ycalendar if ycalendar is not None else _v
self['ycalendar'] = ycalendar if ycalendar is not None else _v
_v = arg.pop('ysrc', None)
self.ysrc = ysrc if ysrc is not None else _v
self['ysrc'] = ysrc if ysrc is not None else _v

# Read-only literals
# ------------------
Expand Down
Loading

0 comments on commit e0e185e

Please sign in to comment.