Skip to main content

Find the Python code for specified symbols

Project description

symbex

PyPI Changelog Tests License

Find the Python code for specified symbols

Installation

Install this tool using pip:

pip install symbex

Usage

symbex can search for names of functions and classes that occur at the top level of a Python file.

To search every .py file in your current directory and all subdirectories, run like this:

symbex my_function

You can search for more than one symbol at a time:

symbex my_function MyClass

Wildcards are supported - to search for every test_ function run this (note the single quotes to avoid the shell interpreting the * as a wildcard):

symbex 'test_*'

To search for methods within classes, use class.method notation:

symbex Entry.get_absolute_url

Wildcards are supported here as well:

symbex 'Entry.*'
symbex '*.get_absolute_url'
symbex '*.get_*'

Or to view every method of every class:

symbex '*.*'

To search within a specific file, pass that file using the -f option. You can pass this more than once to search multiple files.

symbex MyClass -f my_file.py

To search within a specific directory and all of its subdirectories, use the -d option:

symbex Database -d ~/projects/datasette

If symbex encounters any Python code that it cannot parse, it will print a warning message and continue searching:

# Syntax error in path/badcode.py: expected ':' (<unknown>, line 1)

Pass --silent to suppress these warnings:

symbex MyClass --silent

Example output

In a fresh checkout of Datasette I ran this command:

symbex MessagesDebugView get_long_description

Here's the output of the command:

# File: setup.py Line: 5
def get_long_description():
    with open(
        os.path.join(os.path.dirname(os.path.abspath(__file__)), "README.md"),
        encoding="utf8",
    ) as fp:
        return fp.read()

# File: datasette/views/special.py Line: 60
class PatternPortfolioView(View):
    async def get(self, request, datasette):
        await datasette.ensure_permissions(request.actor, ["view-instance"])
        return Response.html(
            await datasette.render_template(
                "patterns.html",
                request=request,
                view_name="patterns",
            )
        )

Just the signatures

The -s/--signatures option will list just the signatures of the functions and classes, for example:

symbex -s -d symbex
# File: symbex/cli.py Line: 37
def cli(symbols, files, directories, signatures, silent)

# File: symbex/lib.py Line: 150
def class_definition(class_def)

# File: symbex/lib.py Line: 32
def code_for_node(code: str, node: AST, class_name: str, signatures: bool)

# File: symbex/lib.py Line: 63
def match(name: str, symbols) -> bool

# File: symbex/lib.py Line: 8
def find_symbol_nodes(code: str, symbols)

# File: symbex/lib.py Line: 88
def function_definition(function_node: AST)

This can be combined with other options, or you can run symbex -s to see every symbol in the current directory and its subdirectories.

Using with LLM

This tool is primarily designed to be used with LLM, a CLI tool for working with Large Language Models.

symbex makes it easy to grab a specific class or function and pass it to the llm command.

For example, I ran this in the Datasette repository root:

symbex Response | llm --system 'Explain this code, succinctly'

And got back this:

This code defines a custom Response class with methods for returning HTTP responses. It includes methods for setting cookies, returning HTML, text, and JSON responses, and redirecting to a different URL. The asgi_send method sends the response to the client using the ASGI (Asynchronous Server Gateway Interface) protocol.

Development

To contribute to this tool, first checkout the code. Then create a new virtual environment:

cd symbex
python -m venv venv
source venv/bin/activate

Now install the dependencies and test dependencies:

pip install -e '.[test]'

To run the tests:

pytest

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

symbex-0.3.1.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

symbex-0.3.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file symbex-0.3.1.tar.gz.

File metadata

  • Download URL: symbex-0.3.1.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for symbex-0.3.1.tar.gz
Algorithm Hash digest
SHA256 76bc1ca57153349ab34bbaa5205a72d24cc843e048c844c5b03e85ccae124343
MD5 1f4128bd0f2186dc1ae8d27777fbba0f
BLAKE2b-256 8d47c5f08d6a041c25e827c7e8338d49757f584ac947b1a8f0727b540c8d07d0

See more details on using hashes here.

File details

Details for the file symbex-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: symbex-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for symbex-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 31efad77e4483f75fc844d8f834cb49751882eb1e46361e13c8f6eacc5079a85
MD5 7317d6c3fe23507ad09af1649d3cf4de
BLAKE2b-256 1d87efb1a1ab97b606825f79603a78ada4d3e44508f4437ce5d73380903d69a6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page