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?