-
Notifications
You must be signed in to change notification settings - Fork 605
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
csvjoin: Implement case-insensitive key comparison #610
Open
anorth
wants to merge
1
commit into
wireservice:master
Choose a base branch
from
dssrt:master
base: master
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
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 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 |
---|---|---|
|
@@ -10,3 +10,4 @@ docs/_build | |
.coverage | ||
.tox | ||
cover | ||
env | ||
This file contains 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 |
---|---|---|
@@ -1,15 +1,16 @@ | ||
#!/usr/bin/env python | ||
|
||
|
||
def _get_ordered_keys(rows, column_index): | ||
def _get_keys(rows, column_index, lowercase=False): | ||
""" | ||
Get ordered keys from rows, given the key column index. | ||
Get keys from rows as keys in a dictionary (i.e. unordered), given the key column index. | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that the order of these keys was never used, just |
||
return [r[column_index] for r in rows] | ||
pairs = ((r[column_index], True) for r in rows) | ||
return CaseInsensitiveDict(pairs) if lowercase else dict(pairs) | ||
|
||
|
||
def _get_mapped_keys(rows, column_index): | ||
mapped_keys = {} | ||
def _get_mapped_keys(rows, column_index, case_insensitive=False): | ||
mapped_keys = CaseInsensitiveDict() if case_insensitive else {} | ||
|
||
for r in rows: | ||
key = r[column_index] | ||
|
@@ -21,6 +22,11 @@ def _get_mapped_keys(rows, column_index): | |
|
||
return mapped_keys | ||
|
||
def _lower(key): | ||
"""Transforms a string to lowercase, leaves other types alone.""" | ||
keyfn = getattr(key, 'lower', None) | ||
return keyfn() if keyfn else key | ||
|
||
|
||
def sequential_join(left_rows, right_rows, header=True): | ||
""" | ||
|
@@ -49,7 +55,7 @@ def sequential_join(left_rows, right_rows, header=True): | |
return output | ||
|
||
|
||
def inner_join(left_rows, left_column_id, right_rows, right_column_id, header=True): | ||
def inner_join(left_rows, left_column_id, right_rows, right_column_id, header=True, ignore_case=False): | ||
""" | ||
Execute an inner join on two tables and return the combined table. | ||
""" | ||
|
@@ -63,7 +69,7 @@ def inner_join(left_rows, left_column_id, right_rows, right_column_id, header=Tr | |
output = [] | ||
|
||
# Map right rows to keys | ||
right_mapped_keys = _get_mapped_keys(right_rows, right_column_id) | ||
right_mapped_keys = _get_mapped_keys(right_rows, right_column_id, ignore_case) | ||
|
||
for left_row in left_rows: | ||
len_left_row = len(left_row) | ||
|
@@ -80,7 +86,7 @@ def inner_join(left_rows, left_column_id, right_rows, right_column_id, header=Tr | |
return output | ||
|
||
|
||
def full_outer_join(left_rows, left_column_id, right_rows, right_column_id, header=True): | ||
def full_outer_join(left_rows, left_column_id, right_rows, right_column_id, header=True, ignore_case=False): | ||
""" | ||
Execute full outer join on two tables and return the combined table. | ||
""" | ||
|
@@ -94,11 +100,11 @@ def full_outer_join(left_rows, left_column_id, right_rows, right_column_id, head | |
else: | ||
output = [] | ||
|
||
# Get ordered keys | ||
left_ordered_keys = _get_ordered_keys(left_rows, left_column_id) | ||
# Get left keys | ||
left_keys = _get_keys(left_rows, left_column_id, ignore_case) | ||
|
||
# Get mapped keys | ||
right_mapped_keys = _get_mapped_keys(right_rows, right_column_id) | ||
right_mapped_keys = _get_mapped_keys(right_rows, right_column_id, ignore_case) | ||
|
||
for left_row in left_rows: | ||
len_left_row = len(left_row) | ||
|
@@ -116,13 +122,13 @@ def full_outer_join(left_rows, left_column_id, right_rows, right_column_id, head | |
for right_row in right_rows: | ||
right_key = right_row[right_column_id] | ||
|
||
if right_key not in left_ordered_keys: | ||
if right_key not in left_keys: | ||
output.append(([u''] * len_left_headers) + right_row) | ||
|
||
return output | ||
|
||
|
||
def left_outer_join(left_rows, left_column_id, right_rows, right_column_id, header=True): | ||
def left_outer_join(left_rows, left_column_id, right_rows, right_column_id, header=True, ignore_case=False): | ||
""" | ||
Execute left outer join on two tables and return the combined table. | ||
""" | ||
|
@@ -137,7 +143,7 @@ def left_outer_join(left_rows, left_column_id, right_rows, right_column_id, head | |
output = [] | ||
|
||
# Get mapped keys | ||
right_mapped_keys = _get_mapped_keys(right_rows, right_column_id) | ||
right_mapped_keys = _get_mapped_keys(right_rows, right_column_id, ignore_case) | ||
|
||
for left_row in left_rows: | ||
len_left_row = len(left_row) | ||
|
@@ -155,7 +161,7 @@ def left_outer_join(left_rows, left_column_id, right_rows, right_column_id, head | |
return output | ||
|
||
|
||
def right_outer_join(left_rows, left_column_id, right_rows, right_column_id, header=True): | ||
def right_outer_join(left_rows, left_column_id, right_rows, right_column_id, header=True, ignore_case=False): | ||
""" | ||
Execute right outer join on two tables and return the combined table. | ||
""" | ||
|
@@ -168,11 +174,11 @@ def right_outer_join(left_rows, left_column_id, right_rows, right_column_id, hea | |
else: | ||
output = [] | ||
|
||
# Get ordered keys | ||
left_ordered_keys = _get_ordered_keys(left_rows, left_column_id) | ||
# Get left keys | ||
left_keys = _get_keys(left_rows, left_column_id, ignore_case) | ||
|
||
# Get mapped keys | ||
right_mapped_keys = _get_mapped_keys(right_rows, right_column_id) | ||
right_mapped_keys = _get_mapped_keys(right_rows, right_column_id, ignore_case) | ||
|
||
for left_row in left_rows: | ||
len_left_row = len(left_row) | ||
|
@@ -188,7 +194,47 @@ def right_outer_join(left_rows, left_column_id, right_rows, right_column_id, hea | |
for right_row in right_rows: | ||
right_key = right_row[right_column_id] | ||
|
||
if right_key not in left_ordered_keys: | ||
if right_key not in left_keys: | ||
output.append(([u''] * len_left_headers) + right_row) | ||
|
||
return output | ||
|
||
|
||
|
||
class CaseInsensitiveDict(dict): | ||
""" | ||
Adapted from http://stackoverflow.com/a/32888599/1583437 | ||
""" | ||
def __init__(self, *args, **kwargs): | ||
super(CaseInsensitiveDict, self).__init__(*args, **kwargs) | ||
self._convert_keys() | ||
|
||
def __getitem__(self, key): | ||
return super(CaseInsensitiveDict, self).__getitem__(_lower(key)) | ||
|
||
def __setitem__(self, key, value): | ||
super(CaseInsensitiveDict, self).__setitem__(_lower(key), value) | ||
|
||
def __delitem__(self, key): | ||
return super(CaseInsensitiveDict, self).__delitem__(_lower(key)) | ||
|
||
def __contains__(self, key): | ||
return super(CaseInsensitiveDict, self).__contains__(_lower(key)) | ||
|
||
def pop(self, key, *args, **kwargs): | ||
return super(CaseInsensitiveDict, self).pop(_lower(key), *args, **kwargs) | ||
|
||
def get(self, key, *args, **kwargs): | ||
return super(CaseInsensitiveDict, self).get(_lower(key), *args, **kwargs) | ||
|
||
def setdefault(self, key, *args, **kwargs): | ||
return super(CaseInsensitiveDict, self).setdefault(_lower(key), *args, **kwargs) | ||
|
||
def update(self, single_arg=None, **kwargs): | ||
super(CaseInsensitiveDict, self).update(self.__class__(single_arg)) | ||
super(CaseInsensitiveDict, self).update(self.__class__(**kwargs)) | ||
|
||
def _convert_keys(self): | ||
for k in list(self.keys()): | ||
v = super(CaseInsensitiveDict, self).pop(k) | ||
self.__setitem__(k, v) |
This file contains 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 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
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.
This is for a virtualenv, but let me know if you have another convention you strongly prefer.