Forum Archive

Pythonista and Pycharm

amdescombes

Hi Everyone,

I have been using Pycharm to cleanup my pythonista code and DropboxSync to , the problem I have is it cannot resolve any of the imports like scene, sounds, etc... Is there any way to include them in Pycharm?
Thanks in advance
Cheers
Andre

ccc

You can use the PyCharm directive on the line before the import:

# noinspection PyUnresolvedReferences
import scene, sounds
amdescombes

Thanks,

I was familiar with that directive, but I was looking for lib skeletons or some such in order to get the benefits of code completion in PyCharm, etc..

Cheers
Andre

ccc
import inspect, scene
print(inspect.getsource(scene))
mikashkin

Modules are available in XCode project template.

amdescombes

Thanks @ccc! now I can get those into PyCharm :-)
Andre

ccc

https://github.com/omz/PythonistaAppTemplate/tree/master/PythonistaAppTemplate/PythonistaKit.framework/pylib_ext Has Scene

amdescombes

Thanks @mikashkin, I didn't think to look for them there :)
Andre

amdescombes

i couldn't find sound.py though, it's not there and inspect tells me it's an internal module, I guess I'll have to build the skeleton myself or is there a way of getting the skeleton automatically ?
Cheers
Andre

ccc
import inspect, sound
fmt = '{} {}:\n    pass  #  {}\n'
for name, func in inspect.getmembers(sound):
    if callable(func):
        def_or_class = 'def' if 'function' in str(func) else 'class'
        print(fmt.format(def_or_class, name, func))
amdescombes

Thanks @ccc !
Is there a way of getting the parameters? :-)
Cheers
Andre

dgelessus

No, it's not possible to get any information about the arguments for functions written in C. Under Python 3 some functions have limited argument information in their __text_signature__ attribute, but I don't think any of the Pythonista module functions support that.

amdescombes

ok, good enough for me!
Cheers
Andre

amdescombes

Hello everybody,

I wrote the following code based on @ccc's post using inspect, I managed to create python files for use with PyCharm. It has some shortcomings as it cannot get the arguments to the functions, and it doesn't group attributes or present the code alphabetically :)
I hope it's useful to someone, I am going to create a gist for it.

Cheers
Andre

import inspect
import sound
import _ui
import _scene2

def_fmt = '\n\n%sdef %s():\n%s    pass\n'
method_fmt = '\n%sdef %s(self):\n%s    pass\n'
cls_method_fmt = '\n%sdef %s(cls):\n%s    pass\n'
class_fmt = '\n\n%sclass %s:%s\n'
doc_fmt = '%s"""%s"""\n'


def handle_attribute(name, func, indent):
    if isinstance(func, int) or isinstance(func, float):
        return '\n%s%s = %s\n' % (indent, name, func)
    else:
        return '\n%s%s = "%s"\n' % (indent, name, func)


def get_info(modul, indentlevel=0, inclass=False):
    _f = []
    indent = '    ' * indentlevel
    for name, func in inspect.getmembers(modul):
        if callable(func):
            if name == '__getattribute__':
                continue
            isfunc = 'function' in str(func)
            if name == '__new__':
                _f.append(cls_method_fmt % (indent, name, indent))
            else:
                _f.append((def_fmt if isfunc else method_fmt if inclass else class_fmt) % (indent, name, indent))
            if not isfunc and not inclass:
                get_info(func, indentlevel + 1, True)
        else:
            if inclass and name == '__doc__':  # insert docstring
                _f.insert(0, doc_fmt % (indent, func))
            else:
                _f.append(handle_attribute(name, func, indent))
    return _f


def create_func(modul, modname, indentlevel=0, inclass=False):
    print "processing %s" % modname
    _f = []
    indent = '    ' * indentlevel
    for name, func in inspect.getmembers(modul):
        if callable(func):
            if name == '__getattribute__':
                continue
            isfunc = 'function' in str(func)
            _f.append((def_fmt if isfunc else method_fmt if inclass else class_fmt) % (indent, name, indent))
            if not isfunc and not inclass:
                cls = get_info(func, indentlevel + 1, True)
                _f += cls
        else:
            if name == '__doc__':  # insert docstring
                _f.insert(0, doc_fmt % (indent, func))
            elif name not in ['__name__', '__package__']:
                _f.append(handle_attribute(name, func, indent))
    open(modname, 'w').write(''.join(_f))
    print "processed %s" % modname


if __name__ == "__main__":
    create_func(sound, 'sound.py')
    create_func(_ui, '_ui.py')
    create_func(_scene2, '_scene2.py')
    print "done"
amdescombes

Ok, here is the gist:

https://gist.github.com/amdescombes/30839ae0280a5077b8669757e5efa75d

Cheers
Andre

ccc

Supercool! If you make stub_builder a repo instead of a gist, then I will send you a pull request or two.

amdescombes

Here you go:

https://github.com/amdescombes/pythonista_stub_builder

Enjoy
Andre