
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
A Python wrapper for ripgrep, providing fast and efficient text searching capabilities.
python-ripgrep is a Python package that wraps the functionality of ripgrep, a line-oriented search tool that recursively searches directories for a regex pattern. This package allows you to harness the power and speed of ripgrep directly from your Python code.
You can install python-ripgrep using pip:
pip install python-ripgrep
Here's a basic example of how to use python-ripgrep:
from python_ripgrep import search
# Perform a simple search, returning a
# list of string results grouped by file.
results = search(
patterns=["pattern"],
paths=["path/to/search"],
globs=["*.py"],
)
# Process the results
for result in results:
print(result)
The main components of python-ripgrep are:
search
: The primary function for performing searchesfiles
: A function for listing files that would be searched (--files equivalent)PySortMode
and PySortModeKind
: Enums for specifying sort modesFor detailed API documentation, please refer to the source code comments.
Unlike many other ripgrep bindings for Python, python-ripgrep doesn't shell out to the ripgrep command-line tool. Instead, it reimplements core ripgrep logic in Rust and provides a direct interface to Python. This approach offers several advantages:
Performance: By avoiding the overhead of creating a new process and parsing stdout, this implementation can be more efficient, especially for large-scale searches or when called frequently.
Fine-grained control: The library can expose more detailed control over the search process and return structured data directly to Python.
Better integration: It allows for tighter integration with Python code, making it easier to incorporate into larger Python applications.
As of now, the library implements a subset of ripgrep's functionality. The main search options currently supported are:
patterns
: The search patterns to usepaths
: The paths to search inglobs
: File patterns to include or excludesort
: Sort mode for search resultsmax_count
: Maximum number of matches to showThe following is a checklist of ripgrep flags that have been implemented in this Python wrapper:
patterns
: Search patternspaths
: Paths to search (default: current directory)globs
: File patterns to include or exclude (default: all non-ignored files)heading
: (Optional) Whether to show file names above matching linessort
: (Optional) Sort mode for search resultsmax_count
: (Optional) Maximum number of matches to show per fileafter_context
: (Optional) Number of lines to show after each matchbefore_context
: (Optional) Number of lines to show before each matchseparator_field_context
: (Optional) Separator between fields in context linesseparator_field_match
: (Optional) Separator between fields in matching linesseparator_context
: (Optional) Separator between context linesThe following flags from ripgrep are not yet implemented in this wrapper:
-C, --context
: Show lines before and after each match--color
: Controls when to use color in output-c, --count
: Only show the count of matching lines--debug
: Show debug messages--dfa-size-limit
: Limit for regex DFA size-E, --encoding
: Specify the text encoding of files to search-F, --fixed-strings
: Treat patterns as literal strings-i, --ignore-case
: Case insensitive search-v, --invert-match
: Invert matching-n, --line-number
: Show line numbers-x, --line-regexp
: Only show matches surrounded by line boundaries-M, --max-columns
: Don't print lines longer than this limit--mmap
: Memory map searched files when possible-U, --multiline
: Enable matching across multiple lines--no-ignore
: Don't respect ignore files--no-unicode
: Disable Unicode-aware search-0, --null
: Print NUL byte after file names-o, --only-matching
: Print only matched parts of a line--passthru
: Print both matching and non-matching lines-P, --pcre2
: Use the PCRE2 regex engine-p, --pretty
: Alias for --color=always --heading -n-r, --replace
: Replace matches with the given text-S, --smart-case
: Smart case search-s, --case-sensitive
: Case sensitive search--stats
: Print statistics about the search-a, --text
: Search binary files as if they were text-t, --type
: Only search files matching TYPE-T, --type-not
: Do not search files matching TYPE-u, --unrestricted
: Reduce the level of "smart" searching-V, --version
: Print version information-w, --word-regexp
: Only show matches surrounded by word boundaries-z, --search-zip
: Search in compressed filesNote that this list may not be exhaustive and some flags might have partial implementations or behave differently from the original ripgrep. Refer to the source code for the most up-to-date information on implemented features.
To add more ripgrep options to the library, you'll need to modify both the Rust and Python sides of the codebase:
PyArgs
struct in src/ripgrep_core.rs
to include the new option.pyargs_to_hiargs
function in the same file to convert the new Python argument to the corresponding ripgrep argument.For example, to add a new option case_sensitive
:
Add to PyArgs
:
pub case_sensitive: Option<bool>,
In pyargs_to_hiargs
, add:
if let Some(case_sensitive) = py_args.case_sensitive {
low_args.case_sensitive = case_sensitive;
}
Update the Python wrapper to include the new option.
Remember to handle any necessary type conversions between Python and Rust in the pyargs_to_hiargs
function.
This project uses maturin for building the Python package from Rust code. To set up a development environment:
pip install maturin
maturin develop
to build and install the package locallyContributions are welcome! Please feel free to submit a Pull Request.
[License information to be added]
This project is based on ripgrep by Andrew Gallant.
This project is built and maintained by the Exponent team.
FAQs
A Python wrapper for ripgrep
We found that python-ripgrep demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.