Package PyFoam :: Package Applications :: Module CommonReadWriteCSV
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Applications.CommonReadWriteCSV

  1  """ 
  2  Class that implements the common functionality for reading and writing CSV-files 
  3  """ 
  4   
  5  from optparse import OptionGroup 
  6   
  7  from os import path 
  8   
  9  from PyFoam.ThirdParty.six import print_ 
 10   
11 -class CommonReadWriteCSV(object):
12 """ The class implement common functionality 13 """ 14
15 - def addOptions(self):
16 calc=OptionGroup(self.parser, 17 "Calculations", 18 "Calculations to be performed on the data. Format is '<name>:::<expr>' (three colons should not appear in variable names). In the expressions there are variables that correspond to the column names. Also a variable 'data' that can be subscripted (for columns that are not valid variable names)") 19 calc.add_option("--recalc-columns", 20 action="append", 21 dest="recalcColumns", 22 default=[], 23 help="Columns that should be recalculated after reading. Can be specified more than once. In the expression a variable 'this' can reference the variable itself") 24 calc.add_option("--regular-expression-for-recalculation", 25 action="store_true", 26 dest="regularExpressionRecalc", 27 default=False, 28 help="The name in recalculations is a regular expression that must match the existing names") 29 calc.add_option("--raw-data-add-column", 30 action="append", 31 dest="rawAddColumns", 32 default=[], 33 help="Columns that should be added to the data after reading") 34 calc.add_option("--joined-data-add-column", 35 action="append", 36 dest="joinedAddColumns", 37 default=[], 38 help="Columns that should be added to the data before writing") 39 self.parser.add_option_group(calc) 40 41 info=OptionGroup(self.parser, 42 "Info", 43 "Information about the data") 44 info.add_option("--print-columns", 45 action="store_true", 46 dest="printColums", 47 default=False, 48 help="Print the column names found") 49 self.parser.add_option_group(info) 50 51 data=OptionGroup(self.parser, 52 "Data", 53 "Specification on the data that is read in") 54 self.parser.add_option_group(data) 55 data.add_option("--time-name", 56 action="store", 57 dest="time", 58 default=None, 59 help="Name of the time column") 60 data.add_option("--column-names", 61 action="append", 62 default=[], 63 dest="columns", 64 help="The columns (names) which should be copied to the CSV. All if unset") 65 data.add_option("--regexp-column-names", 66 action="store_true", 67 default=False, 68 dest="columnsRegexp", 69 help="The column names should be matched as regular expressions") 70 71 formt=OptionGroup(self.parser, 72 "Format", 73 "Specification on the format of the data read and written") 74 self.parser.add_option_group(formt) 75 formt.add_option("--write-excel-file", 76 action="store_true", 77 dest="writeExcel", 78 default=False, 79 help="Write to Excel-file instead of plain CSV. Onle works with the python-libraries pandas and xlwt") 80 formt.add_option("--read-excel-file", 81 action="store_true", 82 dest="readExcel", 83 default=False, 84 help="Read from Excel-file instead of plain CSV. Onle works with the python-libraries pandas and xlrd") 85 formt.add_option("--automatic-format", 86 action="store_true", 87 dest="automaticFormat", 88 default=False, 89 help="Determine from the file extension whether the files are CSV, Excel or plain text") 90 formt.add_option("--delimiter", 91 action="store", 92 dest="delimiter", 93 default=',', 94 help="Delimiter to be used between the values. Default: %default")
95 96
97 - def printColumns(self,fName,data):
98 if self.opts.printColums: 99 print_("Columns in",fName,":",", ".join(data.names()))
100
101 - def recalcColumns(self,data):
102 self.__processColumns(data,self.opts.recalcColumns)
103
104 - def rawAddColumns(self,data):
105 self.__processColumns(data,self.opts.rawAddColumns,create=True)
106
107 - def joinedAddColumns(self,data):
108 self.__processColumns(data,self.opts.joinedAddColumns,create=True)
109
110 - def __processColumns(self,data,specs,create=False):
111 for s in specs: 112 try: 113 name,expr=s.split(":::") 114 except ValueError: 115 self.error(s,"can not be split correctly with ':::':",s.split(":::")) 116 if not create and self.opts.regularExpressionRecalc: 117 import re 118 rex=re.compile(name) 119 for n in data.names(): 120 if rex.match(n): 121 data.recalcData(n,expr) 122 else: 123 data.recalcData(name,expr,create)
124
125 - def dataFormatOptions(self,name):
126 dataFormat="csv" 127 128 if self.opts.readExcel: 129 dataFormat="excel" 130 if self.opts.automaticFormat: 131 ext=path.splitext(name)[1] 132 if ext in [".csv"]: 133 dataFormat="csv" 134 elif ext in [".xls"]: 135 dataFormat="excel" 136 elif ext in [".txt",""]: 137 dataFormat="txt" 138 else: 139 dataFormat=ext[1:] 140 options={"csvName" : None, 141 "txtName" : None, 142 "excelName" : None} 143 if dataFormat=="csv": 144 options["csvName"]=name 145 elif dataFormat=="excel": 146 options["excelName"]=name 147 elif dataFormat=="txt": 148 options["txtName"]=name 149 else: 150 self.error("Unsupported format",dataFormat) 151 152 return options
153 154 # Should work with Python3 and Python2 155