Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c6e5b4d

Browse files
Try taking length of string-like objects
then if that fails, try and print the int, then if that fails, truely fail. This allows taking the length of unicode objects in Python2.
1 parent d6aee64 commit c6e5b4d

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

packages/python/plotly/plotly/basedatatypes.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ def _len_dict_item(item):
3434
know the length of the resulting string when printing we might need to
3535
convert to a string before calling len on it.
3636
"""
37-
if type(item) == type(str()):
38-
return len(item)
39-
elif type(item) == type(int()):
40-
return len("%d" % (item,))
41-
else:
42-
raise ValueError(
43-
"Cannot find string length of an item that is neither a string nor an integer."
44-
)
37+
try:
38+
l = len(item)
39+
except TypeError:
40+
try:
41+
l = len("%d" % (item,))
42+
except TypeError:
43+
raise ValueError(
44+
"Cannot find string length of an item that is not string-like nor an integer."
45+
)
46+
return l
4547

4648

4749
def _str_to_dict_path_full(key_path_str):

0 commit comments

Comments
 (0)