Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:20

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