Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Progress on xml to son parser #5

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

GersonEsquivel
Copy link
Contributor

Improved automatic template generation for autocompletion mechanism. Current code also saves output files on user's specified directory.

…Current code also saves output files on user's specified directory.
Copy link
Member

@gonuke gonuke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a lot of comments here. I think it looks OK so far, but anticipate a big review when it's closer to being done.

@@ -137,28 +208,45 @@ def handle_choice_element(node):
def process_schema_from_mjson(xml_string, element_name):
root = ET.fromstring(xml_string)
processed_content = process_node(root)

return {element_name: processed_content}

def integrate_detailed_schemas(final_json, processed_facilities, processed_regions, processed_institutions):
for facility_name, facility_config in processed_facilities.items():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given all the repetition here, you could instead do:

    for agent_type, agent_list in zip(['facility', 'institution', 'region'], 
                                      [processed_facilities, processed_instutitions, processed_regions]):
        if agent_type in final_json["simulation"]:
            for agent_name, agent_config in agent_list.items():
                if agent_name in final_json["simulation"][agent_type]["config"]["ChildAtMostOne"]:
                    new_agent_config = {"InputTmpl": f'"{agent_name}", "MaxOccurs": 1}
                    new_agent_config.update(agent_config)
                    final_json["simulation"][agent_type]["config"][agent_name] = new_agent_config

child_indent = base_indent + tab

matching_keys = [key for key in annotations.keys() if key.endswith(key_name)]
matching_keys = [key for key in annotations.keys() if key.split(":")[-2] == key_name.split("_")[-3] and key.split(":")[-1] == key_name.split("_")[-1]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will be more readable if we define the following with variables first:

  • key_name.split("_")[-3]
  • key_name.split("_")[-1]

@GersonEsquivel
Copy link
Contributor Author

Done! I reduced the repetition (very nice structure), and added variables to improve readability.

Copy link
Member

@gonuke gonuke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've requested a few changes here. It's a lot of code to review and I wish we could have done it in more pieces.

I think it would help to have a lot more comments because the specialized knowledge you are acquiring is not clear from the code (and may never be from the code alone). On the other hand, there may be value in more modularity/functions that help describe things instead of comments, in some places.

'silver': [192, 192, 192]}
self.highlight_str = self.make_basic_son()

def highlight_maker(self, name, word, color='blue'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can add bold and italic as function parameters and then use it to set the right characteristics of a rule

Comment on lines 49 to 52
# highlight_str += highlight_maker('brack_open', '{', 'red')
# highlight_str += highlight_maker('brack_close', '}', 'red')
# highlight_str += highlight_maker('square_open', '[', 'lime')
# highlight_str += highlight_maker('square_close', ']', 'lime')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you comment these out? Might be good to put them back in.

Copy link
Contributor Author

@GersonEsquivel GersonEsquivel Feb 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were commented out by Jin Whan. I uncommented it to add them to the highlighter, but the items don't get highlighted. I tried multiple times to see if I was doing something wrong, but it appears that they are not recognized as elements that can be highlighted.

Comment on lines 53 to 88
highlight_str += '''rule("Quoted string") {
pattern = """'[^']*'|"[^"]*""""
bold = true
foreground {
red = 255
green = 130
blue = 0
}
background {
red = 255
green = 130
blue = 0
alpha = 25
}
}

rule("equal"){
pattern="="
background{
red=192
green=192
blue=192
}
}

rule("Comment") {
pattern = "%.*"
italic = true
foreground {
red = 0
green = 128
blue = 0
}
}

'''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we use the highlight_maker function for these? especially if we extend it to support italics and add more colors to the color list

Comment on lines 193 to 195
replacement_text = generate_child_exactly_one_line('facility')
node.text = node.text.replace('@Facility_REFS@', replacement_text)
return f"[{' '.join(facilities)}]", {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this could be a little function since we reuse it in all three cases.

new_region_config.update(region_config)
final_json["simulation"]["region"]["config"][region_name] = new_region_config

for agent_type, agent_list in zip(['facility', 'region'], [processed_facilities, processed_regions]):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be safer/more readable to manually bind/zip these since there will only ever be two:

Suggested change
for agent_type, agent_list in zip(['facility', 'region'], [processed_facilities, processed_regions]):
for agent_type, agent_list in [ ('facility, processed_facilities), ('region', processed_regions) ]:

…highlighter class and some other modularity/function issues.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants