Skip to content

Commit

Permalink
fixed some weird error where edit_sei_line was—
Browse files Browse the repository at this point in the history
expecting an int for a replacement value and not—
a string.

seisen.py updated for the changes above ^
same with vocab.py and words.py

Removed debugging statements that were in add_csep

added replace_vocab_value (holy hell that was suffering)

Fixed a formatting issue in searcher
  • Loading branch information
Bikatr7 committed Jul 27, 2023
1 parent 8f9609c commit bf1e31d
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 19 deletions.
124 changes: 116 additions & 8 deletions modules/changeSettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,18 @@ def vocab_settings(local_handler:localHandler) -> localHandler:
"""

vocab_message = "What are you trying to do?\n\n1.Add Vocab\n2.Add CSEP/Answer to Vocab\n"
vocab_message = "What are you trying to do?\n\n1.Add Vocab\n2.Add CSEP/Answer to Vocab\n3.Replace Vocab Value\n"

print(vocab_message)

type_setting = util.input_check(4, str(msvcrt.getch().decode()), 2, vocab_message)
type_setting = util.input_check(4, str(msvcrt.getch().decode()), 3, vocab_message)

if(type_setting == "1"):
local_handler = add_vocab(local_handler)
elif(type_setting == "2"):
local_handler = add_csep(local_handler)
elif(type_setting == "3"):
local_handler = replace_vocab_value(local_handler)

return local_handler

Expand Down Expand Up @@ -279,7 +281,7 @@ def add_csep(local_handler:localHandler) -> localHandler:
local_handler (object - localHandler) : the local handler.\n
Returns:\n
local_handler (object - localHandler) : the altered local handler.n
local_handler (object - localHandler) : the altered local handler.\n
"""

Expand All @@ -305,11 +307,6 @@ def add_csep(local_handler:localHandler) -> localHandler:

try:

print(vocab_term)
print(vocab_id)

util.pause_console()

assert vocab_term != "-1"
assert vocab_id != -1

Expand All @@ -334,4 +331,115 @@ def add_csep(local_handler:localHandler) -> localHandler:
new_csep = csep_blueprint(int(vocab_id), new_csep_id, csep_value, local_handler.VOCAB_WORD_TYPE)
local_handler.vocab[target_index].testing_material_answer_all.append(new_csep)

return local_handler

##--------------------start-of-replace_vocab_value()------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

def replace_vocab_value(local_handler:localHandler) -> localHandler:

"""
Replaces a value within a vocab.\n
Parameters:\n
local_handler (object - localHandler) : the local handler.\n
Returns:\n
local_handler (object - localHandler) : the altered local handler.\n
"""

value_to_replace = 0
index_to_replace = 0

target_vocab_line = 0
target_csep_line = 0

target_index = 0

csep_id = 0

ATTRIBUTE_TESTING_MATERIAL = 2
ATTRIBUTE_ROMAJI = 3
ATTRIBUTE_FURIGANA = 5
ATTRIBUTE_TESTING_MATERIAL_ANSWER_MAIN = 4
ATTRIBUTE_INCORRECT_COUNT = 6
ATTRIBUTE_CORRECT_COUNT = 7

try:
vocab_term_or_id = util.user_confirm("Please enter the vocab or vocab id that you want to replace a value in.")

except:
return local_handler

if(vocab_term_or_id.isdigit() == True):
vocab_id = int(vocab_term_or_id)
vocab_term = local_handler.searcher.get_term_from_id(local_handler, vocab_id)
else:
vocab_term = vocab_term_or_id
vocab_id = local_handler.searcher.get_id_from_term(local_handler, vocab_term)

try:

assert vocab_term != "-1"
assert vocab_id != -1

except AssertionError:
local_handler.logger.log_action("Invalid id or term.")
print("invalid id or term\n")
time.sleep(1)
return local_handler

except util.UserCancelError:
return local_handler

target_index = next((i for i, vocab in enumerate(local_handler.vocab) if vocab.word_id == vocab_id), -1)

target_vocab = local_handler.vocab[target_index]

type_replacement_message = local_handler.searcher.get_print_item_from_id(local_handler, vocab_id)

type_replacement_message += "\n\nWhat value would you like to replace? (1-6) (select index)"

print(type_replacement_message)

type_value = util.input_check(4, str(msvcrt.getch().decode()), 6, type_replacement_message)

attributes_map = {
"1": (target_vocab.testing_material, ATTRIBUTE_TESTING_MATERIAL),
"2": (target_vocab.romaji, ATTRIBUTE_ROMAJI),
"3": (target_vocab.furigana, ATTRIBUTE_FURIGANA),
"4": (target_vocab.testing_material_answer_main, ATTRIBUTE_TESTING_MATERIAL_ANSWER_MAIN),
"5": (target_vocab.incorrect_count, ATTRIBUTE_INCORRECT_COUNT),
"6": (target_vocab.correct_count, ATTRIBUTE_CORRECT_COUNT)
}

value_to_replace, index_to_replace = attributes_map[type_value]

try:
replacement_value = util.user_confirm("What are you replacing " + str(value_to_replace) + " with?")

except:
return local_handler

## if the user is changing the main definition, we also need to adjust the csep for it
if(type_value == "4"):

csep_id = next((csep.csep_id for csep in target_vocab.testing_material_answer_all if csep.csep_value == value_to_replace))

target_csep_line = next((i + 1 for i, line in enumerate(local_handler.vocab_csep_path) if int(util.read_sei_file(local_handler.vocab_csep_path, i + 1, 2)) == csep_id))

util.edit_sei_line(local_handler.vocab_csep_path, target_csep_line, 3, str(replacement_value))

else:
pass

target_vocab_line = next((i + 1 for i, line in enumerate(local_handler.vocab_path) if int(util.read_sei_file(local_handler.vocab_path, i + 1, 1)) == vocab_id))

## edits the vocab word
util.edit_sei_line(local_handler.vocab_path, target_vocab_line, index_to_replace, str(replacement_value))

## it's easier to just reload everything than for me to figure out how to juggle csep values in the handler if the user wants to fuck with definitions or answers
local_handler.load_words_from_local_storage()

return local_handler
2 changes: 1 addition & 1 deletion modules/searcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def get_print_item_from_id(self, local_handler:localHandler, vocab_id:int):
f"Vocab: {target_vocab.testing_material}\n"
f"Romaji: {target_vocab.romaji}\n"
f"Furigana: {target_vocab.furigana}\n"
f"Definition {target_vocab.testing_material_answer_main}\n"
f"Definition: {target_vocab.testing_material_answer_main}\n"
f"Incorrect Guesses: {target_vocab.incorrect_count}\n"
f"Correct Guesses: {target_vocab.correct_count}\n"
f"ID: {target_vocab.word_id}\n"
Expand Down
4 changes: 2 additions & 2 deletions modules/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def write_sei_line(sei_file_path:str, items_to_write:typing.List[str]) -> None:

##-------------------start-of-read_sei_file()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

def edit_sei_line(file_path:str, target_line:int, column_number:int, value_to_replace_to:int) -> None:
def edit_sei_line(file_path:str, target_line:int, column_number:int, value_to_replace_to:str) -> None:

"""
Expand All @@ -267,7 +267,7 @@ def edit_sei_line(file_path:str, target_line:int, column_number:int, value_to_re
line = lines[target_line - 1]
items = line.split(",")

items[column_number - 1] = str(value_to_replace_to)
items[column_number - 1] = value_to_replace_to

new_line = ",".join(items)

Expand Down
4 changes: 2 additions & 2 deletions modules/vocab.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def log_correct_answer(self, handler:localHandler) -> None:
## line returned needs to be incremented by one to match file
line_to_write_to = vocab_ids.index(str(self.word_id)) + 1

util.edit_sei_line(handler.vocab_path, line_to_write_to, CORRECT_ANSWER_COUNT_FILE_INDEX_LOCATION , self.correct_count)
util.edit_sei_line(handler.vocab_path, line_to_write_to, CORRECT_ANSWER_COUNT_FILE_INDEX_LOCATION , str(self.correct_count))

handler.logger.log_action("Logged a correct answer for " + self.testing_material + ", id : " + str(self.word_id))

Expand Down Expand Up @@ -133,7 +133,7 @@ def log_incorrect_answer(self, handler:localHandler) -> None:
## line returned needs to be incremented by one to match file
line_to_write_to = vocab_ids.index(str(self.word_id)) + 1

util.edit_sei_line(handler.vocab_path, line_to_write_to, INCORRECT_ANSWER_COUNT_FILE_INDEX_LOCATION , self.incorrect_count)
util.edit_sei_line(handler.vocab_path, line_to_write_to, INCORRECT_ANSWER_COUNT_FILE_INDEX_LOCATION , str(self.incorrect_count))

handler.logger.log_action("Logged an incorrect answer for " + self.testing_material + ", id : " + str(self.word_id))

Expand Down
4 changes: 2 additions & 2 deletions modules/words.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def log_correct_answer(self, handler:localHandler) -> None:
## line returned needs to be incremented by one to match file
line_to_write_to = kana_ids.index(str(self.word_id)) + 1

util.edit_sei_line(handler.kana_path, line_to_write_to, CORRECT_ANSWER_COUNT_FILE_INDEX_LOCATION , self.correct_count)
util.edit_sei_line(handler.kana_path, line_to_write_to, CORRECT_ANSWER_COUNT_FILE_INDEX_LOCATION , str(self.correct_count))

handler.logger.log_action("Logged a correct answer for " + self.testing_material + ", id : " + str(self.word_id))

Expand Down Expand Up @@ -148,7 +148,7 @@ def log_incorrect_answer(self ,handler:localHandler) -> None:
## line returned needs to be incremented by one to match file
line_to_write_to = kana_ids.index(str(self.word_id)) + 1

util.edit_sei_line(handler.kana_path, line_to_write_to, INCORRECT_ANSWER_COUNT_FILE_INDEX_LOCATION , self.incorrect_count)
util.edit_sei_line(handler.kana_path, line_to_write_to, INCORRECT_ANSWER_COUNT_FILE_INDEX_LOCATION , str(self.incorrect_count))

handler.logger.log_action("Logged an incorrect answer for " + self.testing_material + ", id : " + str(self.word_id))

Expand Down
8 changes: 4 additions & 4 deletions seisen.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ def test_kana(self) -> None:

util.clear_console()

util.edit_sei_line(self.loop_data_path, 1, ROUND_COUNT_INDEX_LOCATION, total_number_of_rounds)
util.edit_sei_line(self.loop_data_path, 1, NUMBER_OF_CORRECT_ROUNDS_INDEX_LOCATION, number_of_correct_rounds)
util.edit_sei_line(self.loop_data_path, 1, ROUND_COUNT_INDEX_LOCATION, str(total_number_of_rounds))
util.edit_sei_line(self.loop_data_path, 1, NUMBER_OF_CORRECT_ROUNDS_INDEX_LOCATION, str(number_of_correct_rounds))

self.logger.log_action("--------------------------------------------------------------")

Expand Down Expand Up @@ -421,8 +421,8 @@ def test_vocab(self) -> None:

util.clear_console()

util.edit_sei_line(self.loop_data_path, 1, ROUND_COUNT_INDEX_LOCATION, total_number_of_rounds)
util.edit_sei_line(self.loop_data_path, 1, NUMBER_OF_CORRECT_ROUNDS_INDEX_LOCATION, number_of_correct_rounds)
util.edit_sei_line(self.loop_data_path, 1, ROUND_COUNT_INDEX_LOCATION, str(total_number_of_rounds))
util.edit_sei_line(self.loop_data_path, 1, NUMBER_OF_CORRECT_ROUNDS_INDEX_LOCATION, str(number_of_correct_rounds))

self.logger.log_action("--------------------------------------------------------------")

Expand Down

0 comments on commit bf1e31d

Please sign in to comment.