Skip to main content

A simple code generator

Project description

ghost_writer

CI codecov DOI PyPI PyPI - Downloads

A simple package that allows you to build a class to parameterize scripts via code generation.

A cmdline utility is add to convert python scripts into generators which can later be modified.

Installation

pip install ghost-writer

Usage

Say you have a simple script that you want to parameterize for code generation.

import os


for i in range(10):
    for j in range(20):
        pass

In the terminal run the scriptify_py command on the script

scriptify_py script.py

Now you will have a new file called generated_script.py which contains a class that can generate that script which can be run:

from ghost_writer import ScriptGenerator


class NewGenerator(ScriptGenerator):
    def __init__(self, file_name: str) -> None:
        super().__init__(file_name)

    def _build_script(self) -> None:
        self._add_line('import os')
        self._end_line()
        self._end_line()
        self._add_line('for i in range(10):')
        self._add_line('for j in range(20):', indent_level=1)
        self._add_line('pass', indent_level=2)

We can now modify the class to be parameterized:

from ghost_writer import ScriptGenerator


class MyGenerator(ScriptGenerator):
    def __init__(self, file_name: str, parameter: int) -> None:

        self._parameter = parameter

		super().__init__(file_name)

    def _build_script(self) -> None:
        self._add_line('import os')
        self._end_line()
        self._end_line()
        self._add_line('for i in range(10):')
        self._add_line('for j in range(20):', indent_level=1)
        self._add_line(f'print({parameter})', indent_level=2)

Now in python, we can import our class and run it:

generator = MyGenerator("new_script.py", 4)

generator.write()

And now we have a generated script called new_script.py

import os


for i in range(10):
    for j in range(20):
        print(4)
  • Free software: GNU General Public License v3

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

pyghost_writer-0.0.2.tar.gz (24.8 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: pyghost_writer-0.0.2.tar.gz
  • Upload date:
  • Size: 24.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for pyghost_writer-0.0.2.tar.gz
Algorithm Hash digest
SHA256 9db34f71d1fa29ab529ab699417ab7827daeb522960b0217fd7fdceb497e12c2
MD5 9482aeb3c2055dcb6272b56279d982f3
BLAKE2b-256 1e1efca0cd86474067c35869d8cfe754777a5dc8cd0d092af93cf02e8b6b83d4

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