-
-
Notifications
You must be signed in to change notification settings - Fork 10
Test the Union Operator on dictionaries #53
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9013cec
Test the union operator for dictionaries
brocla 2ab9e9d
Convert to tomlib
brocla bec33ff
Drop unit tests.
brocla b4c2c17
Update test/example-uniondict-normalization/example_uniondict_normali…
BethanyG 5bf6ab5
Update test/example-uniondict-normalization/example_uniondict_normali…
BethanyG 9f06ce7
Re Ran Test Representation
BethanyG File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"authors": [ | ||
"brocla" | ||
], | ||
"files": { | ||
"solution": [ | ||
"example_uniondict_normalization.py" | ||
] | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
test/example-uniondict-normalization/example_uniondict_normalization.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
""" Examples adapted from Mecha Munch Management, Alphametrics, and ChatGPT | ||
|
||
ChatGPT Prompt: Write a python function that | ||
reads user settings from a toml file and | ||
merges them with a dictionary of default | ||
settings to create a single dictionary. | ||
If there are any conflicts between the | ||
two sources of settings, the data from | ||
the toml file should be used. | ||
""" | ||
|
||
|
||
def update_recipes_tuple(ideas, recipe_updates): | ||
"""Mecha Munch Management Example. | ||
|
||
Update the recipe ideas dictionary. | ||
:param ideas: dict - The "recipe ideas" dict. | ||
:param recipe_updates: tuple - tuple with updates for the ideas section. | ||
:return: dict - updated "recipe ideas" dict. | ||
""" | ||
|
||
# recipe_updates here is a tuple. | ||
# Since this action updates the dict in place, | ||
# the dict then needs to be returned separately, otherwise it is a syntax error. | ||
ideas |= recipe_updates | ||
return ideas | ||
|
||
|
||
def update_recipes_dict(ideas, recipe_updates): | ||
"""Second Mecha Munch Management Example. | ||
|
||
Update the recipe ideas dictionary. | ||
:param ideas: dict - The "recipe ideas" dict. | ||
:param recipe_updates: dict - dictionary with updates for the ideas section. | ||
:return: dict - updated "recipe ideas" dict. | ||
""" | ||
|
||
# Since this action returns a *new* dict, it can go directly on the return line. | ||
return dict(ideas) | dict(recipe_updates) | ||
|
||
##Example Usage## | ||
ideas = {'Banana Bread' : {'Banana': 1, 'Apple': 1, 'Walnuts': 1, 'Flour': 1, 'Eggs': 2, 'Butter': 1}} | ||
|
||
recipe_updates_tuple= (('Banana Bread', {'Banana': 4, 'Walnuts': 2, 'Flour': 1, 'Eggs': 2, 'Butter': 1, 'Milk': 2, 'Eggs': 3}),) | ||
|
||
recipe_update_dict= {'Banana Bread': {'Banana': 4, 'Walnuts': 2, 'Flour': 1, 'Eggs': 2, 'Butter': 1, 'Milk': 2, 'Eggs': 3}} | ||
|
||
update_recipes_tuple(ideas, recipe_updates_tuple) | ||
# {'Banana Bread': {'Banana': 4, 'Walnuts': 2, 'Flour': 1, 'Eggs': 3, 'Butter': 1, 'Milk': 2}} | ||
|
||
update_recipes_dict(ideas, recipe_update_dict) | ||
# {'Banana Bread': {'Banana': 4, 'Walnuts': 2, 'Flour': 1, 'Eggs': 3, 'Butter': 1, 'Milk': 2}} | ||
|
||
|
||
def assign(letters, selections, lefty, righty): | ||
""" Example from `alphametrics` exercise """ | ||
|
||
while letters: | ||
new_selections = [] | ||
|
||
for selection in selections: | ||
slc, choices = selection | ||
|
||
if letters[0] in [lefty, righty]: | ||
curr_choices = choices - set([0]) | ||
|
||
else: | ||
curr_choices = choices | ||
|
||
for item in curr_choices: | ||
actual = slc | {letters[0]: item} # combine two dictionaries | ||
new_selections.append((actual, choices - set([item]))) | ||
|
||
selections = new_selections | ||
letters = letters[1:] | ||
return [slc for slc, _ in selections] | ||
|
||
|
||
import tomlib | ||
|
||
def merge_settings(default_settings, toml_file_path): | ||
# Load settings from TOML file | ||
with open(toml_file_path, 'r') as f: | ||
toml_settings = tomlib.load(f) | ||
|
||
# Merge default settings with settings from TOML file | ||
merged_settings = dict(default_settings) | dict(toml_settings) | ||
|
||
return merged_settings | ||
|
||
# Example usage: | ||
default_settings = { | ||
'timeout': 30, | ||
'retry_count': 3, | ||
'log_level': 'INFO' | ||
} | ||
toml_file_path = 'settings.toml' # Path to the TOML file | ||
|
||
final_settings = merge_settings(default_settings, toml_file_path) | ||
print("Final Settings:", final_settings) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"placeholder_0": "update_recipes_tuple", | ||
"placeholder_1": "ideas", | ||
"placeholder_2": "recipe_updates", | ||
"placeholder_3": "update_recipes_dict", | ||
"placeholder_4": "recipe_updates_tuple", | ||
"placeholder_5": "recipe_update_dict", | ||
"placeholder_6": "assign", | ||
"placeholder_7": "letters", | ||
"placeholder_8": "selections", | ||
"placeholder_9": "lefty", | ||
"placeholder_10": "righty", | ||
"placeholder_11": "new_selections", | ||
"placeholder_12": "selection", | ||
"placeholder_13": "slc", | ||
"placeholder_14": "choices", | ||
"placeholder_15": "curr_choices", | ||
"placeholder_16": "item", | ||
"placeholder_17": "actual", | ||
"placeholder_18": "_", | ||
"placeholder_19": "merge_settings", | ||
"placeholder_20": "default_settings", | ||
"placeholder_21": "toml_file_path", | ||
"placeholder_22": "f", | ||
"placeholder_23": "toml_settings", | ||
"placeholder_24": "merged_settings", | ||
"placeholder_25": "final_settings" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"version": 2 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.