Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os #to work with files and directorys
- import json #to work with JSON
- import csv #to work with CSV
- #base class that store all data
- class DataEntry(object):
- def __init__(self,date, time, sku, warehouse,
- warehouse_cell_id, operation,invoice,
- expiration_date,o peration_cost,comment):
- #def __init__(self, *args):
- self.date = date
- self.time = time
- self.sku = sku
- self.warehouse = warehouse
- self.warehouse_cell_id = warehouse_cell_id
- self.operation = operation
- self.invoice = invoice
- self.expiration_date = expiration_date
- self.operation_cost = operation_cost
- self.comment = comment
- def to_string(self):
- return f'date: {self.date}\t ' \
- f'time: {self.time}\t ' \
- f'sku: {self.sku}\t ' \
- f'warehouse: {self.warehouse}\t ' \
- f'warehouse_cell_id: {self.warehouse_cell_id}\t ' \
- f'operation: {self.operation}\t ' \
- f'invoice: {self.invoice}\t ' \
- f'expiration_date: {self.expiration_date}\t ' \
- f'operation_cost: {self.operation_cost} ' \
- f'comment: {self.comment}'
- class FileProcessor:
- def __init__(self, file_name):
- self.file = open(file_name)
- self.entrys = []
- def process_data(self):
- pass
- def show_entrys(self):
- for i in self.entrys:
- print(i.to_string())
- class JSONFileProcessor(FileProcessor):
- def process_data(self):
- self.data = json.load(self.file)['data']
- for line in self.data:
- # create entry, giving to it list with values
- entry = DataEntry(*list(line.values()))
- # adding entry to list of class
- self.entrys.append(entry)
- return self.entrys
- class CSVFileProcessor(FileProcessor):
- def process_data(self):
- self.data = csv.reader(self.file, delimiter=',', quotechar="\n")
- for line in self.data:
- # in first line are data titles, so we don't need it
- if self.data.line_num == 1:
- continue
- # create entry, giving to it list with values
- entry = DataEntry(*list(line))
- # adding entry to list of class
- self.entrys.append(entry)
- return self.entrys
- # look all files in the same directory where current python file is
- dir = os.getcwd()
- processed_files = []
- for i in os.listdir(dir):
- # if we found a dir just skip it
- if os.path.isdir(i): continue
- if i.endswith(".json"): # if we found a JSON file - process him
- j = JSONFileProcessor(i)
- j.process_data()
- processed_files.append(j)
- elif i.endswith(".csv"): # if we found a CSV file - process him
- c = CSVFileProcessor(i)
- c.process_data()
- processed_files.append(c)
- for i in processed_files:
- i.show_entrys()
- print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement