Skip to content

Commit 334b7fd

Browse files
update index data check, support all object with index method
1 parent 016eefc commit 334b7fd

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

pysenal/utils/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,10 @@ def index(l, val, default=-1):
106106
:param default: default value to return that value not in list
107107
:return: value index in list
108108
"""
109-
if type(l) not in {list, tuple}:
110-
raise TypeError('value is not in index')
109+
try:
110+
getattr(l, 'index')
111+
except:
112+
raise TypeError('ipnut data doesn\'t support index')
111113
if val not in l:
112114
return default
113115
else:

tests/utils/test_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ def test_index():
106106
with pytest.raises(TypeError):
107107
index(10, 10)
108108

109+
assert index('This is an example', 'example') == 11
110+
assert index(('a', 'b', 'c'), 'b') == 1
111+
109112

110113
def test_json_serialize():
111114
assert json_serialize(Decimal('3.1415926')) == '3.1415926'

0 commit comments

Comments
 (0)