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))
#> @staticmethod
#> 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.2.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.2-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: getsources-0.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 ed83fb44b654f8a257f552be400430502353a52b03e88cd4568996405862d921
MD5 0c3f5eb31ae4c87feb15678a71510307
BLAKE2b-256 04a8ec2a280de2f2de1fc7dd7b4d6356be2576895141cf9790bccc146d55a2f0

See more details on using hashes here.

Provenance

The following attestation bundles were made for getsources-0.0.2.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.2-py3-none-any.whl.

File metadata

  • Download URL: getsources-0.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0639f7c7237830be40bf0e1fad48b7a365db1d741552e23d4b9b62f83110cb62
MD5 da3450d92a9cf5771689e9cbcdb4ea8f
BLAKE2b-256 6167531d79f148369c52b14936e62c1f80830e764c18285666951d4395a451f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for getsources-0.0.2-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