Skip to main content
https://travis-ci.org/podhmo/prestring.svg?branch=master

this package is heavily inspired by srcgen .

(todo: gentle introduction)

features

  • generating code with with-syntax

  • string injection after writing string

generating code with with-syntax

from prestring.python import PythonModule

m = PythonModule()

with m.class_("Point", metaclass="InterfaceMeta"):
    with m.def_("__init__", "self", "value"):
        m.stmt("self.value = value")

    with m.def_("__str__", "self"):
        m.return_("self.value")

output is.

class Point(object, metaclass=InterfaceMeta)
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return self.value

string injection after writing string

from prestring.python import PythonModule

m = PythonModule()

with m.def_("setup", "config"):
    import_area = m.submodule()
    m.sep()
    for k in ["a", "b", "c", "d", "e"]:
        import_area.stmt("from .plugins import {k}_plugin", k=k)
        m.stmt("config.activate({}_plugin)", k)

print(m)
def setup(config):
    from .plugins import(
        a_plugin,
        b_plugin,
        c_plugin,
        d_plugin,
        e_plugin
    )

    config.activate(a_plugin)
    config.activate(b_plugin)
    config.activate(c_plugin)
    config.activate(d_plugin)
    config.activate(e_plugin)

sub modules

  • prestring.output

  • prestring.python.transform, prestring.text.transform

prestring.output

prestring.output can write multiple files.

import sys
from prestring.python import Module
from prestring.output import output, cleanup_all # noqa


dst = sys.argv[1]
with output(root=dst) as fs:
    with fs.open("projects/x.txt", "w") as wf:
        print("hello x", file=wf)
        print("bye x", file=wf)

    with fs.open("projects/y.txt", "w") as wf:
        print("hello y", file=wf)
        print("bye y", file=wf)

    with fs.open("projects/z.py", "w", opener=Module) as m:
        with m.def_("hello"):
            m.stmt("print('hello')")

Above code will generate three files. if creating directory is needed, it will be created automatically.

$ python src/main.py dst
[D]  create  dst/projects
[F]  create  dst/projects/x.txt
[F]  create  dst/projects/y.txt
[F]  create  dst/projects/z.py

On rerun, no message is displayed. And rerun with VERBOSE=1 var env to see more detailed output.

$ python src/main.py dst
$ VERBOSE=1 python src/main.py dst
[F]  no change       dst/projects/x.txt
[F]  no change       dst/projects/y.txt
[F]  no change       dst/projects/z.py

dry-run

Running with CONSOLE=1 varenv or calling with use_console=True option, doesn’t save files.

$ CONSOLE=1 python src/main.py dst
[F]  update  dst/projects/x.txt
[F]  update  dst/projects/y.txt
[F]  update  dst/projects/z.py

# more verbose output
VERBOSE=1 CONSOLE=1 python src/00/main.py dst/00/create
# dst/00/create/projects/x.txt
----------------------------------------
  hello x
  bye x


# dst/00/create/projects/y.txt
----------------------------------------
  hello y
  bye y


# dst/00/create/projects/z.py
----------------------------------------
  def hello():
      print('hello')

prestring.python.transform, prestring.text.transform

the Transform function means converting raw source code (or text) to prestring’s code. And you can use python -m prestring.python (or running python -m prestring.text) as a CLI command, as follows.

$ cat hello.py
def hello(name: str, *, message: str = "hello world"):
    """
    greeting message
    """
    print(f"{name}: {message}")


if __name__ == "__main__":
    hello("foo")

$ python -m prestring.python hello.py

from prestring.python import PythonModule


def gen(*, m=None, indent='    '):
    m = m or PythonModule(indent=indent)

    import textwrap
    with m.def_('hello', 'name: str', '*', 'message: str =  "hello world"'):
        m.docstring(textwrap.dedent("""
        greeting message
        """).strip())
        m.stmt('print(f"{name}: {message}")')

    with m.if_('__name__ == "__main__"'):
        m.stmt('hello("foo")')
    return m


if __name__ == "__main__":
    m = gen(indent='    ')
    print(m)

Of course, reversible.

$ python <(python -m prestring.python hello.py)
def hello(name: str, *, message: str =  "hello world"):
    """
    greeting message
    """
    print(f"{name}: {message}")


if __name__ == "__main__":
    hello("foo")

$ python hello.py
foo: hello world
$ python <(python <(python -m prestring.python hello.py))
foo: hello world

prestring.text

If you want to prestring’s expression as first step, in other language, prestring.text is probably useful.

$ python -m prestring.text --tab hello.go
from prestring.text import Module


def gen(*, m=None, indent='\t'):
    m = m or Module(indent=indent)

    m.stmt('package main')
    m.sep()
    m.stmt('import (')
    with m.scope():
        m.stmt('"fmt"')
        m.stmt('"os"')
    m.stmt(')')
    m.sep()
    m.stmt('// Hello is print Hello')
    m.stmt('func Hello(name string)  {')
    with m.scope():
        m.stmt('fmt.Printf("%s: Hello", name)')
    m.stmt('}')
    m.sep()
    m.stmt('func main()  {')
    with m.scope():
        m.stmt('var name string')
        m.stmt('if len(os.Args) > 1  {')
        with m.scope():
            m.stmt('name = os.Args[1]')
        m.stmt('} else  {')
        with m.scope():
            m.stmt('name = "foo"')
        m.stmt('}')
        m.stmt('// with block')
        m.stmt('{')
        with m.scope():
            m.stmt('Hello(name)')
        m.stmt('}')
    m.stmt('}')
    return m


if __name__ == "__main__":
    m = gen(indent='\t')
    print(m)

0.9.0

  • codeobject module

  • some refactoring

0.8.3, 0.8.4, 0.8.5

  • adjustment for typing

0.8.2

  • add codeobject module, PythonModule.import_() returns symbol object

  • remove unused methods

0.8.1

  • prestring.python, async/await support

0.8.0

  • typing (but this is not completely strict)

0.7.3

  • In prestring.text, use tab default indent 1 (#47)

0.7.2

  • bug fix of 0.7.1

0.7.1

  • “python -m prestring.python” support typed function definition

0.7.0

  • changes “python -m prestring.python.transform” to “python -m prestring.python”

  • some logging adjustments and fix bugs (prestring.output)

  • add prestring.text

0.6.0

  • support only python3.6+

  • refactoring prestring.output

0.5.3

  • rollback, python’s import part output

0.5.1, 0.5.2

  • some bug fixes

0.5.0

  • add prestring.python.transform

0.4.7

  • add prestring.output.SeparatedOutput

  • LazyKeywords is sorted

0.4.3

  • clear if empty (e.g. go-module’s import_group)

0.4.2

  • bug fix LazyArgumentsAndKeywords

0.4

  • more lazy (python module)

  • lazy object is cached

0.3.3

  • goname function

0.3.1

  • more lazy (go module)

0.3.0

  • go code output support

Release files for prestring 0.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for prestring 0.9.0
File Size Uploaded
prestring-0.9.0.tar.gz 26.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for prestring 0.9.0
File Interpreter ABI Platform
prestring-0.9.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 58.0 kB

Release files / prestring-0.9.0.tar.gz

Download URL prestring-0.9.0.tar.gz
Size 26.9 kB
Tags Source
SHA-256 checksum
How to use checksums
359b08cccb0f76abea3f8278f36b60d85f20081c0acc188777ae01c5531965ff
BLAKE2b-256 checksum
How to use checksums
05798f31ef53bbce40aa7fa90d8eaaee479066c6fab830636522f9c6e232e878
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

Release files / prestring-0.9.0-py2.py3-none-any.whl

Download URL prestring-0.9.0-py2.py3-none-any.whl
Size 31.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
cefe27205f820803d41cc53c4911fc75486187b87bcd34678907ce62894b92c0
BLAKE2b-256 checksum
How to use checksums
8a0299f8a1e91d63be66aa23d601a0939381d57eee86e5771739d8339c6abbf8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

Release history Release notifications | RSS feed

This release

0.9.0 This release

2 release files

0.8.5

2 release files

0.8.4

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.3

2 release files

0.7.2

1 release file

0.7.1

1 release file

0.7.0

1 release file

0.6.0

1 release file

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.9

2 release files

0.4.8

2 release files

0.4.7

2 release files

0.4.6

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

0.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.5

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page