Skip to content

Modules are now callable, see issue #14 #21

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

Open
wants to merge 3 commits into
base: master
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
30 changes: 15 additions & 15 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@ Overflow?

Let’s take it one step further.

| ``from stackoverflow import quick_sort`` will go through the search
| ``from stackoverflow import sort`` will go through the search
results
| of ``[python] quick sort`` looking for the largest code block that
| of ``[python] sort`` looking for the largest code block that
doesn’t
| syntax error in the highest voted answer from the highest voted
question
| and return it as a module. If that answer doesn’t have any valid
python
| code, it checks the next highest voted answer for code blocks.

.. code:: python
.. code:: pytho

>>> from stackoverflow import quick_sort, split_into_chunks
from stackoverflow import sort
print(sort([3,1,2]))
print("I wonder who made sort", sort.__author__)
print("but what's the license? Can I really use this?", sort.__license__)

>>> print(quick_sort.sort([1, 3, 2, 5, 4]))
[1, 2, 3, 4, 5]

>>> print(list(split_into_chunks.chunk("very good chunk func")))
['very ', 'good ', 'chunk', ' func']

>>> print("I wonder who made split_into_chunks", split_into_chunks.__author__)
I wonder who made split_into_chunks https://stackoverflow.com/a/35107113

>>> print("but what's the license? Can I really use this?", quick_sort.__license__)
from stackoverflow import file_copy
print(file_copy("main.py", "main2.py"))

""" prints
[1, 2, 3]
I wonder who made sort https://stackoverflow.com/a/49073645
but what's the license? Can I really use this? CC BY-SA 3.0
>>> assert("nice, attribution!")
████████████████████ [100.00%]main2.py
"""

This module is licensed under whatever license you want it to be as
long as the license is compatible with the fact that I blatantly
Expand Down
17 changes: 7 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from stackoverflow import quick_sort, split_into_chunks
print(quick_sort.sort([1, 3, 2, 5, 4]))
print(list(split_into_chunks.chunk("very good chunk func")))
print("gotta take a break")
from time import time
t1 = time()
from stackoverflow import time_delay
print("that's enough, let's continue", time()-t1)
print("I wonder who made split_into_chunks", split_into_chunks.__author__)
print("but what's the license? Can I really use this?", quick_sort.__license__)
from stackoverflow import sort
print(sort([3,1,2]))
print("I wonder who made sort", sort.__author__)
print("but what's the license? Can I really use this?", sort.__license__)

from stackoverflow import file_copy
print(file_copy("main.py", "main2.py"))
46 changes: 35 additions & 11 deletions stackoverflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import html
import re
import sys
from copy import copy
from importlib._bootstrap import spec_from_loader

import requests


class StackOverflowImporter:
"""
`from stackoverflow import quick_sort` will go through the search results
Expand All @@ -19,10 +19,25 @@ class StackOverflowImporter:
@classmethod
def find_spec(cls, fullname, path=None, target=None):
spec = spec_from_loader(fullname, cls, origin='hell')
spec.__license__ = "CC BY-SA 3.0"
spec._url = cls._fetch_url(spec.name)
spec._code, spec.__author__ = cls._fetch_code(spec._url)
spec.__license__ = "CC BY-SA 3.0" # todo: fix this
spec._code, spec._url, spec.__author__ = cls.get_code_url_author(spec.name)
cls.make_callable(spec)
return spec

@classmethod
def make_callable(cls, spec):
""" to be callable, your (copy of) __class__ must have a __call__ """
methods = spec._code.co_names
if methods:
spec.__class__ = copy(spec.__class__)
spec.__class__.method = methods[-1] # first methods can be imports like os, so take last!
def call(cls, *arg, **varg):
""" during find_spec, the method itself is not in cls -> dynamic calling """
method = cls.__getattribute__(cls.method)
return method(*arg, **varg)
spec.__class__.__call__ = call
else:
print("No module-calling avaiable, as co_names is empty")

@classmethod
def create_module(cls, spec):
Expand All @@ -36,16 +51,24 @@ def exec_module(cls, module=None):
exec(module._code, module.__dict__)
except:
print(module._url)
print(module._code)
print(module)
raise

@classmethod
def get_code(cls, fullname):
return compile(cls._fetch_code(cls._fetch_url(fullname)), 'StackOverflow.com/' + fullname, 'exec')
def get_code_url_author(cls, fullname):
urls = cls._fetch_urls(fullname)
for url in urls:
try:
code, author = cls._fetch_code(url)
return compile(code, 'StackOverflow.com/' + fullname, 'exec') , url, author
except ImportError:
pass
raise ImportError("This question ain't got no good code.")


@classmethod
def get_source(cls, fullname):
return cls.get_code(fullname)
return cls.get_code_url_author(fullname)[0]

@classmethod
def is_package(cls, fullname):
Expand All @@ -54,7 +77,7 @@ def is_package(cls, fullname):
############################

@classmethod
def _fetch_url(cls, query):
def _fetch_urls(cls, query):
query = query.lower().replace("stackoverflow.", "").replace("_", " ")
ans = requests.get(cls.API_URL + "/search", {
"order": "desc",
Expand All @@ -65,7 +88,8 @@ def _fetch_url(cls, query):
}).json()
if not ans["items"]:
raise ImportError("Couldn't find any question matching `" + query + "`")
return ans["items"][0]["link"]
items = ans["items"]
return [i["link"] for i in items]

@classmethod
def _fetch_code(cls, url):
Expand All @@ -74,7 +98,7 @@ def _fetch_code(cls, url):

@staticmethod
def _find_code_in_html(s):
answers = re.findall(r'<div id="answer-.*?</table', s, re.DOTALL) # come get me, Zalgo
answers = re.findall(r'<div id="answer-.*<div class="answercell.*class="suggest-edit-post"', s, re.DOTALL) # come get me, Zalgo

def votecount(x):
"""
Expand Down