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.2.tar.gz (7.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.2-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for perg_search-0.0.2.tar.gz
Algorithm Hash digest
SHA256 bc4233f8a8b3986a637a7605ddb408a6e9fd2cd16dcd9fd573466be5ce7255d6
MD5 e87a7c0b8ff7bdc3fbfa9e273ff527f2
BLAKE2b-256 940d368772ad59567a76b8defe2c66c2ccad00619910a9c496912f96b0ceafbd

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for perg_search-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d1a28e116023468bd43df744aec52c187e16f7bbc81fca799aaf083ac099490b
MD5 d4a8806309181fcef271d168bca1a6c8
BLAKE2b-256 69951603878f604ccc9ff4aeb2df5cc13e8d354e3c32018bc4f6fce6998c824a

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