Guest User

Untitled

a guest
Jun 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. ##################################################
  2. #
  3. # chuli.py
  4. # chu li is chinese for processing
  5. #
  6. #
  7. ##################################################
  8.  
  9. import subprocess
  10. import os
  11. from put import lazyWrite
  12.  
  13. class chuli:
  14. """Performs chu li on data set"""
  15.  
  16. def __init__(self, downDir, upDir):
  17. """note that downDir and upDir should be in absolute"""
  18.  
  19. self.downDir = downDir
  20. self.upDir = upDir
  21.  
  22. self.downFinalDir = "downFinal"
  23.  
  24.  
  25.  
  26.  
  27. def run(self):
  28. """co-ordinate this class"""
  29.  
  30. # Note that we should do the walking here because
  31. # we can save time "walking"
  32.  
  33. self.act( self.downDir, self.downFinalDir)
  34.  
  35.  
  36. def act(self, fname, UorDFinalDir):
  37. """manages the walking, walk once only for all the functions"""
  38.  
  39. if os.path.isdir( fname):
  40. for root, dir, files, in os.walk(fname):
  41. for single_file in files:
  42. self._act( os.path.join(root, single_file), UorDFinalDir)
  43. else:
  44. self._act(fname, UorDFinalDir)
  45.  
  46.  
  47.  
  48. def _act(self, singlef, UorDFinalDir):
  49. """Workhorse of _act, add functions here if you want them to
  50. be applied to each individual file during os.walk"""
  51. self.sort(singlef)
  52.  
  53.  
  54.  
  55. def sort(self, singlef):
  56. """reads .in then sort to .sort and mv to .in again
  57. NOTE: singlef is a SINGLE FILE """
  58.  
  59. sortName = os.extsep.join( [os.path.splitext( singlef )[0], "sort"] )
  60. #print "sorting", singlef, sortName
  61.  
  62. ret = subprocess.call(["sort", "-u", singlef], stdout=open( sortName, 'w'))
  63. if ret != 0: raise Exception("sort calling on " + singlef + " returns non zero")
  64.  
  65. ret = subprocess.call(["mv", sortName, singlef])
  66.  
  67.  
  68.  
  69.  
  70. def toFinal(self, singlef, UorDFinalDir):
  71. """reads a file, output using lazyWrite
  72. NOTE THAT the first line of the final file is a \\n (Does that matter?)"""
  73.  
  74. writeAdr = self.getWriteDir( singlef, UorDFinalDir)
  75.  
  76. f = open(singlef, 'r')
  77. line = f.readline()
  78. last_link = None
  79.  
  80. while line:
  81. (link1, link2) = line.split(' ')
  82.  
  83. if last_link != link1: # a new link1
  84. #print "DEBUG", writeAdr, link1
  85. last_link = link1
  86. lazyWrite( writeAdr, '\n' + link1 + ':' + '\n')
  87.  
  88. lazyWrite( writeAdr, link2 ) #link2 has a \n at the end
  89. line = f.readline()
  90.  
  91. f.close()
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. if __name__ == "__main__":
  100. import sys
  101. if len(sys.argv) != 3:
  102. print "Usage: python chuli.py [downlinkDir] [uplinkDir]"
  103. exit()
  104.  
  105. downDir = os.path.abspath(sys.argv[1])
  106. upDir = os.path.abspath(sys.argv[2])
  107. myChuli = chuli( downDir, upDir)
  108. myChuli.run()
Add Comment
Please, Sign In to add comment