Skip to main content

A way to get the source code of functions

Project description

Downloads Downloads Coverage Status Lines of code Hits-of-Code Test-Package Python versions PyPI version Checked with mypy Ruff DeepWiki

logo

This library is needed to obtain the source code of functions at runtime. It can be used, for example, as a basis for libraries that work with AST on the fly. In fact, it is a thin layer built around inspect.getsource and dill.source.getsource.

Installation

You can install getsources using pip:

pip install getsources

You can also quickly try out this and other packages without having to install using instld.

Usage

The basic function of the library is getsource, which works similarly to the function of the same name from the standard library:

from getsources import getsource

def function():
    ...

print(getsource(function))
#> def function():
#>     ...

Unlike its counterpart from the standard library, this thing can also work:

  • With lambda functions
  • With functions defined inside REPL

We also often need to trim excess indentation from a function object to make it easier to further process the resulting code. To do this, use the getclearsource function:

from getsources import getclearsource

class SomeClass:
    @staticmethod
    def method():
        ...

print(getclearsource(SomeClass.method))
#> def method():
#>     ...

As you can see, the resulting source code text has no extra indentation, but in all other respects this function is completely identical to the usual getsource.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

getsources-0.0.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

getsources-0.0.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: getsources-0.0.1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for getsources-0.0.1.tar.gz
Algorithm Hash digest
SHA256 43a72c19df1fd7a438c038ba2cde909e1cdaec61229a055c284a1124054767bd
MD5 182e9157d9a42c353dd8c15ddadb9a93
BLAKE2b-256 dc2569d6f26e7da6524ded85b60efa546785fc808a8e610798f67d7e0c3b37fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for getsources-0.0.1.tar.gz:

Publisher: release.yml on mutating/getsources

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: getsources-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for getsources-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 892712d83aa1bd997f241fc168d7bdb03846c1f7e281f9278d9c67c15cace5cc
MD5 52df813fbacff8bfac8add2c98329527
BLAKE2b-256 ffa2e4238c5981acd100af3469502b642a7612cff15e7a7f4e0a797012154598

See more details on using hashes here.

Provenance

The following attestation bundles were made for getsources-0.0.1-py3-none-any.whl:

Publisher: release.yml on mutating/getsources

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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