Advertisement
a1ananth

utilities.py

Aug 11th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.24 KB | None | 0 0
  1. # Utility functions for the module
  2.  
  3. import re
  4.  
  5. # Function for finding out if an object represents an integer
  6. def RepresentsInt(s):
  7.     try:
  8.         int(s)
  9.         return True
  10.     except ValueError:
  11.         return False
  12.  
  13. # prints the 2d array matrix upto given rows and cols
  14. def printMatrix(matrix, rows, cols):
  15.     print("Printing matrix..")
  16.     i = 0
  17.     while (i < rows):
  18.         j = 0
  19.  
  20.         while (j < cols):
  21.             print(repr(matrix[i][j]) + "\t")
  22.             j = j + 1
  23.  
  24.         print("")
  25.         i = i + 1
  26.  
  27.     print("Print matrix complete.")
  28.  
  29. # gets the previous non empty value in the same row
  30. # used to find out the col label from merged cells
  31. def getPrevNonEmpty(matrix, row, col):
  32.     while (col > 0):
  33.         # if cell is blank go to previous row
  34.         if (matrix[row][col] == ""):
  35.             col = col - 1
  36.         else:
  37.             # if its not blank return the current position
  38.             return (row, col)
  39.  
  40.     # if col reaches 0 without returning anything then this row is totally blank
  41.     return (-1, -1)
  42.  
  43. def getRecursiveColLabel(matrix, i, j, single_rows, break_row):
  44.     # if passed row is not a single valued row
  45.     if (i >= single_rows):
  46.         # print("i=" + repr(i) + " & j=" + repr(j))
  47.         if (matrix[i][j] == ""):
  48.             # if current cell is blank
  49.             if (i >= break_row - 1):
  50.                 # if row is just beneath the break row, go to previous row to get the text
  51.                 col_label = getRecursiveColLabel(matrix, i - 1, j, single_rows, break_row).replace(" ", "_");
  52.             else:
  53.                 # if not then get the previous non empty value in the same row
  54.                 r, c = getPrevNonEmpty(matrix, i, j-1)
  55.                 # if col doesn't reach zero
  56.                 if (not r == -1) and (not c == -1):
  57.                     # add the non empty value to col label and get the label from the row beneath the prev non empty row
  58.                     col_label = getRecursiveColLabel(matrix, r - 1, c, single_rows, break_row).replace(" ", "_") + "_" + matrix[r][c].strip().replace(" ", "_")
  59.                 else:
  60.                     # if col reached zero then just get the label from previous row
  61.                     col_label = getRecursiveColLabel(matrix, i - 1, j, single_rows, break_row).replace(" ", "_");
  62.            
  63.         else:
  64.             # if current cell is not blank then the label is prev row's label + current cell content
  65.             col_label = getRecursiveColLabel(matrix, i - 1, j, single_rows, break_row).replace(" ", "_") + "_" + str(matrix[i][j]).strip().replace(" ", "_")
  66.            
  67.         return re.sub(r'\W+', '', col_label) # remove all non-alpha numeric characters with and return the label
  68.     else:
  69.         return ""
  70.  
  71. # removes all non alpha numeric chars from a string
  72. def removeNonAlNum(mystr):
  73.     return re.sub(r'\W+', '', mystr)
  74.  
  75. # returns state from filename, the first two characters
  76. def getFileState(filename):
  77.     return filename[:2]
  78.  
  79. # returns file name without extension
  80. def removeFileExt(filename):
  81.     # find the index of last dot, whatever's after the dot will be the file extension
  82.     index = filename.rfind(".")
  83.     # return substring from the start upto the dot index
  84.     return filename[:index].replace(" ", "_")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement