I have the following pattern in code which I use quite frequently:
def update_missing_content_type(cls):
items_missing_content_type = ItemMaster.objects.filter(content_type__isnull=True)
num_items = items_missing_content_type.count()
for num, item in enumerate(items_missing_content_type):
if num % 100 == 0:
log.info('>>> %s / %s updated...' % (num+1, num_items))
# do something
The enumerate
can be non-ideal though if the size of the Query is non-trivial. However, I still need to know the progress of the script (it might run for ten hours, etc.).
What would be a better pattern than the above to do something over a number of results while logging the general process of it?
Enumerate behaves as an iterator, and will produce the integer numberings on the fly. More details here: What is the implementation detail for enumerate? Enumerate should behave almost identically in performance as to just going over the indices of the iterable and looking up the item.
Presumably you need to have the index for logging and the item in #do something
, so we can time the two. Here are my results:
python -m timeit -s 'test=range(10)*1000' 'for i, elem in enumerate(test): pass' 1000 loops, best of 3: 370 usec per loop
python -m timeit -s 'test=range(10)*1000' 'for i in xrange(len(test)): elem=test[i]' 1000 loops, best of 3: 397 usec per loop
There seems to be no difference in speed between the two as expected in this use case. There is however a difference if you don't need the index: python -m timeit -s 'test=range(10)*1000' 'for elem in test: pass' 10000 loops, best of 3: 153 usec per loop
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加