Advertisement
Lonely_Wanderer

hard solution

Feb 22nd, 2023
838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.97 KB | None | 0 0
  1. import os #to work with files and directorys
  2. import json #to work with JSON
  3. import csv #to work with CSV
  4.  
  5. #base class that store all data
  6. class DataEntry(object):
  7.     def __init__(self,date, time, sku, warehouse,
  8.                  warehouse_cell_id, operation,invoice,
  9.                  expiration_date,o  peration_cost,comment):
  10.     #def __init__(self, *args):
  11.         self.date = date
  12.         self.time = time
  13.         self.sku = sku
  14.         self.warehouse = warehouse
  15.         self.warehouse_cell_id = warehouse_cell_id
  16.         self.operation = operation
  17.         self.invoice = invoice
  18.         self.expiration_date = expiration_date
  19.         self.operation_cost = operation_cost
  20.         self.comment = comment
  21.  
  22.     def to_string(self):
  23.         return f'date: {self.date}\t ' \
  24.                f'time: {self.time}\t ' \
  25.                f'sku: {self.sku}\t ' \
  26.                f'warehouse: {self.warehouse}\t ' \
  27.                f'warehouse_cell_id: {self.warehouse_cell_id}\t ' \
  28.                f'operation: {self.operation}\t ' \
  29.                f'invoice: {self.invoice}\t ' \
  30.                f'expiration_date: {self.expiration_date}\t ' \
  31.                f'operation_cost: {self.operation_cost} ' \
  32.                f'comment: {self.comment}'
  33.  
  34.  
  35. class FileProcessor:
  36.     def __init__(self, file_name):
  37.         self.file = open(file_name)
  38.         self.entrys = []
  39.  
  40.     def process_data(self):
  41.         pass
  42.  
  43.     def show_entrys(self):
  44.         for i in self.entrys:
  45.             print(i.to_string())
  46.  
  47. class JSONFileProcessor(FileProcessor):
  48.     def process_data(self):
  49.         self.data = json.load(self.file)['data']
  50.         for line in self.data:
  51.             # create entry, giving to it list with values
  52.             entry = DataEntry(*list(line.values()))
  53.             # adding entry to list of class
  54.             self.entrys.append(entry)
  55.         return self.entrys
  56.  
  57. class CSVFileProcessor(FileProcessor):
  58.     def process_data(self):
  59.         self.data = csv.reader(self.file, delimiter=',', quotechar="\n")
  60.         for line in self.data:
  61.             # in first line are data titles, so we don't need it
  62.             if self.data.line_num == 1:
  63.                 continue
  64.             # create entry, giving to it list with values
  65.             entry = DataEntry(*list(line))
  66.             # adding entry to list of class
  67.             self.entrys.append(entry)
  68.         return self.entrys
  69.  
  70.  
  71.  
  72.  
  73. # look all files in the same directory where current python file is
  74. dir = os.getcwd()
  75. processed_files = []
  76. for i in os.listdir(dir):
  77.     # if we found a dir just skip it
  78.     if os.path.isdir(i): continue
  79.     if i.endswith(".json"): # if we found a JSON file - process him
  80.         j = JSONFileProcessor(i)
  81.         j.process_data()
  82.         processed_files.append(j)
  83.     elif i.endswith(".csv"): # if we found a CSV file - process him
  84.         c = CSVFileProcessor(i)
  85.         c.process_data()
  86.         processed_files.append(c)
  87.  
  88. for i in processed_files:
  89.     i.show_entrys()
  90.     print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement