def iter_devices_partitions(devices_dir, item_type):
 devices = listdir(devices_dir)
 shuffle(devices)
 devices_partitions = []
 for device in devices:
  partitions = listdir(os.path.join(devices_dir, device, item_type))
  shuffle(partitions)
  devices_partitions.append((device, iter(partitions)))
 yielded = True
 while yielded:
  yielded = False
  for (device, partitions) in devices_partitions:
   try:
    (yield (device, partitions.next()))
    yielded = True
   except StopIteration:
    pass

def iter_devices_partitions ( devices_dir , item_type ) : devices = listdir ( devices_dir ) shuffle ( devices ) devices_partitions = [ ] for device in devices : partitions = listdir ( os . path . join ( devices_dir , device , item_type ) ) shuffle ( partitions ) devices_partitions . append ( ( device , iter ( partitions ) ) ) yielded = True while yielded : yielded = False for ( device , partitions ) in devices_partitions : try : ( yield ( device , partitions . next ( ) ) ) yielded = True except StopIteration : pass
iterate over partitions across all devices .