0

I need to find all classes and their methods(with arguments/signature) in a .py file. There are some restrictions:

  1. Should work under python2 and python3(the files to be scanned are python2).

  2. The files are not in python/system path and module dependencies may be not satisfied (import may fail)

As far as I understand such modules like ast, inspect, pyclbr can not be used here for the reasons given above(different python versions etc).

What else can be helpful expect of re module?

user3067543
  • 71
  • 2
  • 5
  • To the best of my knowledge, there haven't been any major changes to classes/functions/modules (in regards to what you want to find), and thus either of the Python 2 or Python 3 `inspect` modules should do the job. – Rushy Panchal Dec 23 '13 at 15:52
  • Can not be done with regex. Python can generate classes/methods dynamically. I think `ast` module will be ok ... your reasons for discounting them don't make sense? – wim Dec 23 '13 at 15:53
  • 1
    You can use separate grammars for Python 2 and Python 3 and use a parser generator to create your own parser. For a good example of this take a look at the code for lib2to3. The Python source code contains a text file describing the grammar for the version you're looking at. Do not try to use regular expressions to implement a parser for Python. It will not work. – Iguananaut Dec 23 '13 at 15:54
  • Regarding `ast`. It fails to parse python2 module from python3. For example, parsing this [module](https://code.google.com/p/robotframework/source/browse/src/robot/libraries/BuiltIn.py) from python3 fails with error: `File "/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/ast.py", line 35, in parse return compile(source, filename, mode, PyCF_ONLY_AST) File "", line 637 first, second = [self._convert_to_string(i) for i in first, second] ^ SyntaxError: invalid syntax` – user3067543 Dec 23 '13 at 16:08
  • Regarding `inspect`. It requires the module to be imported, as it 'inspects live objects', am I right? In my case, I cannot import the module to be inspected. The import statement fails because of issues like: ```>>> import BuiltIn Traceback (most recent call last): File "", line 1, in File "BuiltIn.py", line 20, in from robot.api import logger ImportError: No module named robot.api``` – user3067543 Dec 23 '13 at 16:13

0 Answers0