CMS 3D CMS Logo

FileUtils.py

Go to the documentation of this file.
00001 import os
00002 import re
00003 import pprint as pprint # for testing
00004 
00005 def loadListFromFile (filename):
00006     """Loads a list of strings from file.  Will append to given list
00007     if asked."""
00008     retval = []
00009     filename = os.path.expanduser (filename)
00010     if not os.path.exists (filename):
00011         print "Error: '%s' file does not exist."
00012         raise RuntimeError, "Bad filename"
00013     source = open (filename, 'r')        
00014     for line in source.readlines():
00015         line = re.sub (r'#.+$', '', line) # remove comment characters
00016         line = line.strip()
00017         if len (line):
00018             retval.append (line)
00019     source.close()
00020     return retval
00021 
00022 
00023 def sectionNofTotal (inputList, currentSection, numSections):
00024     """Returns the appropriate sublist given the current section
00025     (1..numSections)"""
00026     currentSection -= 1 # internally, we want 0..N-1, not 1..N
00027     size       = len (inputList)
00028     perSection = size // numSections
00029     extra      = size %  numSections
00030     start      = perSection * currentSection
00031     num        = perSection
00032     if currentSection < extra:
00033         # the early sections get an extra item
00034         start += currentSection
00035         num   += 1
00036     else:
00037         start += extra
00038     stop = start + num
00039     return inputList[ start:stop ]
00040 
00041 
00042 ##############################################################################
00043 ## ######################################################################## ##
00044 ## ##                                                                    ## ##
00045 ## ######################################################################## ##
00046 ##############################################################################
00047 
00048     
00049 if __name__ == "__main__":
00050     #############################################
00051     ## Load and save command line history when ##
00052     ## running interactively.                  ##
00053     #############################################
00054     import os, readline
00055     import atexit
00056     historyPath = os.path.expanduser("~/.pyhistory")
00057 
00058 
00059     def save_history(historyPath=historyPath):
00060         import readline
00061         readline.write_history_file(historyPath)
00062         if os.path.exists(historyPath):
00063             readline.read_history_file(historyPath)
00064 
00065 
00066     atexit.register(save_history)
00067     readline.parse_and_bind("set show-all-if-ambiguous on")
00068     readline.parse_and_bind("tab: complete")
00069     if os.path.exists (historyPath) :
00070         readline.read_history_file(historyPath)
00071         readline.set_history_length(-1)
00072 
00073 
00074     ############################
00075     # Example code starts here #
00076     ############################
00077 

Generated on Tue Jun 9 17:36:32 2009 for CMSSW by  doxygen 1.5.4