@@ -1752,7 +1752,17 @@ def get_index_from_name(name):
17521752 except ValueError :
17531753 raise IndexError (
17541754 'Compound model {0} does not have a component named '
1755- '{1}' .format (cls .name , index ))
1755+ '{1}' .format (cls .name , name ))
1756+
1757+ def check_for_negative_index (index ):
1758+ if index < 0 :
1759+ ind = len (cls .submodel_names ) + index
1760+ if ind < 0 :
1761+ # If still < 0 then this is an invalid index
1762+ raise IndexError ("Model index {0} out of range." .format (index ))
1763+ else :
1764+ return ind
1765+ return index
17561766
17571767 if isinstance (index , six .string_types ):
17581768 return get_index_from_name (index )
@@ -1766,11 +1776,14 @@ def get_index_from_name(name):
17661776 start = index .start if index .start is not None else 0
17671777 stop = (index .stop
17681778 if index .stop is not None else len (cls .submodel_names ))
1779+ if isinstance (start , int ):
1780+ start = check_for_negative_index (start )
1781+ if isinstance (stop , int ):
1782+ stop = check_for_negative_index (stop )
17691783 if isinstance (start , six .string_types ):
1770- start = cls . submodel_names . index (start )
1784+ start = get_index_from_name (start )
17711785 if isinstance (stop , six .string_types ):
1772- stop = cls .submodel_names .index (stop ) + 1
1773-
1786+ stop = get_index_from_name (stop ) + 1
17741787 length = stop - start
17751788
17761789 if length == 1 :
@@ -1782,7 +1795,6 @@ def get_index_from_name(name):
17821795 elif isinstance (index , int ):
17831796 if index < 0 :
17841797 index = len (cls .submodel_names ) + index
1785-
17861798 if index < 0 or index >= len (cls .submodel_names ):
17871799 # If still < 0 then this is an invalid index
17881800 raise IndexError ("Model index out of range." )
0 commit comments