Skip to content

To string quote #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 31, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 66 additions & 16 deletions plotly/graph_objs/graph_objs.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,7 @@ def to_string(self, level=0, indent=4, eol='\n', pretty=True, max_chars=80):
obj_key = NAME_TO_KEY[self.__class__.__name__]
for key in INFO[obj_key]: # this sets the order of the keys! nice.
if key in self:
index += 1
string += "{eol}{indent}{key}=".format(
eol=eol,
indent=' ' * indent * (level+1),
Expand All @@ -819,16 +820,40 @@ def to_string(self, level=0, indent=4, eol='\n', pretty=True, max_chars=80):
pretty=pretty,
max_chars=max_chars)
except AttributeError:
val = repr(self[key])
val_chars = max_chars - (indent*(level+1)) - (len(key)+1)
if pretty and (len(val) > val_chars):
string += val[:val_chars - 5] + '...' + val[-1]
else:
string += val
if index < len(self) - 1:
if pretty: # curtail representation if too many chars
max_len = (max_chars -
indent*(level + 1) -
len(key + "=") -
len(eol))
if index < len(self):
max_len -= len(',') # remember the comma!
if isinstance(self[key], list):
s = "[]"
for iii, entry in enumerate(self[key], 1):
if iii < len(self[key]):
s_sub = graph_objs_tools.curtail_val_repr(
entry,
max_chars=max_len - len(s),
add_delim=True
)
else:
s_sub = graph_objs_tools.curtail_val_repr(
entry,
max_chars=max_len - len(s),
add_delim=False
)
s = s[:-1] + s_sub + s[-1]
if len(s) == max_len:
break
string += s
else:
string += graph_objs_tools.curtail_val_repr(
self[key], max_len)
else: # they want it all!
string += repr(self[key])
if index < len(self):
string += ","
index += 1
if index == len(self):
if index == len(self): # TODO: extraneous...
break
string += "{eol}{indent})".format(eol=eol, indent=' ' * indent * level)
return string
Expand Down Expand Up @@ -1341,16 +1366,41 @@ def to_string(self, level=0, indent=4, eol='\n', pretty=True, max_chars=80):
pretty=pretty,
max_chars=max_chars)
except AttributeError:
val = repr(self[key])
val_chars = max_chars - (indent*(level+1)) - (len(key)+1)
if pretty and (len(val) > val_chars):
string += val[:val_chars - 5] + '...' + val[-1]
else:
string += val
if pretty: # curtail representation if too many chars
max_len = (max_chars -
indent*(level + 1) -
len(key + "=") -
len(eol))
if index < len(self):
max_len -= len(',') # remember the comma!
if isinstance(self[key], list):
s = "[]"
for iii, entry in enumerate(self[key], 1):
if iii < len(self[key]):
s_sub = graph_objs_tools.curtail_val_repr(
entry,
max_chars=max_len - len(s),
add_delim=True
)
else:
s_sub = graph_objs_tools.curtail_val_repr(
entry,
max_chars=max_len - len(s),
add_delim=False
)
s = s[:-1] + s_sub + s[-1]
if len(s) == max_len:
break
string += s
else:
string += graph_objs_tools.curtail_val_repr(
self[key], max_len)
else: # they want it all!
string += repr(self[key])
if index < len(self) - 1:
string += ","
index += 1
if index == len(self):
if index == len(self): # TODO: extraneous...
break
left_over_keys = [key for key in self if key not in INFO[obj_key]]
left_over_keys.sort()
Expand Down
31 changes: 31 additions & 0 deletions plotly/graph_objs/graph_objs_tools.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import six

def update_keys(keys):
"""Change keys we used to support to their new equivalent."""
updated_keys = list()
Expand All @@ -12,3 +14,32 @@ def update_keys(keys):
scl="colorscale",
reversescl="reversescale"
)


def curtail_val_repr(val, max_chars, add_delim=False):
delim = ", "
end = ".."
if isinstance(val, six.string_types):
if max_chars <= len("'" + end + "'"):
return ' ' * max_chars
elif add_delim and max_chars <= len("'" + end + "'") + len(delim):
return "'" + end + "'" + ' ' * (max_chars - len("'" + end + "'"))
else:
if max_chars <= len(end):
return ' ' * max_chars
elif add_delim and max_chars <= len(end) + len(delim):
return end + ' ' * (max_chars - len(end))
if add_delim:
max_chars -= len(delim)
r = repr(val)
if len(r) > max_chars:
if isinstance(val, six.string_types):
# TODO: can we assume this ends in "'"
r = r[:max_chars - len(end + "'")] + end + "'"
elif isinstance(val, list) and max_chars >= len("[{end}]".format(end)):
r = r[:max_chars - len(end + ']')] + end + ']'
else:
r = r[:max_chars - len(end)] + end
if add_delim:
r += delim
return r