CMS 3D CMS Logo

FileUtils.py
Go to the documentation of this file.
1 from __future__ import print_function
2 import os
3 import re
4 import pprint as pprint # for testing
5 
6 def loadListFromFile (filename):
7  """Loads a list of strings from file. Will append to given list
8  if asked."""
9  retval = []
10  filename = os.path.expanduser (filename)
11  if not os.path.exists (filename):
12  print("Error: file '%s' does not exist."%(filename))
13  raise RuntimeError("Bad filename")
14  source = open (filename, 'r')
15  for line in source.readlines():
16  line = re.sub (r'#.+$', '', line) # remove comment characters
17  line = line.strip()
18  if len (line):
19  retval.append (line)
20  source.close()
21  return retval
22 
23 
24 def sectionNofTotal (inputList, currentSection, numSections):
25  """Returns the appropriate sublist given the current section
26  (1..numSections)"""
27  currentSection -= 1 # internally, we want 0..N-1, not 1..N
28  size = len (inputList)
29  perSection = size // numSections
30  extra = size % numSections
31  start = perSection * currentSection
32  num = perSection
33  if currentSection < extra:
34  # the early sections get an extra item
35  start += currentSection
36  num += 1
37  else:
38  start += extra
39  stop = start + num
40  return inputList[ start:stop ]
41 
42 
43 ##############################################################################
44 ## ######################################################################## ##
45 ## ## ## ##
46 ## ######################################################################## ##
47 ##############################################################################
48 
49 
50 if __name__ == "__main__":
51  #############################################
52  ## Load and save command line history when ##
53  ## running interactively. ##
54  #############################################
55  import os, readline
56  import atexit
57  historyPath = os.path.expanduser("~/.pyhistory")
58 
59 
60  def save_history(historyPath=historyPath):
61  import readline
62  readline.write_history_file(historyPath)
63  if os.path.exists(historyPath):
64  readline.read_history_file(historyPath)
65 
66 
67  atexit.register(save_history)
68  readline.parse_and_bind("set show-all-if-ambiguous on")
69  readline.parse_and_bind("tab: complete")
70  if os.path.exists (historyPath) :
71  readline.read_history_file(historyPath)
72  readline.set_history_length(-1)
73 
74 
75  ############################
76  # Example code starts here #
77  ############################
78 
def save_history(historyPath=historyPath)
Definition: FileUtils.py:60
def sectionNofTotal(inputList, currentSection, numSections)
Definition: FileUtils.py:24
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def loadListFromFile(filename)
Definition: FileUtils.py:6