Skip to main content

minimize python source code

Project description

pypi version Python Versions PyPI - Downloads GitHub Sponsors

pysource-minimize

If you build a linter, formatter or any other tool which has to analyse python source code you might end up searching bugs in pretty large input files.

pysource_minimize is able to remove everything from the python source which is not related to the problem.

CLI

You can use pysource-minimize from the command line like follow:

pysource-minimize --file bug.py --track "Assertion" -- python bug.py

This executes python bug.py and tries to find the string “Assertion” in the output. The ---file bug.py will be minimized as long as “assertion” is part of the output of the command. The --file option can be specified multiple times and there is also an --dir option which can be used to search directories recursively for Python files.

[!WARNING] Be careful when you execute code which gets minimized. It might be that some combination of the code you minimize erases your hard drive or does other unintended things.

example

API

Example for single files:

>>> from pysource_minimize import minimize

>>> source = """
... def f():
...     print("bug"+"other string")
...     return 1+1
... f()
... """

>>> print(minimize(source, lambda new_source: "bug" in new_source))
"""bug"""

This example minimizes multiple files and searches for sets which have 2 common elements:

>>> from pathlib import Path
>>> from typing import Dict
>>> from pprint import pprint
>>> from pysource_minimize._minimize import minimize_all
>>> sources = {
...     Path(
...         "a.py"
...     ): """\
... l={1,81894,9874,89228,897985,897498,9879,9898}
...     """,
...     Path(
...         "b.py"
...     ): """\
... l={5,81894,9274,89218,897985,897298,9879,9898}
...     """,
...     Path(
...         "c.py"
...     ): """\
... l={0,81894,9874,89218,897985,897498,9879,9298}
...     """,
... }
>>> def check(sources: Dict[Path, str | None], current_filename: Path) -> bool:
...     # current_filename can be used for progress output
...     # print(f"working on {current_filename} ...")
...     sets = []
...     for source in sources.values():
...         if source is not None:
...             globals = {}
...             try:
...                 exec(source, globals)
...             except:
...                 return False
...             if "l" not in globals:
...                 return False
...             sets.append(globals["l"])
...     return (
...         len(sets) >= 2
...         and all(isinstance(s, set) for s in sets)
...         and len(set.intersection(*sets)) >= 2
...     )
...
>>> pprint(minimize_all(sources, checker=check))
{PosixPath('a.py'): None,
 PosixPath('b.py'): 'l = {81894, 0}',
 PosixPath('c.py'): 'l = {0, 81894}'}

You might think that there are no two zeros in the original sets. This problem can occur if your check function is not specific enough. pysource-minimize tries to minimize numbers and strings, does so for one of the sets and finds that it satisfies your check. It generates new code during the minimization and can only use the check function to know if the solution is correct. This kind of problem can be solved by using a more precise check function or a --track argument when using the CLI. For example, you can add a check that all numbers in the set must be non-zero. However, this problem will not occur if you are looking for real minimal examples that throw certain exceptions. The worst that can happen here is that pysource-minimize finds another example that triggers the same problem.

fixed check function
>>> from pathlib import Path
>>> from typing import Dict
>>> from pprint import pprint
>>> from pysource_minimize._minimize import minimize_all
>>> sources = {
...     Path(
...         "a.py"
...     ): """\
... l={1,81894,9874,89228,897985,897498,9879,9898}
...     """,
...     Path(
...         "b.py"
...     ): """\
... l={5,81894,9274,89218,897985,897298,9879,9898}
...     """,
...     Path(
...         "c.py"
...     ): """\
... l={0,81894,9874,89218,897985,897498,9879,9298}
...     """,
... }
>>> def check(sources: Dict[Path, str | None], current_filename: Path) -> bool:
...     # current_filename can be used for progress output
...     # print(f"working on {current_filename} ...")
...     sets = []
...     for source in sources.values():
...         if source is not None:
...             globals = {}
...             try:
...                 exec(source, globals)
...             except:
...                 return False
...             if "l" not in globals:
...                 return False
...             sets.append(globals["l"])
...     return (
...         len(sets) >= 2
...         and all(isinstance(s, set) for s in sets)
...         and len(result := set.intersection(*sets)) >= 2
...         and 0 not in result
...     )
...
>>> pprint(minimize_all(sources, checker=check))
{PosixPath('a.py'): None,
 PosixPath('b.py'): 'l = {81894, 89218}',
 PosixPath('c.py'): 'l = {81894, 89218}'}

Sponsors

I would like to thank my sponsors. Without them, I would not be able to invest so much time in my projects. If you want, you can also support my work.

Bronze sponsor 🥉

pydantic logfire

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

pysource_minimize-0.10.1.tar.gz (9.3 MB view details)

Uploaded Source

Built Distribution

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

pysource_minimize-0.10.1-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file pysource_minimize-0.10.1.tar.gz.

File metadata

  • Download URL: pysource_minimize-0.10.1.tar.gz
  • Upload date:
  • Size: 9.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pysource_minimize-0.10.1.tar.gz
Algorithm Hash digest
SHA256 05000b5174207d10dbb6da1a67a6f3a6f61d295efa17e3c74283f0d9ece6401c
MD5 5fbd88969f9bfbd19210ce44d1e1da21
BLAKE2b-256 03e614136d4868c3ea2e46d7f83faef26d07fd9231b7bd3fc811b6b6f8688cf2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pysource_minimize-0.10.1.tar.gz:

Publisher: create-release.yml on 15r10nk/pysource-minimize

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pysource_minimize-0.10.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pysource_minimize-0.10.1-py3-none-any.whl
Algorithm Hash digest
SHA256 69b41fd2f0c1840ca281ec788c6ffb4e79680b06c7cc0e7c4d9a75321654b709
MD5 0886c51123444fcb40304adc86dfcd5e
BLAKE2b-256 f5de847456f142124b242933a1cea17aea7041d297f1ac99b411c5dee69ac250

See more details on using hashes here.

Provenance

The following attestation bundles were made for pysource_minimize-0.10.1-py3-none-any.whl:

Publisher: create-release.yml on 15r10nk/pysource-minimize

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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