Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Utility functions for the module
- import re
- # Function for finding out if an object represents an integer
- def RepresentsInt(s):
- try:
- int(s)
- return True
- except ValueError:
- return False
- # prints the 2d array matrix upto given rows and cols
- def printMatrix(matrix, rows, cols):
- print("Printing matrix..")
- i = 0
- while (i < rows):
- j = 0
- while (j < cols):
- print(repr(matrix[i][j]) + "\t")
- j = j + 1
- print("")
- i = i + 1
- print("Print matrix complete.")
- # gets the previous non empty value in the same row
- # used to find out the col label from merged cells
- def getPrevNonEmpty(matrix, row, col):
- while (col > 0):
- # if cell is blank go to previous row
- if (matrix[row][col] == ""):
- col = col - 1
- else:
- # if its not blank return the current position
- return (row, col)
- # if col reaches 0 without returning anything then this row is totally blank
- return (-1, -1)
- def getRecursiveColLabel(matrix, i, j, single_rows, break_row):
- # if passed row is not a single valued row
- if (i >= single_rows):
- # print("i=" + repr(i) + " & j=" + repr(j))
- if (matrix[i][j] == ""):
- # if current cell is blank
- if (i >= break_row - 1):
- # if row is just beneath the break row, go to previous row to get the text
- col_label = getRecursiveColLabel(matrix, i - 1, j, single_rows, break_row).replace(" ", "_");
- else:
- # if not then get the previous non empty value in the same row
- r, c = getPrevNonEmpty(matrix, i, j-1)
- # if col doesn't reach zero
- if (not r == -1) and (not c == -1):
- # add the non empty value to col label and get the label from the row beneath the prev non empty row
- col_label = getRecursiveColLabel(matrix, r - 1, c, single_rows, break_row).replace(" ", "_") + "_" + matrix[r][c].strip().replace(" ", "_")
- else:
- # if col reached zero then just get the label from previous row
- col_label = getRecursiveColLabel(matrix, i - 1, j, single_rows, break_row).replace(" ", "_");
- else:
- # if current cell is not blank then the label is prev row's label + current cell content
- col_label = getRecursiveColLabel(matrix, i - 1, j, single_rows, break_row).replace(" ", "_") + "_" + str(matrix[i][j]).strip().replace(" ", "_")
- return re.sub(r'\W+', '', col_label) # remove all non-alpha numeric characters with and return the label
- else:
- return ""
- # removes all non alpha numeric chars from a string
- def removeNonAlNum(mystr):
- return re.sub(r'\W+', '', mystr)
- # returns state from filename, the first two characters
- def getFileState(filename):
- return filename[:2]
- # returns file name without extension
- def removeFileExt(filename):
- # find the index of last dot, whatever's after the dot will be the file extension
- index = filename.rfind(".")
- # return substring from the start upto the dot index
- return filename[:index].replace(" ", "_")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement