SelwynChan / HelloWorld

My first Git Repository
0 stars 0 forks source link

Mapping #10

Open SelwynChan opened 1 year ago

SelwynChan commented 1 year ago

To add a list of logicalOrderIDs for which there was no receivedOrderID found, and use a tkinter progress bar, you can modify the function as follows:

import os
import csv
import tkinter as tk
from tkinter import ttk

def map_logical_to_received_order_ids(directory, progress_var):
    mapping = {}
    logical_order_ids_without_received = []

    csv_files = [f for f in os.listdir(directory) if f.endswith(".csv")]
    total_files = len(csv_files)

    for idx, filename in enumerate(csv_files):
        file_path = os.path.join(directory, filename)

        with open(file_path, 'r') as csvfile:
            reader = csv.reader(csvfile, delimiter=chr(31))
            headers = next(reader)

            logical_order_idx = headers.index('logicalOrderID')
            received_order_idx = headers.index('receivedOrderID')

            for row in reader:
                logical_order_id = row[logical_order_idx]
                received_order_id = row[received_order_idx]

                if received_order_id:
                    mapping[logical_order_id] = received_order_id
                else:
                    logical_order_ids_without_received.append(logical_order_id)

        progress_var.set((idx + 1) / total_files * 100)

    return mapping, logical_order_ids_without_received

def run_function_with_progress_bar():
    directory = "path/to/csv/files"

    progress_var = tk.DoubleVar()
    progress_bar = ttk.Progressbar(root, variable=progress_var, maximum=100)
    progress_bar.pack(pady=10)

    result, missing_received_order_ids = map_logical_to_received_order_ids(directory, progress_var)

    print("Mapping:", result)
    print("Logical Order IDs without Received Order ID:", missing_received_order_ids)

root = tk.Tk()
root.geometry("300x100")
root.title("Processing CSV Files")

process_button = ttk.Button(root, text="Process CSV Files", command=run_function_with_progress_bar)
process_button.pack(pady=10)

root.mainloop()

This modified function now initializes an empty list logical_order_ids_without_received to store the logicalOrderIDs without corresponding receivedOrderIDs. It also takes an additional argument progress_var to update the progress bar.

The function now returns a tuple containing the mapping and the list of logicalOrderIDs without a receivedOrderID.

In the main part of the code, a tkinter progress bar is added. The run_function_with_progress_bar function is called when the user clicks the "Process CSV Files" button. This function sets up the progress bar and calls the map_logical_to_received_order_ids function with the specified directory and progress bar variable.

The progress bar is updated based on the number of processed files out of the total number of CSV files in the directory.

SelwynChan commented 1 year ago

Sure, here's the modified function that prints the progress to the console:

import os
import csv

def map_logical_to_received_order_ids(directory):
    mapping = {}
    logical_order_ids_without_received = []

    csv_files = [f for f in os.listdir(directory) if f.endswith(".csv")]
    total_files = len(csv_files)

    for idx, filename in enumerate(csv_files):
        file_path = os.path.join(directory, filename)

        with open(file_path, 'r') as csvfile:
            reader = csv.reader(csvfile, delimiter=chr(31))
            headers = next(reader)

            logical_order_idx = headers.index('logicalOrderID')
            received_order_idx = headers.index('receivedOrderID')

            for row in reader:
                logical_order_id = row[logical_order_idx]
                received_order_id = row[received_order_idx]

                if received_order_id:
                    mapping[logical_order_id] = received_order_id
                else:
                    logical_order_ids_without_received.append(logical_order_id)

        progress = (idx + 1) / total_files * 100
        print(f'Progress: {progress:.2f}%')

    return mapping, logical_order_ids_without_received

# Usage example:
directory = "path/to/csv/files"
result, missing_received_order_ids = map_logical_to_received_order_ids(directory)
print("Mapping:", result)
print("Logical Order IDs without Received Order ID:", missing_received_order_ids)

In this version, the function calculates the progress percentage based on the number of processed files out of the total number of CSV files in the directory and prints it to the console. The tkinter progress bar has been removed.