Skip to main content

Wrap and unwrap code to allow for modules without top-level code

Project description

no_toplevel_code

Package to wrap a piece of python code in a function and function call, to avoid running code at the top level of a script.

Example

E.g., if you have a script that looks like this:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

plt.plot(x, y)
plt.show()

no_toplevel_code converts it to this:

import numpy as np
import matplotlib.pyplot as plt

def _main():
    x = np.linspace(0, 2*np.pi, 100)
    y = np.sin(x)

    plt.plot(x, y)
    plt.show()

_main()

Interface

This package provides two functions: wrap_code and unwrap_code as well as their equivalent functions wrap_ast and unwrap_ast. The former functions take a string as input, while the latter functions take an AST (Abstract Syntax Tree) as input.

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

no-toplevel-code-1.0.0.tar.gz (2.9 kB view details)

Uploaded Source

File details

Details for the file no-toplevel-code-1.0.0.tar.gz.

File metadata

  • Download URL: no-toplevel-code-1.0.0.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.65.0 CPython/3.8.8

File hashes

Hashes for no-toplevel-code-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1e7d33fa2f4567aba616b4b2a7cf069e8902c3c826afe8268e003a0351aa66a8
MD5 d12e22b4bce3134d325a5cdec6c8afd4
BLAKE2b-256 4cd580b10e7ec7676a2d25ae1890dff817effa913c33b2345538fc3e3961e254

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