Skip to content

Commit

Permalink
Modules are now callable, see issue drathier#14
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbwdsh committed Oct 1, 2019
1 parent 9523ca6 commit ab9fb06
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Let’s take it one step further.
python
| code, it checks the next highest voted answer for code blocks.
.. code:: python
.. code:: pytho
from stackoverflow import sort
print(sort.sort_iterable([3,1,2]))
Expand Down
16 changes: 16 additions & 0 deletions stackoverflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,23 @@ def find_spec(cls, fullname, path=None, target=None):
spec = spec_from_loader(fullname, cls, origin='hell')
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[0]
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 Down

0 comments on commit ab9fb06

Please sign in to comment.