0

trying to find a python library or to find the best way to find methods from python code files

for example, app.py

from django.conf.urls import url
from oscar.core.loading import get_class


class SearchApplication(Application):
    name = 'search'
    search_view = get_class('search.views', 'FacetedSearchView')
    search_form = get_class('search.forms', 'SearchForm')

    def get_urls(self):

        # The form class has to be passed to the __init__ method as that is how
        # Haystack works.  It's slightly different to normal CBVs.
        urlpatterns = [
            url(r'^$', search_view_factory(
                view_class=self.search_view,
                form_class=self.search_form,
                searchqueryset=self.get_sqs()),
                name='search'),
        ]
        return self.post_process_urls(urlpatterns)

and my aim is to write code that takes that file app.py as input as text or as file(either way is fine) and outputs something like this:

{
   "methods": [
        {"name": "get_urls", "class": "SearchApplication", "line": 9, "args": [self], "kwargs": []}
     ],

  "classs": [
    {"name": "SearchApplication", "inherits_from": "Application", "line": 5}
  ]   
}

thanks. please ask if the intention is unclear or if the question is missing data.

4
  • Take a look at ast.parse. Commented Aug 15, 2018 at 15:44
  • If you're working only with "live" objects (i.e. active parts of the current program), then the Python inspect package will likely serve your needs nicely. "Live" in this case would mean that you had imported the package. Commented Aug 15, 2018 at 16:01
  • @Aran-Fey i've used ast.parse thanks. Suggest it in an anwer, and i'll accept. Commented Aug 21, 2018 at 19:02
  • I won't be posting an answer, because writing the code to extract all that information about all functions is non-trivial and I don't want to write it. You can post your own answer if you'd like. Commented Aug 21, 2018 at 19:04

1 Answer 1

1

used ast.parse

    file_content = open('/path_to_file', 'r').read()
    parsed_result = ast.parse(self.file_content)
    for element in parsed_result.body:
        results = self.index_element(element)


def index_element(self, element, class_name=None):
    '''

        if element is relevant, meaning method -> index
        if element is Class -> recursively call it self

    :param element:
    :param class_name:
    :return: [{insert_result: <db_insert_result>, 'structured_data': <method> object}, ...]
    '''
    # find classes
        # find methods inside classes
    # find hanging functions

    # validation on element type
    if self.should_index_element(element):
        if self.is_class_definition(element):
            class_element = element
            indexed_items = []
            for inner_element in class_element.body:
                # recursive call
                results = self.index_element(inner_element, class_name=class_element.name)
                indexed_items += results

            return indexed_items
        else:
            structured_data = self.structure_method_into_an_object(element, class_name=class_name)
            result_graph = self.dal_client.methods_graph.insert_or_update(structured_data)
            return "WhatEver"

    return "WhatEver"

element object has properties of the function/class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.