Skip to main content

Search code for patterns that match a given string

Project description

perg: like grep, but backwards

grep finds all the strings in your files that match a given pattern.

perg finds all the patterns in your files that match a given string.

Patterns may be regexes, globs, or other pattern-matching languages. Patterns may also be format strings or templates.

Examples

Webapp routes

Ever forget where you defined the route that handles that URL?

@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
    return {"item_id": item_id, "q": q}

perg '/items/12345' would find the @app.get line.

Log entries

Trying to figure out where that log line comes from?

perg "User Alice performed a login action." would find this code:

logging.info(f"User {user} performed a {action} action.")

Won't this just find every .*, ., %, or * in the codebase?

By default, perg uses simple heuristics to ignore matches where the pattern would also match the empty string or any single byte. This gets rid of most of the pointless matches.

Additionally, for each match, perg attempts various edits to your test string to decide how much information in your test string is implied by the pattern.

For the string foo bar baz, the following regexes are sorted from most to least informative:

  • foo bar baz
  • foo [a-z]* baz
  • foo [a-zA-Z]* baz
  • foo .* baz
  • foo .*
  • ...........
  • .*

Anything that scores worse than 50% as strict as the best match is hidden by default. This threshold can be adjusted with --pct-of-best-score, and this behavior can be disabled entirely with --no-score-by-information.

(Additional details can be seen in the docstring for information() in heuristics.py.)

Language support

For each programming language perg supports, there is a module in syntaxes/.

Currently supported languages:

  • python

Each syntax module must have a method parse(f, filename). This method will be passed the file object and filename of each source file. It should return an iterator of Pattern objects.

A Pattern object contains:

  • The location of the pattern (filename, starting & ending line numbers and columns)
  • A set of check functions which determine whether the given string matches the pattern.

This allows a single pattern to be checked against several different pattern languages. For example, a string literal found in your source code could be tested as a format string, a regex, a glob, a SQL pattern, and for string equality.

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

perg_search-0.0.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

perg_search-0.0.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: perg_search-0.0.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for perg_search-0.0.1.tar.gz
Algorithm Hash digest
SHA256 02c4c94d2f1950488d61fff721c8f528a9879568c0637c59f7f22723bfbfab41
MD5 7c91fb1080e2abae41551f18c5d9dd47
BLAKE2b-256 66580cac405947bf91dc7ee37486f5cb42de3c557000a519936bf825e29608cc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: perg_search-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for perg_search-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b881f95d7e5e7db8ffa982e2e3479b2e66de95a30aae6052b794a1b8b24d9997
MD5 82a5b3e70521e6521cbcbc6b0d938b47
BLAKE2b-256 e59f8666511fbf2a4572b786a10d6d920e72bd3cbb0aeb25962dd95a7dbf7443

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