Skip to content

Commit

Permalink
1/26 Fix bug and optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
FLwolfy committed Jan 26, 2024
1 parent 7716f84 commit eb1724f
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 63 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,7 @@ cython_debug/
~$*

# generated
example/result/*
example/results/*

# previous
UI/UI_old.py
36 changes: 29 additions & 7 deletions UI/UI.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self, width:int=1200, height:int=720):
"preview": None,
}

self.window.minsize(1200, 720)
self.create_widgets()

def create_widgets(self):
Expand Down Expand Up @@ -327,13 +328,19 @@ def browse(self, entry_widget:tk.Entry, kind: str = ""):
Parameters:
- entry_widget: The entry widget to display the selected file path.
- kind: The kind (str) of the path to import.
"""
path = filedialog.askopenfilename()
entry_widget.delete(0, tk.END)
entry_widget.insert(0, path)

"""
if kind == "Excel Data Path":
# import Excel
path = filedialog.askopenfilename(
title="Select An Excel Dataset",
filetypes=[("Excel File", "*.xlsx")])

if path == "":
return

entry_widget.delete(0, tk.END)
entry_widget.insert(0, path)

self.generator.import_dataset(self.widgets["path"]["Excel Data Path"].get())
self.widgets["columns"].delete(0, tk.END)

Expand All @@ -342,9 +349,24 @@ def browse(self, entry_widget:tk.Entry, kind: str = ""):

elif kind == "Json Template Path":
# import Json Templates
path = filedialog.askopenfilename(
title="Select A Json Template",
filetypes=[("Json File", "*.json")])

if path == "":
return

entry_widget.delete(0, tk.END)
entry_widget.insert(0, path)

self.generator.import_template(self.widgets["path"]["Json Template Path"].get())



elif kind == "Export Path":
# export
path = filedialog.askdirectory()
entry_widget.delete(0, tk.END)
entry_widget.insert(0, path)

def run(self):
"""
Run the main loop of the tkinter window.
Expand Down
39 changes: 0 additions & 39 deletions example/001.json

This file was deleted.

File renamed without changes.
Binary file renamed example/test.xlsx → example/test1.xlsx
Binary file not shown.
File renamed without changes.
12 changes: 8 additions & 4 deletions utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

class JSONGenerator:
def __init__(self):
self.name = ""
self.template: dict = None
self.dataset: pd.DataFrame = None
self.data_size: int = -1
Expand All @@ -23,6 +24,7 @@ def import_template(self, template_dir: str):

def import_dataset(self, excel_dir: str):
self.dataset, self.data_columns, self.data_size = IO.read_excel(excel_dir)
self.name = excel_dir.split("/")[-1].split(".")[0]

# column-wise data pre-processing
self.option_list = dict()
Expand Down Expand Up @@ -54,12 +56,14 @@ def export_json(self, target_dir: str, g_range: tuple[int, int] = None):
if g_range is None:
g_range = (0, self.data_size)

if target_dir[-1] != '/':
target_dir += '/'

for i in range(*g_range):
if(self.previews[i] != {}):
IO.write_json(self.previews[i], f"{target_dir}{i}.json")
id = self.previews[i]["identifier"]

if id:
IO.write_json(self.previews[i], f"{target_dir}/{self.name}/{id}.json")
else:
raise ValueError("No 'identifier' column!")

print(f"total count: {g_range[1] - g_range[0]}")
print(f"to folder: {target_dir}")
Expand Down
9 changes: 4 additions & 5 deletions utils/fileIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import json
import numpy as np


def read_json(json_file_path: str):
# Reading JSON file content into a string
with open(json_file_path, 'r') as json_file:
Expand Down Expand Up @@ -41,12 +40,12 @@ def update_json(tar, path: list[str], value: any):
return tar


def write_json(json_obj: dict, json_file_path: str):
dir = os.path.dirname(json_file_path)
def write_json(json_obj: dict, json_output_path: str):
dir = os.path.dirname(json_output_path)
if not os.path.exists(dir):
os.makedirs(dir)

with open(json_file_path, 'w') as json_file:
with open(json_output_path, 'w') as json_file:
json.dump(json_obj, json_file, indent=4)


Expand Down
14 changes: 7 additions & 7 deletions utils/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def __init__(self):
self.remove_ext_name = False
self.number_to_string = False
self.string_to_number = False
self.expression: str = None # e.g. 'str(x)+".png"'
# self.expression: str = None # e.g. 'str(x)+".png"'

def parse_links(template, columns: pd.Index):
def find_path(json_obj, target_value, current_path=[]):
Expand All @@ -33,10 +33,10 @@ def find_path(json_obj, target_value, current_path=[]):
return result
return None

def to_str_path(path: list):
if path is None:
return None
return "$." + ".".join(path)
# def to_str_path(path: list):
# if path is None:
# return None
# return "$." + ".".join(path)

links = dict()

Expand Down Expand Up @@ -73,8 +73,8 @@ def process_column(datacol: pd.DataFrame, opt: Options) -> pd.DataFrame:
if opt.number_to_string:
datacol = datacol.astype(str)

if opt.expression is not None:
datacol = datacol.apply(lambda x: eval(opt.expression, {'x': x}))
# if opt.expression is not None:
# datacol = datacol.apply(lambda x: eval(opt.expression, {'x': x}))

return datacol

Expand Down

0 comments on commit eb1724f

Please sign in to comment.