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

Test the Union Operator on dictionaries #53

Merged
merged 6 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions test/example-uniondict-normalization/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"authors": [
"brocla"
],
"files": {
"solution": [
"example_uniondict_normalization.py"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""
This exmple comes from 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.
"""
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 = default_settings | 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)




""" Examples from PEP584 - Add Union Operators To dict"""
d = {'spam': 1, 'eggs': 2, 'cheese': 3}
e = {'cheese': 'cheddar', 'aardvark': 'Ethel'}
# d | e # Representer fails, result must be assigned. # TODO Fix this ????
# e | d # Representer fails
x = d | e # {'spam': 1, 'eggs': 2, 'cheese': 'cheddar', 'aardvark': 'Ethel'}
y = e | d # {'cheese': 3, 'aardvark': 'Ethel', 'spam': 1, 'eggs': 2}
d |= e # {'spam': 1, 'eggs': 2, 'cheese': 'cheddar', 'aardvark': 'Ethel'}
BethanyG marked this conversation as resolved.
Show resolved Hide resolved





""" Examples from the Cpython unit tests"""
a = {0: 0, 1: 1, 2: 1}
b = {1: 1, 2: 2, 3: 3}

c = a.copy()
c |= b

assert a | b == {0: 0, 1: 1, 2: 2, 3: 3}
assert c == {0: 0, 1: 1, 2: 2, 3: 3}

c = b.copy()
c |= a

assert b | a == {1: 1, 2: 1, 3: 3, 0: 0}
assert c == {1: 1, 2: 1, 3: 3, 0: 0}

c = a.copy()
c |= [(1, 1), (2, 2), (3, 3)]

assert c == {0: 0, 1: 1, 2: 2, 3: 3}



BethanyG marked this conversation as resolved.
Show resolved Hide resolved

""" Example from `alphametrics` exercise """
def assign(letters, selections, lefty, righty):
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 i in curr_choices:
BethanyG marked this conversation as resolved.
Show resolved Hide resolved
actual = slc | {letters[0]: i} # combine two dictionaries
new_selections.append((actual, choices - set([i])))

BethanyG marked this conversation as resolved.
Show resolved Hide resolved
selections = new_selections
letters = letters[1:]
return [slc for slc, _ in selections]
29 changes: 29 additions & 0 deletions test/example-uniondict-normalization/mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"placeholder_0": "merge_settings",
"placeholder_1": "default_settings",
"placeholder_2": "toml_file_path",
"placeholder_3": "f",
"placeholder_4": "toml_settings",
"placeholder_5": "merged_settings",
"placeholder_6": "final_settings",
"placeholder_7": "d",
"placeholder_8": "e",
"placeholder_9": "x",
"placeholder_10": "y",
"placeholder_11": "a",
"placeholder_12": "b",
"placeholder_13": "c",
"placeholder_14": "assign",
"placeholder_15": "letters",
"placeholder_16": "selections",
"placeholder_17": "lefty",
"placeholder_18": "righty",
"placeholder_19": "new_selections",
"placeholder_20": "selection",
"placeholder_21": "slc",
"placeholder_22": "choices",
"placeholder_23": "curr_choices",
"placeholder_24": "i",
"placeholder_25": "actual",
"placeholder_26": "_"
}
3 changes: 3 additions & 0 deletions test/example-uniondict-normalization/representation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": 2
}
Loading
Loading