Skip to main content

A tool to syntactically query Python source code

Project description

Presearch

A Python program to syntactically query Python source code

Instalation

  1. Clone the repository by running git clone https://github.com/dignissimus/presearch
  2. Install the requirements using pip install -r requirements.txt

Usage

Currently the program can be run by executing python -m presearch

usage: presearch [-h] --file FILE directory

Syntactically query python source code

positional arguments:
  directory             The directory containing the source code to query

options:
  -h, --help            show this help message and exit
  --file FILE, -f FILE  The file containing query to execute

Examples

Finding files that import the ast library

The following query matches files that import the ast library

from presearch.query import MatchQuery

# Matches files that import the `ast` library
query = MatchQuery(lambda module: module.imports("ast"))

Finding classes that explicitly define __init__

The following query searches for class definitions, then reports back with the number of those class definitions that explicitly define __init__.

from presearch.tree import ClassDef
from presearch.query import Domain, StatisticalQuery

# Calculates the percentage of classes that define `__init__`
query = StatisticalQuery(
    lambda klass: klass.defines("__init__"),
    domain=Domain(ClassDef),
    domain_description="class definitions",
    match_description="classes that explicitly define __init__",
)

Finding __init__ definitions that directly store all of their non-self arguments as attributes

This query finds all classes that define __init__ then reports the number of __init__ definitions that assign all of its arguments to attributes (i.e. run self.argument = argument for all arguments)

from presearch.query import Domain, StatisticalQuery
from presearch.constraints import ContainsMethodDefinition
from presearch.tree import ClassDef, Self


def assigns_all_arguments_to_attributes(class_def):
    init_function = class_def.function("__init__")
    for argument in init_function.non_self_arguments:
        if not init_function.contains(Self.attribute(argument.name).assign(argument)):
            return False

    return True


# Calculates the proportion of class `__init__` definitions
# that assign all their non-self arguments as attributes
query = StatisticalQuery(
    assigns_all_arguments_to_attributes,
    domain=Domain(ClassDef, constraints=[ContainsMethodDefinition("__init__")]),
    domain_description="classes defining __init__",
    match_description="classes whose __init__ functions assigned all non-self arguments as attributes",
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

presearch-0.0.1.tar.gz (18.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

presearch-0.0.1-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file presearch-0.0.1.tar.gz.

File metadata

  • Download URL: presearch-0.0.1.tar.gz
  • Upload date:
  • Size: 18.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.2

File hashes

Hashes for presearch-0.0.1.tar.gz
Algorithm Hash digest
SHA256 089668d42fc12de28fe0c58be503d319b51f23ecd38b9ee4e736ae6bd9d9fdb4
MD5 c50f6e4d00311ec20a8d0d70ecba63e0
BLAKE2b-256 efece636a3ed44763ecba3653fc71afca75cf536676a21993cbe6ec09b240bc2

See more details on using hashes here.

File details

Details for the file presearch-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: presearch-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 19.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.2

File hashes

Hashes for presearch-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 020c1ddcac780724cfb08a4a37dc06895c4e18893d9c6202d2298cfd4dacb52e
MD5 914e0133fd6c4a282e379df7c075057a
BLAKE2b-256 10cfb33cdcd3ba0c6c149bf54aea18f49974f47021efe0932e1a18be3aafbe48

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page