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

Make the imported module callable #14

Open
Xion opened this issue Sep 25, 2018 · 2 comments
Open

Make the imported module callable #14

Xion opened this issue Sep 25, 2018 · 2 comments

Comments

@Xion
Copy link

Xion commented Sep 25, 2018

While this marvelous library is obviously saving everyone a lot of time going back and forth between SO, clipboard, and their IDE, I feel its usage is still not as straightforward as it could be.

For example, in this snippet:

from stackoverflow import split_into_chunks
print(list(split_into_chunks.chunk("very good chunk func")))

you still need to know to call a chunk function from the imported module. It could've been chunks, split, or whatever else (depending on what the SO answer chose), forcing the programmer to (gasp!) look at the code he or she imports and perhaps even understand it.

I'd suggest that the importer should be clever enough to pick the correct function for the programmer, and then make the imported symbol itself callable:

from stackoverflow import split_into_chunks
print(list(split_into_chunks("very good chunk func")))

The heuristics of making the correct choice are left as exercise for the reader.

@xigoi
Copy link

xigoi commented Sep 12, 2019

Maybe it could look for an answer which contains a def block; if there are multiple def blocks, find the one that isn't called from the others.

rnbwdsh added a commit to rnbwdsh/stack-overflow-import that referenced this issue Oct 1, 2019
@amcgregor
Copy link

amcgregor commented May 16, 2020

Additional heuristic: in code blocks which contain viable executable code theoretically applicable to the problem, bare / outside the scope of a declared function, isolate the initial literal assignment(s) (which is/are most typically the sample input value(s) in short code demonstrations), remove it/them, and use the labels as the argument names of the generated callable. (Any trailing print call may be suitable for transformation into the final return.)

E.g. this following bare snippet in answer to "how do you multiply two numbers together":

x = 27
y = 42
print(x * y)

Would transform to, if imported as "multiply":

def multiply(x, y):
    return x * y

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

No branches or pull requests

3 participants