Batch data processing example. Two methods included, generator and cmdlet.


SUBMITTED BY: antfuentes87

DATE: Nov. 25, 2015, 12:38 a.m.

FORMAT: Text only

SIZE: 1.0 kB

HITS: 689

  1. #!python
  2. # coding: utf-8
  3. # --------------------------------------------
  4. # Make data for test.
  5. with file('g:\\data.txt', 'w') as fd:
  6. for i in range(1000):
  7. fd.write('%d\n' % i)
  8. fd.close()
  9. # --------------------------------------------
  10. # Use generator for batch processing
  11. def get_data(filename, num_of_data):
  12. """Gather data for batch processing."""
  13. data_collection = []
  14. for i,ln in enumerate(file(filename, 'r'), 1):
  15. data_collection.append(ln.strip())
  16. if i % num_of_data == 0:
  17. yield data_collection
  18. data_collection = []
  19. for data_50 in get_data('g:\\data.txt', 50):
  20. # process data here.
  21. data_sum = sum(map(int, data_50))
  22. print data_sum
  23. # --------------------------------------------
  24. # Use cmdlet package to handle same task.
  25. from cmdlet.cmds import *
  26. for data_50 in (readline('g:\\data.txt') | pack(50)):
  27. # process data here.
  28. data_sum = sum(map(int, data_50))
  29. print data_sum

comments powered by Disqus