-5

Is there a python regex which will generically match a method definition (not just the declaration but also the method body) inside a python code file?

I did my share of googling but only found something similar for Java. Python is different w.r.t. to the way scopes are entered through indentation rather than accolades. What makes this problem hard is the fact that indentation may drop inside the method body (i.e. blank lines, multiline strings, comments).

I also looked for DOM parsers but basically they're all aimed at XML or HTML.

Finally I am looking into introspection (How can I get the source code of a Python function?) but I still wonder if there is a nicer way for code analysis without execution.

EDIT: the question receives a bunch of downvotes but I think it's actually a valid and specific programming question. I elaborated the question a bit.

Community
  • 1
  • 1
Freek Wiekmeijer
  • 4,556
  • 30
  • 37
  • 2
    What did you come up with so far? `def\s+([^(]*)` will match the function name. – Jan Nov 24 '15 at 10:24
  • 1
    It seems simple, but then you have to keep in mind the fact that the text for a function definition could appear in a string or as a comment, so I wonder if that's not entering "parse HTML with regex" territory. – TigerhawkT3 Nov 24 '15 at 10:26
  • There's quite some regex Q&A here on StackOverflow but nothing related to complex code introspection. The Java one is not very applicable because of the accolades vs indentation. – Freek Wiekmeijer Nov 24 '15 at 10:26
  • @TigerhawkT3: It doesn;t even seem that simple because the end of a method is not fully defined by the indentation dropping. You could have multiline comments inside of the function body and also blank lines with any amount of whitespace. – Freek Wiekmeijer Nov 24 '15 at 10:30
  • Oh, you want the entire function body? I thought you just meant the signature, e.g. `def fun(x)`. Yeah, don't use a regex for that. Use the proper modules as stated in the answer. – TigerhawkT3 Nov 24 '15 at 14:00

1 Answers1

3

Err, you don't want to use regexes to parse Python. The 'nicer way for code analysis without execution' is to use the Python standard library parser and/or ast modules. Look under the heading Python Language Services, e.g. https://docs.python.org/2/library/language.html