Skip to main content

Split top-level Python functions into dedicated modules and rewire imports automatically.

Project description

image

ManaSplice

ManaSplice is a small CLI for pulling top-level Python functions into their own files without leaving the original module broken.

It creates a modules/ package next to the source file, moves the function there, and rewrites the source module to import it back in.

What it does

  • Splits one top-level function with splitfunc.
  • Splits every top-level function in a file, or every top-level function in each Python file in a directory, with splitall.
  • Checks whether a split is safe, and shows the planned changes without writing files, with check.
  • Copies the imports and top-level definitions the extracted function needs.
  • Maintains modules/__init__.py so rewritten files can use a single merged import line.

Quick example

Starting point:

import math


def area(radius: float) -> float:
    return math.pi * radius * radius


def greet(name: str) -> str:
    return f"Hello, {name}!"

Run:

uv run manasplice splitfunc main.area

Result:

import math
from modules import area


def greet(name: str) -> str:
    return f"Hello, {name}!"
"""Auto-generated by ManaSplice from main.py."""

import math


def area(radius: float) -> float:
    return math.pi * radius * radius

Installation

For using in a project:

uv add manasplice

or

pip install manasplice

For local development in this repo:

uv sync

To run the CLI without installing it globally:

uv run manasplice --help

Commands

manasplice splitfunc <module>.<function>
manasplice splitall <path/to/file.py>
manasplice splitall --dir <directory>
manasplice check <module>.<function>
manasplice check <path/to/file.py>
manasplice check --dir <directory>
manasplice undo [count]
manasplice splitfunc <module>.<function> --preview
manasplice splitall <path/to/file.py> --preview
manasplice splitfunc <module>.<function> --validate
manasplice splitall <path/to/file.py> --public-only --exclude main,_*
manasplice splitfunc <module>.<function> --output-package generated
manasplice splitfunc <module>.<function> --force

Examples:

uv run manasplice splitfunc main.area
uv run manasplice splitfunc package.utils.normalize_name
uv run manasplice splitall main.py
uv run manasplice splitall --dir app
uv run manasplice check main.area
uv run manasplice check main.py --public-only --exclude main,_*
uv run manasplice splitall main.py --preview
uv run manasplice splitall main.py --public-only --exclude main,_*
uv run manasplice splitfunc main.area --validate
uv run manasplice splitfunc main.area --output-package generated
uv run manasplice undo

Notes

  • ManaSplice only moves top-level functions.
  • splitall is literal: it will split every top-level function it finds, including helper functions and main() if present.
  • Use check for a no-write safety report without diffs.
  • Use --preview to inspect planned edits with a safety report and unified diffs before ManaSplice writes anything.
  • Use --validate to make ManaSplice parse the generated Python source before it writes changes.
  • Use --include, --exclude, and --public-only to make splitall selective instead of splitting every top-level function.
  • Use --output-package if you want generated modules somewhere other than modules/.
  • ManaSplice refuses to overwrite an existing generated module unless you pass --force.
  • ManaSplice refuses splits that depend on mutable top-level globals, because copying those globals into a new module changes runtime state.
  • ManaSplice now refuses to split functions that participate in a local top-level dependency cycle, such as simple mutual recursion.
  • Every non-preview split records rollback history in .manasplice_history.json, and undo replays the last recorded operation.
  • The generated modules/ package is part of the rewritten code, not just scratch output.

Development

Run tests with:

uv run python -m pytest tests --basetemp .pytest_tmp
uv run ruff check src tests
uv run mypy

CONTRIBUTION

Feel free to give feedback and/or suggest changes. This is just meant to be a helpful tool for larger projects made for fun.

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

manasplice-0.1.0.1a0.tar.gz (99.7 kB view details)

Uploaded Source

Built Distribution

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

manasplice-0.1.0.1a0-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

Details for the file manasplice-0.1.0.1a0.tar.gz.

File metadata

  • Download URL: manasplice-0.1.0.1a0.tar.gz
  • Upload date:
  • Size: 99.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for manasplice-0.1.0.1a0.tar.gz
Algorithm Hash digest
SHA256 349b57fe18f4e962eb9eb6233231574c7bb35ebb341d4d7c0d0d786d2f5a26d1
MD5 8d84486177213611e7e79f3959e1a9fe
BLAKE2b-256 b95b9da8aa5099fb01f7f8504fdf5f8a4bdfbe9c75010edfe842ec58a80782a9

See more details on using hashes here.

File details

Details for the file manasplice-0.1.0.1a0-py3-none-any.whl.

File metadata

  • Download URL: manasplice-0.1.0.1a0-py3-none-any.whl
  • Upload date:
  • Size: 22.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for manasplice-0.1.0.1a0-py3-none-any.whl
Algorithm Hash digest
SHA256 de7559449ca4910c9e506406934024a277107cbad143369040d08c5e60bf4007
MD5 bfbef5c268e6fc61c2d5175f27a7259b
BLAKE2b-256 681e9380020ac91b9473e50ac44d58aff06c984fef8a740f260ecd0e6eaff3f5

See more details on using hashes here.

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