Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-12-01 23:40:21

0001 import os
0002 import re
0003 import pprint as pprint # for testing
0004 
0005 def loadListFromFile (filename):
0006     """Loads a list of strings from file.  Will append to given list
0007     if asked."""
0008     retval = []
0009     filename = os.path.expanduser (filename)
0010     if not os.path.exists (filename):
0011         print("Error: file '%s' does not exist."%(filename))
0012         raise RuntimeError("Bad filename")
0013     source = open (filename, 'r')        
0014     for line in source.readlines():
0015         line = re.sub (r'#.+$', '', line) # remove comment characters
0016         line = line.strip()
0017         if len (line):
0018             retval.append (line)
0019     source.close()
0020     return retval
0021 
0022 
0023 def sectionNofTotal (inputList, currentSection, numSections):
0024     """Returns the appropriate sublist given the current section
0025     (1..numSections)"""
0026     currentSection -= 1 # internally, we want 0..N-1, not 1..N
0027     size       = len (inputList)
0028     perSection = size // numSections
0029     extra      = size %  numSections
0030     start      = perSection * currentSection
0031     num        = perSection
0032     if currentSection < extra:
0033         # the early sections get an extra item
0034         start += currentSection
0035         num   += 1
0036     else:
0037         start += extra
0038     stop = start + num
0039     return inputList[ start:stop ]
0040 
0041 
0042 ##############################################################################
0043 ## ######################################################################## ##
0044 ## ##                                                                    ## ##
0045 ## ######################################################################## ##
0046 ##############################################################################
0047 
0048     
0049 if __name__ == "__main__":
0050     #############################################
0051     ## Load and save command line history when ##
0052     ## running interactively.                  ##
0053     #############################################
0054     import os, readline
0055     import atexit
0056     historyPath = os.path.expanduser("~/.pyhistory")
0057 
0058 
0059     def save_history(historyPath=historyPath):
0060         import readline
0061         readline.write_history_file(historyPath)
0062         if os.path.exists(historyPath):
0063             readline.read_history_file(historyPath)
0064 
0065 
0066     atexit.register(save_history)
0067     readline.parse_and_bind("set show-all-if-ambiguous on")
0068     readline.parse_and_bind("tab: complete")
0069     if os.path.exists (historyPath) :
0070         readline.read_history_file(historyPath)
0071         readline.set_history_length(-1)
0072 
0073 
0074     ############################
0075     # Example code starts here #
0076     ############################
0077