Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. class ColumnFileFactory: # Create factory class
  2.  
  3. def melt(self, headers, rows, seperators=[" ", "\n"], blank=False): # Create function to create a file by melting data
  4. if type(blank) == str: # If blank is a string
  5. for row in rows: # Loop through rows
  6. data_index = 0 # Create index counter of data
  7. for data in row: # Loop through data in row
  8. if type(data) == int: # If type of data is int
  9. if data == 0: # If data is 0
  10. row[data_index] = blank if type(blank) == str else "" # Replace it with blank
  11.  
  12. data_index += 1 # Count up data index counter
  13.  
  14. if len(rows) < 1: # If there isnt any rows
  15. return "Failed to create formatted file." # Return error
  16.  
  17. column = list(map(lambda row : 0, rows[0])) # Create some empty columns
  18. rows = [headers, []] + rows # Add headers to rows
  19.  
  20. pr_rows = [] # Create a list for print rows
  21. for row in rows: # Loop through all the rows
  22. pr_row = [] # Create an empty list for the new print row
  23. data_index = 0 # Current iteration count
  24. for data in row: # Loop through data in row
  25. data = str(data) # Cast data to string
  26. if len(column) > data_index: # Check if there is access for a column
  27. if len(data) > column[data_index]: # If length of data is more than the column padding length
  28. column[data_index] = len(data) # Set the column padding length to the data length
  29.  
  30. pr_row.append(data) # Put the new data into a print row
  31.  
  32. data_index += 1 # Count up for iteration
  33.  
  34. pr_rows.append(pr_row) # Add the print row to the print rows
  35.  
  36. new_data = [] # Create an empty array for the new data
  37. for row in pr_rows: # For every row in print rows
  38. data_index = 0 # Set data interation counter to 0
  39. data_row = [] # Create an empty array for new row
  40. for data in row:
  41. data = str(data) # Cast data to string
  42. data_row.append(data.ljust(column[data_index])) # Rewrite the row but using spacing
  43. data_index += 1 # Count up for iteration
  44.  
  45. new_data.append(str(seperators[0]).join(data_row)) # Add the row as print format with padding as given seperators
  46.  
  47. return str(seperators[1]).join(new_data) # Return the mixture
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement