-
Notifications
You must be signed in to change notification settings - Fork 67
add custom dict camelize logic #246
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
Open
DaltheCow
wants to merge
3
commits into
main
Choose a base branch
from
remove-pyhumps
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
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,23 @@ | ||
def recursive_key_update(d, key_update_func): | ||
if not isinstance(d, dict) and not isinstance(d, list): | ||
return d | ||
|
||
if isinstance(d, list): | ||
for item in d: | ||
recursive_key_update(item, key_update_func) | ||
return d | ||
|
||
updated_key_pairs = [] | ||
for key, _ in d.items(): | ||
updated_key = key_update_func(key) | ||
if key != updated_key: | ||
updated_key_pairs.append((key, updated_key )) | ||
|
||
for key_pair in updated_key_pairs: | ||
old_key, updated_key = key_pair | ||
d[updated_key] = d[old_key] | ||
del d[old_key] | ||
|
||
for _, value in d.items(): | ||
recursive_key_update(value, key_update_func) | ||
return d |
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
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
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
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
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
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
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
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
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,76 @@ | ||
import pytest | ||
|
||
from guidellm.utils.dict import recursive_key_update | ||
|
||
|
||
def update_str(string): | ||
return string + "_updated" | ||
|
||
@pytest.mark.smoke | ||
def test_recursive_key_update_updates_keys(): | ||
my_dict = { | ||
"my_key": { | ||
"my_nested_key": { | ||
"my_double_nested_key": "someValue" | ||
}, | ||
"my_other_nested_key": "someValue" | ||
}, | ||
"my_other_key": "value" | ||
} | ||
my_updated_dict = { | ||
"my_key_updated": { | ||
"my_nested_key_updated": { | ||
"my_double_nested_key_updated": "someValue" | ||
}, | ||
"my_other_nested_key_updated": "someValue" | ||
}, | ||
"my_other_key_updated": "value" | ||
} | ||
recursive_key_update(my_dict, update_str) | ||
assert my_dict == my_updated_dict | ||
|
||
|
||
def truncate_str_to_ten(string): | ||
return string[:10] | ||
|
||
|
||
@pytest.mark.smoke | ||
def test_recursive_key_update_leaves_unchanged_keys(): | ||
my_dict = { | ||
"my_key": { | ||
"my_nested_key": { | ||
"my_double_nested_key": "someValue" | ||
}, | ||
"my_other_nested_key": "someValue" | ||
}, | ||
"my_other_key": "value" | ||
} | ||
my_updated_dict = { | ||
"my_key": { | ||
"my_nested_": { | ||
"my_double_": "someValue" | ||
}, | ||
"my_other_n": "someValue" | ||
}, | ||
"my_other_k": "value" | ||
} | ||
recursive_key_update(my_dict, truncate_str_to_ten) | ||
assert my_dict == my_updated_dict | ||
|
||
|
||
@pytest.mark.smoke | ||
def test_recursive_key_update_updates_dicts_in_list(): | ||
my_dict = { | ||
"my_key": | ||
[{ "my_list_item_key_1": "someValue" }, | ||
{ "my_list_item_key_2": "someValue" }, | ||
{ "my_list_item_key_3": "someValue" }] | ||
} | ||
my_updated_dict = { | ||
"my_key_updated": | ||
[{ "my_list_item_key_1_updated": "someValue" }, | ||
{ "my_list_item_key_2_updated": "someValue" }, | ||
{ "my_list_item_key_3_updated": "someValue" }] | ||
} | ||
recursive_key_update(my_dict, update_str) | ||
assert my_dict == my_updated_dict |
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,12 @@ | ||
import pytest | ||
|
||
from guidellm.utils.text import camelize_str | ||
|
||
|
||
@pytest.mark.smoke | ||
def test_camelize_str_camelizes_string(): | ||
assert camelize_str("no_longer_snake_case") == "noLongerSnakeCase" | ||
|
||
@pytest.mark.smoke | ||
def test_camelize_str_leaves_non_snake_case_text_untouched(): | ||
assert camelize_str("notsnakecase") == "notsnakecase" |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small nit. Might want to throw an exception here in the case that
updated_key
already exists. Could save a bunch of debugging time later.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this handled by line 13 where we prevent any addition to updated_key_pairs if the old key and updated key are the same? So it is impossible for updated_key_pairs to contain anything but updated keys I would say.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also sort of a sidenote but it will be pretty common for keys to already exist in the case of a camelizing function being the update function, since for example
benchmarks
would not require any updating and that is an expected behavior that we don't need to update every key if it is already the same as the updated version