range(len(forbidden))
with enumerate(forbidden)
74
75 def validate(self, attrs):
76 forbidden = ['insert', 'update', 'delete', 'grant', 'show']
77 for i in range(len(forbidden)): 78 forbidden.append('({}'.format(forbidden[i]))
79 if any(map(lambda x: ' {} '.format(attrs['query'].lower()).find(' {} '.format(x)) != -1, forbidden)):
80 raise ValidationError({'query': 'forbidden query'})
Using range(len(...))
is not pythonic. Python does not have not index-based loops. Instead, it uses collection iterators.
Python has a built-in method enumerate
which adds a counter to an iterable.
Using this, you can access the counter and the value from the iterable at the same time.
It is therefore recommended to replace range(len(...))
with enumerate(...)
.
for index in range(len(mylist)):
...
for index, element in enumerate(mylist):
...