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

lambda command #2

Closed
simonw opened this issue Nov 4, 2019 · 5 comments
Labels

Comments

@simonw
Copy link
Owner

@simonw simonw commented Nov 4, 2019

This will allow users to specify their own Python expression or multi-line expression for transforming columns.

For example:

$ sqlite-transform lambda mydb.db mytable col1 --expr='value.upper()'
@simonw simonw added the enhancement label Nov 4, 2019
@simonw

This comment has been minimized.

Copy link
Owner Author

@simonw simonw commented Nov 4, 2019

I think I'm going to have the code come as a required option rather than a positional argument because mixing in that code with the database file and table and columns could get a little messy.

@simonw

This comment has been minimized.

Copy link
Owner Author

@simonw simonw commented Nov 4, 2019

This example is fine for single expression transformations, but it would be great if we could support multi-line function bodies as well. Maybe something like this:

$ sqlite-transform lambda mydb.db mytable col1 --body='
upper = value.upper()
return upper.replace("foo", "bar")'

This would be automatically compiled as the body of a function that takes a single value argument.

@simonw

This comment has been minimized.

Copy link
Owner Author

@simonw simonw commented Nov 4, 2019

For both of these variants we can also support multiple --import= arguments for specifying modules to import.

I mucked around a bit and eventually came up with this recipe for handling dynamic imports with code that is compiled using Python's compile() function.

code = compile("""
def fn(url): 
    return requests.get(url).json() 
""", "<string>", "exec")
locals = {}
globals = {"requests": __import__("requests")}
exec(code, globals, locals)
fn = locals['fn']
json_data = fn("https://latest.datasette.io/.json")
@simonw

This comment has been minimized.

Copy link
Owner Author

@simonw simonw commented Nov 4, 2019

I'm going to ditch --expr and rename --body above to --code. If you want a single line transformation you'll specify it like this:

$ sqlite-transform lambda mydb.db mytable col1 --code='return value.upper()'

This feels less confusing than having two commands depending on if you are using more than one statement or not.

@simonw simonw closed this in 54f7463 Nov 4, 2019
@simonw

This comment has been minimized.

Copy link
Owner Author

@simonw simonw commented Nov 4, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.