Skip to main content

codegrab

PyPI Changelog License

codegrab

codegrab is a command-line tool that allows you to extract Python module or function code from local files or directly from a GitHub repository. It concatenates all files fetched to prepare for feeding into LLMs.

Features

  • Retrieve an entire Python module locally or from a GitHub repository.
  • Extract specific function definitions, including decorators.
  • Supports specifying a GitHub repository and branch to fetch files remotely.
  • Concats all results.

Why this is useful

By fetching code and concatting it into a string, you can feed it into tools like Simon Willison's command-line tool llm.

The following will take one function from the specified code base, compare it to another, and use OpenAI to give feedback on why one works and not the other by piping it into the llm command.

uv run codegrab module.location:function --repo [private repo] \
 | uv run codegrab other_module.location:function --repo [private repo] \
 | llm --system "The first function does not work, the second one does. The first one fails when I supply stats=['mean'] whereas the second one successfully includes stats. Can you see what the difference could be? Can you suggest changes to the first implementation so that it accepts stats in the same way the second one does?"

Fetching code as a string could also be used to load a prompt with lots of examples to show it how to write domain specific code and so on.

Installation

pip install codegrab

Alternatively, you can run it without installing using uv:

uvx codegrab

Usage

Extract a Local Python Module

To retrieve the full code of a module from your local file system:

codegrab module.submodule

Fetch from a GitHub Repository

If the --repo option is provided, CodeGrab fetches the file from a GitHub repository instead of the local system:

codegrab module.submodule --repo https://github.com/user/repo

By default, CodeGrab fetches from the repository's default branch. You can specify a different branch using the --branch option:

codegrab module.submodule --repo https://github.com/user/repo --branch dev

Example:

Download and concat the LLM module from Simon Willison's command-line llm tool.

codegrab llm --repo https://github.com/simonw/llm

Extract a Specific Function

To retrieve only the code for a specific function within a module:

codegrab module.submodule:function_name

Example:

codegrab llm.cli:prompt --repo https://github.com/simonw/llm

This will retreive the prompt method from the llm repository.

Environment Variables

To avoid GitHub rate limiting, you can set a personal access token:

export GITHUB_TOKEN=your_personal_token

Error Handling

  • If the file or function is not found, an error message is displayed.
  • If the GitHub request fails, an HTTP error code is shown.

License

MIT License

Contributions

Feel free to open issues and submit pull requests to improve codegrab!

Metadata

Release files for codegrab 0.1.2

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

Source distribution (sdist)

Source distribution for codegrab 0.1.2
File Size Uploaded Provenance
codegrab-0.1.2.tar.gz 7.8 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 16.8 kB

Release files / codegrab-0.1.2.tar.gz

Download URL codegrab-0.1.2.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
258bda8770e8d107d5f86e007085fc46c07129f71fbeb199971df9a2918d9a30
BLAKE2b-256 checksum
How to use checksums
86687e5ad7bcdbd6691a67e0a614f01454ae6cf51978205fa190847b6489f195
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.1

Release files / codegrab-0.1.2-py3-none-any.whl

Download URL codegrab-0.1.2-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5a1c6a344ef783f7bfe6cf26b0326e21b9a236b05f8281a1773e51ea10260da6
BLAKE2b-256 checksum
How to use checksums
7cfb51c3fb390f8b9f2158bc17cad30d4aa7f4fe2cf4feffdf4b28c4e3408d37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.1

Security for codegrab 0.1.2

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.

Release history Release notifications | RSS feed

This release

0.1.2 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