{"id":37773784,"url":"https://github.com/scottvr/fplit","last_synced_at":"2026-01-16T14:56:47.006Z","repository":{"id":269911296,"uuid":"908832055","full_name":"scottvr/fplit","owner":"scottvr","description":"A Python source code file-splitting tool that intelligently separates function calls from a single file into individual files while preserving setup context (global initialization, local prints, etc) .","archived":false,"fork":false,"pushed_at":"2025-02-20T10:12:18.000Z","size":86,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-20T10:35:33.607Z","etag":null,"topics":["code","demo","file","python","splitter","tests"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/scottvr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-27T04:42:29.000Z","updated_at":"2025-02-20T10:12:21.000Z","dependencies_parsed_at":null,"dependency_job_id":"4a328e42-d6c1-4802-bd71-0c90afedc4be","html_url":"https://github.com/scottvr/fplit","commit_stats":null,"previous_names":["scottvr/fplit"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/scottvr/fplit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scottvr%2Ffplit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scottvr%2Ffplit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scottvr%2Ffplit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scottvr%2Ffplit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/scottvr","download_url":"https://codeload.github.com/scottvr/fplit/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scottvr%2Ffplit/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28479405,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-16T11:59:17.896Z","status":"ssl_error","status_checked_at":"2026-01-16T11:55:55.838Z","response_time":107,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code","demo","file","python","splitter","tests"],"created_at":"2026-01-16T14:56:46.913Z","updated_at":"2026-01-16T14:56:46.981Z","avatar_url":"https://github.com/scottvr.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fplit\r\nfplit reads in Python source files containing one or more function calls and intelligently splits them into separate, standalone script files (e.g., to use as unit tests or examples.) \r\n\r\nNecessary context such as imports, boilerplate setup code for well-known libraries, related statements such as print(), debug, comments,\r\nand docstrings are detected via proximity and a string similarity threshold, and will be added as apropriate to the main execution block \r\nof each generated script file (one file per function).\r\n\r\nIf the generated files are not intended for execution (e.g., they are generated as docs or other reference purposes) the aforementioned \r\ncontext can be omitted by passing the --funcdefs-only option to fplit on the command-line.\r\n\r\nThe name 'fplit' is a combination of 'file' and 'split', with a typographical nod to the historical 'long s' (ſ) character; 'split' would have appeared as 'ſplit' in historical typography, which often looks like an 'f' to a modern day reader.\r\n\r\n## Overview\r\n\r\n`fplit` reads in Python source files containing one or more function _*calls*_ (typically in a `__main__` block or at module level) and splits them into separate, self-contained files. Necessary context for successful execution of the functions (such as imports, setup code, and related statements) is preserved from the source file and added to the main execution block of each generated script. \r\n\r\nOr, if the source contains function _*definitions*_, and the generated files are not intended for execution, this context can be skipped via the `--funcdefs-only` option.\r\n\r\n### Key Features\r\n\r\n- Splits Python files into function-call-specific demonstration files\r\n  - alternatively, into function-specific files containing only the actual function definition (e.g., for reference/documentation purposes)\r\n- Intelligently preserves setup code and configuration (or optionally, doesn't)\r\n- Maintains imports and necessary context (or optionally, doesn't)\r\n- Handles both explicit `__main__` blocks and module-level code\r\n- Smart detection and inclusion of related print statements and comments (or optionally, not)\r\n- Configurable pattern matching for setup code detection\r\n\r\n\r\n## Command Line Options\r\n\r\n```\r\nusage: fplit.py [-h] [-o OUTPUT_DIR] [-v] [--wrap-main] [--no-setup]        \r\n                [--list-patterns]\r\n                [--disable-patterns PATTERN [PATTERN ...]]\r\n                [--enable-patterns PATTERN [PATTERN ...]] [--show-setup]    \r\n                [--patterns-dir PATTERNS_DIR] [--skip-user-patterns]        \r\n                [--skip-project-patterns]\r\n                source_file\r\n\r\npositional arguments:\r\n  source_file             Python source file to split\r\n\r\noptional arguments:\r\n  -h, --help              show help message and exit\r\n  -o OUTPUT_DIR           output directory for split files (default: current directory)\r\n  -v, --verbose           increase output verbosity (use -v or -vv)\r\n  --wrap-main             always wrap code in __main__ blocks\r\n  --no-setup              skip preservation of module-level setup code\r\n  --funcdefs-only         extracts only function definitions to unique files\r\n  --show-setup            show detected module-level setup code without splitting\r\n  --list-patterns         list all available setup patterns\r\n  --disable-patterns      disable specific setup patterns\r\n  --enable-patterns       enable only specified patterns\r\n  --patterns-dir          directory containing custom pattern definitions\r\n  --skip-user-patterns    skip loading user pattern overrides\r\n  --skip-project-patterns skip loading project-specific patterns\r\n  --similarity-threshold  threshold for print statement similarity\r\n```\r\n\r\n## Usage\r\n\r\nBasic usage:\r\n```bash\r\npython fplit.py demo.py                 # Split into current directory\r\npython fplit.py demo.py -o output_dir   # Split into specified directory\r\npython fplit.py demo.py -v              # Show progress\r\npython fplit.py demo.py -vv             # Show detailed debug info\r\n```\r\n\r\n### Example - Function Call Extraction and Demonstration\r\n\r\nThe default mode of operation is to extract function *calls* into single-purpose fully-runnable scripts, each demonstrating one specific function\r\nGiven the following as input file `demo.py`:\r\n```python\r\nimport logging\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\n# Configure logging\r\nlogging.basicConfig(level=logging.INFO)\r\n\r\n# Set plot style\r\nplt.style.use('seaborn')\r\n\r\nif __name__ == \"__main__\":\r\n    # Demo the data processing\r\n    data = process_data(sample_input)\r\n    print(\"Data processed successfully\")\r\n    \r\n    # Visualize results\r\n    plot_results(data)\r\n    print(\"Generated visualization\")\r\n```\r\n\r\nRunning:\r\n```bash\r\npython fplit.py demo.py\r\n```\r\n\r\nwill create separate files for each function call, preserving necessary setup, comments, and print statements:\r\n```python\r\n### generated file: process_data_demo.py\r\nimport logging\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\n# Configure logging\r\nlogging.basicConfig(level=logging.INFO)\r\n\r\nif __name__ == \"__main__\":\r\n    data = process_data(sample_input)\r\n    print(\"Data processed successfully\")\r\n    exit(0)\r\n```\r\n``` python\r\n### generated file: plot_results_demo.py\r\nimport logging\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\n# Configure logging\r\nlogging.basicConfig(level=logging.INFO)\r\n\r\n# Set plot style\r\nplt.style.use('seaborn')\r\n\r\nif __name__ == \"__main__\":\r\n    plot_results(data)\r\n    print(\"Generated visualization\")\r\n    exit(0)\r\n```\r\n\r\n### Example - Function Reference Extraction\r\n\r\nThis mode extracts only the pure function definitions from your source code. This is useful for creating reference libraries or cataloging implementations:\r\n\r\nGiven the following as an input file:\r\n```python\r\nimport numpy as np\r\nfrom typing import List\r\n\r\ndef quicksort(arr: List[int]) -\u003e List[int]:\r\n    if len(arr) \u003c= 1:\r\n        return arr\r\n    pivot = arr[len(arr) // 2]\r\n    left = [x for x in arr if x \u003c pivot]\r\n    middle = [x for x in arr if x == pivot]\r\n    right = [x for x in arr if x \u003e pivot]\r\n    return quicksort(left) + middle + quicksort(right)\r\n\r\ndef binary_search(arr: List[int], target: int) -\u003e int:\r\n    left, right = 0, len(arr) - 1\r\n    while left \u003c= right:\r\n        mid = (left + right) // 2\r\n        if arr[mid] == target:\r\n            return mid\r\n        elif arr[mid] \u003c target:\r\n            left = mid + 1\r\n        else:\r\n            right = mid - 1\r\n    return -1\r\n\r\nif __name__ == \"__main__\":\r\n    test_arr = [3, 6, 8, 10, 1, 2, 1]\r\n    sorted_arr = quicksort(test_arr)\r\n    idx = binary_search(sorted_arr, 6)\r\n```\r\n\r\nRunning:\r\n```bash\r\npython fplot.py source.py --funcdefs-only\r\n```\r\n\r\nwill create separate files for each function call, _*NOT*_ preserving any surrounding setup, comments, etc:\r\n\r\n```python\r\n### generated file: quicksort.py\r\ndef quicksort(arr: List[int]) -\u003e List[int]:\r\n    if len(arr) \u003c= 1:\r\n        return arr\r\n    pivot = arr[len(arr) // 2]\r\n    left = [x for x in arr if x \u003c pivot]\r\n    middle = [x for x in arr if x == pivot]\r\n    right = [x for x in arr if x \u003e pivot]\r\n    return quicksort(left) + middle + quicksort(right)\r\n```\r\n```python\r\n### generated file: binary_search.py\r\ndef binary_search(arr: List[int], target: int) -\u003e int:\r\n    left, right = 0, len(arr) - 1\r\n    while left \u003c= right:\r\n        mid = (left + right) // 2\r\n        if arr[mid] == target:\r\n            return mid\r\n        elif arr[mid] \u003c target:\r\n            left = mid + 1\r\n        else:\r\n            right = mid - 1\r\n    return -1\r\n```\r\n\r\nThis mode:\r\n- Extracts only function definitions\r\n- Names files directly after the functions\r\n- Excludes imports, setup code, and main blocks\r\n- Preserves function signatures and type hints\r\n- Creates a clean reference library of implementations\r\n\r\nThis is particularly useful when:\r\n- Creating an algorithm reference library\r\n- Extracting reusable functions from existing code\r\n- Building a catalog of implementation patterns\r\n- Preparing code examples for documentation\r\n \r\n## Common Python Library Setup Pattern Detection\r\n\r\nfplit intelligently detects and preserves setup code for many popular Python libraries. Here's what each pattern matches:\r\n\r\n### Data Science \u0026 ML\r\n- **NumPy**: Random seeds, print options, error settings (but not array operations)\r\n- **Pandas**: Display options, default settings (but not data operations)\r\n- **Matplotlib**: Style settings, backend config (but not actual plotting)\r\n- **Seaborn**: Theme setting, style config (but not visualizations)\r\n- **Plotly**: Template selection, renderer config (but not plotting)\r\n- **TensorFlow**: GPU/device config, random seeds (but not model ops)\r\n- **PyTorch**: Device selection, seeds, cudnn config (but not training)\r\n- **JAX**: Platform selection, precision config (but not computations)\r\n- **Scikit-learn**: Random state setup (but not model operations)\r\n\r\n### Web \u0026 API\r\n- **FastAPI**: App creation, middleware setup (but not routes)\r\n- **Django**: Settings adjustment (but not views)\r\n- **Requests**: Session creation, auth setup (but not API calls)\r\n- **SQLAlchemy**: Engine creation, pool config (but not queries)\r\n\r\n### Testing \u0026 Debug\r\n- **Pytest**: Skip conditions, import checking (but not test functions)\r\n- **Logging**: Logger creation, level setting (but not log messages)\r\n- **Warnings**: Warning filters (but not warning raises)\r\n\r\n### Other\r\n- **OpenCV**: Threading config, window params (but not image ops)\r\n- **Ray**: Init config, resource setup (but not computations)\r\n- **Random**: Seed setting (but not generation)\r\n- **Environment Variables**: Environment variable setting\r\n\r\n### Setup Patterns Configuration Guide\r\n[Setup Patterns Configuration Guide](https://github.com/scottvr/fplit/blob/main/Pattern_Configuration_Guide.md)\r\n\r\n## Installation\r\n\r\n```bash\r\ngit clone https://github.com/scottvr/fplit.git\r\ncd fplit\r\npython -m pip install -r requirements.txt\r\n```\r\n\r\n## TODO\r\n\r\nContributions are welcome. Here are some things on the todo list:\r\n\r\n- Additional setup patterns for other popular libraries\r\n- Smarter handling of function dependencies\r\n- Support for async/await syntax\r\n- Configuration file support\r\n\r\n## License\r\n\r\nMIT\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscottvr%2Ffplit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscottvr%2Ffplit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscottvr%2Ffplit/lists"}