Skip to main content

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",
)

Metadata

Release files for presearch 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for presearch 0.0.1
File Size Uploaded Provenance Actions
presearch-0.0.1.tar.gz 18.6 kB Without provenance Details for presearch-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for presearch 0.0.1
File Interpreter ABI Platform Actions
presearch-0.0.1-py3-none-any.whl Python 3 none any Details for presearch-0.0.1-py3-none-any.whl

Total release size: 38.1 kB

Release files / presearch-0.0.1.tar.gz

Download URL presearch-0.0.1.tar.gz
Size 18.6 kB
Tags Source
SHA-256 checksum
How to use checksums
089668d42fc12de28fe0c58be503d319b51f23ecd38b9ee4e736ae6bd9d9fdb4
BLAKE2b-256 checksum
How to use checksums
efece636a3ed44763ecba3653fc71afca75cf536676a21993cbe6ec09b240bc2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.2

Release files / presearch-0.0.1-py3-none-any.whl

Download URL presearch-0.0.1-py3-none-any.whl
Size 19.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
020c1ddcac780724cfb08a4a37dc06895c4e18893d9c6202d2298cfd4dacb52e
BLAKE2b-256 checksum
How to use checksums
10cfb33cdcd3ba0c6c149bf54aea18f49974f47021efe0932e1a18be3aafbe48
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.2

Security for presearch 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page