diff --git a/README.rst b/README.rst index 7b5a7d8..c4393ce 100644 --- a/README.rst +++ b/README.rst @@ -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])) diff --git a/stackoverflow/__init__.py b/stackoverflow/__init__.py index f7e3cab..b0f96ba 100644 --- a/stackoverflow/__init__.py +++ b/stackoverflow/__init__.py @@ -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):