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

Added support for multi-keys on CSVs #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ venv
.DS_Store
.schema
.vscode
build/*
7 changes: 5 additions & 2 deletions csv_diff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import hashlib


def load_csv(fp, key=None, dialect=None):
def load_csv(fp, key=None, dialect=None, key_sep='-'):
if dialect is None and fp.seekable():
# Peek at first 1MB to sniff the delimiter and other dialect details
peek = fp.read(1024 ** 2)
Expand All @@ -18,7 +18,10 @@ def load_csv(fp, key=None, dialect=None):
headings = next(fp)
rows = [dict(zip(headings, line)) for line in fp]
if key:
keyfn = lambda r: r[key]
if type(key) == list: # if a list of cols provided then build a concatenated key with them - order matters
keyfn = lambda r: key_sep.join([r[x] for x in key])
else:
keyfn = lambda r: r[key]
else:
keyfn = lambda r: hashlib.sha1(
json.dumps(r, sort_keys=True).encode("utf8")
Expand Down
25 changes: 25 additions & 0 deletions tests/test_csv_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@
1,Cleo,5
2,Pancakes,3"""

ELEVEN = """name, state, age
Ann, UT, 56
Ann, NY, 24
Lisa, CA, 35
Bill, FL, 33
Bill, WY, 23"""

TWELVE = """name, state, age
Ann, UT, 45
Ann, NY, 24
Lisa, CA, 35
Bill, WY, 23"""


def test_row_changed():
diff = compare(
Expand Down Expand Up @@ -115,3 +128,15 @@ def test_tsv():
"columns_added": [],
"columns_removed": [],
} == diff

def test_multi_key():
diff = compare(
load_csv(io.StringIO(ELEVEN), key=["name", "state"], key_sep='~'), load_csv(io.StringIO(TWELVE), key=["name", "state"], key_sep='~'),
)
assert {
'added': [],
'removed': [{'name': 'Bill', 'state': 'FL', 'age': '33'}],
'changed': [{'key': 'Ann~UT', 'changes': {'age': ['56', '45']}}],
'columns_added': [],
'columns_removed': [],
} == diff