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

add "fixed-column" option #81

Merged
merged 12 commits into from
Nov 18, 2021
Merged

add "fixed-column" option #81

merged 12 commits into from
Nov 18, 2021

Conversation

williamrowell
Copy link
Contributor

For colname:value, add a column colname and populate with value.

The use case here is that I have a snakemake workflow that produces a lot of csv files with a structure like:
samples/<sample_id>/<condition_name>/this.csv
I want to import them all into the same table, where sample_id and condition_name are columns. I tried to match how --filename_column works, but allow for multiple new columns to be added.

I haven't yet added any tests, but if you think this option would be a useful addition, I will add some tests to cover the basic use case and the combination with shape.

@simonw
Copy link
Owner

simonw commented Nov 17, 2021

I like this as a feature. I'd prefer it to use the following syntax though, rather than implementing its own split on the ::

csvs-to-sqlite test.csv test.db --fixed-column col1 foo --fixed-column col2 bar

Here's an example of an option in sqlite-utils that does this: https://github.com/simonw/sqlite-utils/blob/3b8abe608796e99e4ffc5f3f4597a85e605c0e9b/sqlite_utils/cli.py#L982-L987

The syntax for the decorator would look like this:

@click.option(
    "--fixed-column",
    multiple=True,
    type=(str, str),
    help="Populate column with a fixed value",
)

csvs_to_sqlite/cli.py Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@williamrowell
Copy link
Contributor Author

Realizing that casting the values as float or int probably isn't necessary.

@williamrowell
Copy link
Contributor Author

I integrated the changes above and updated test_fixed_column to test all three options.

  --fixed-column <TEXT TEXT>...   Populate column with a fixed string
  --fixed-column-int <TEXT INTEGER>...
                                  Populate column with a fixed integer
  --fixed-column-float <TEXT FLOAT>...
                                  Populate column with a fixed float

This didn't work for a date column though:
--fixed-column created 20211116 --date created -df '%Y%m%d'

And the reason is a little confusing:
sqlite3.OperationalError: table test has no column named created

@simonw
Copy link
Owner

simonw commented Nov 18, 2021

This looks great to me - I'll merge this in the morning!

@simonw simonw merged commit b686230 into simonw:main Nov 18, 2021
simonw added a commit that referenced this pull request Nov 18, 2021
@williamrowell williamrowell deleted the fixed_column_value branch November 18, 2021 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants