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

Composite key #1

Open
DavidUnderdown opened this issue Mar 14, 2019 · 5 comments
Open

Composite key #1

DavidUnderdown opened this issue Mar 14, 2019 · 5 comments
Labels
enhancement New feature or request

Comments

@DavidUnderdown
Copy link

This looks like it will be really useful, thanks.

When we were working on CSV Schema Language we found it necessary to allow uniqueness to be defined over a composite set of columns (the unique column rule in the schema). I can see from the code structure that this wouldn't necessarily be entirely straightforward here, but I think it would be useful.

@simonw
Copy link
Owner

simonw commented Apr 7, 2019

This doesn't seem impossibly difficult to add... it could work by allowing users to specify the --key option multiple times

$ csv-diff one.csv two.csv --key=id --key=secondary

Part of the work would be teaching the CSV loading function to work with compound keys and create the internal ID as a tuple of values:

rows = [dict(zip(headings, line)) for line in fp]
if key:
keyfn = lambda r: r[key]
else:
keyfn = lambda r: hashlib.sha1(json.dumps(r, sort_keys=True).encode("utf8"))
return {keyfn(r): r for r in rows}

The human_text() function would then need to learn how to display a compound ID.

@DavidUnderdown
Copy link
Author

Thanks Simon,

I must admit that I was forgetting that our CSVs do typically have a URI per row too which is unique, so we could use that for purposes of getting a diff. May still be useful for others though.

For human_text(), perhaps some way of passing in a formatting string? In Python terms we'd want something likef"{r['lettercode']} {r['series']}/{r['piece']}/{r['item']} image {r['ordinal']}"

@maxwelllc
Copy link

Seconding this, I'd find this very useful. As best as I can find, there are no other similar libraries that allow for composite keys, but I already use and am very happy with this package.

@simonw simonw added the enhancement New feature or request label Oct 16, 2020
@puddleoasis
Copy link

Hi @simonw I've made progress on this feature and would like to share. Would you grant me write access?

@patric-r
Copy link

@puddleoasis why not submit a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants