CMS 3D CMS Logo

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