Skip to main content

API for generating Python source code from runtime.

Project description

PythonPoet

API for generating Python source code from runtime. It can be useful for generating types' files, interacting with metadata and creating auto-generated code.

Getting Started

PythonPoet allows you to very quickly start working on your first autogenerated code. In order to simplify generation process, we've created a lot of simply-to-use builders.

Installation

PythonPoet requires Python 3.10 or newer. You can install this module using Pip and PyPI: pip install PythonPoet.

Creating class

So, after you've installed PythonPoet, you'll want to generate your first class. It's very, very simple. First of all, you'll need to create a new instance of the PythonPoet class. You need to know that PythonPoet class is a class that manages everything during the generation process. Let's see an example:

import pythonpoet

poet = pythonpoet.PythonPoet()

Moving on. Now, after you've created a PythonPoet class, you can start generating methods and classes. In order to create your first class, you'll need to call .add_class method on the PythonPoet instance. This method takes only one argument - class' builder. This is an example:

poet.add_class(
    pythonpoet.ClassBuilder()
    .set_name('YourClassName')
)

This code will result in

class YourClassName:
    ...

That's it. You've created your first class using PythonPoet. Let's move on creating methods, fields and decorating the class. By the way, you can combine class with methods, fields and decorators.

Creating method

Creating your first method is simple as creating your first class. The principle are more or less the same. You'll need to use MethodBuilder class, set method's name, arguments, source code and a return type[^type_note]. The only required field in method is its name, everything else is optional. Here's an example:

pythonpoet.types.method.MethodBuilder()
.set_name('your_method_name')
.set_source_code('print("Hello, World!")')

This code will result in

def your_method_name():
    print("Hello, World!")

Creating field

Yet again, this technique is similar to what you've done for creating methods and classes itself. You'll need to use ClassFieldBuilder and set its name and type[^type_note] (it's required). Here's an example:

pythonpoet.ClassFieldBuilder()
.set_name('your_field_name')
.set_type(str)

This code will result in

your_field_name: str

Adding comments

Currently, you can only add comments to the fields, classes and methods. In order to do this, you'll need to call a add_comment on the builder. You can also add a newline using add_newline method. Here's an example:

pythonpoet.ClassFieldBuilder()
.set_name('yet_another_field')
.set_type(str)
.add_comment('Example comment!')

This code will result in

yet_another_field: str
"""Example comment!"""

Adding decorators

Adding decorators is simple as other things of PythonPoet! We're supporting decorators on methods and classes. In order to add a new decorator to your type, you'll need to call a .add_decorator method. If you need, you can also add an import for your decorator. Here's an example:

pythonpoet.ClassBuilder()
.set_name('YourClass')
.add_decorator('some_decorator')

This will result in

@some_decorator
class YourClass:
    ...

Need help?

Join our Discussions in order to get any help with this library. Feel free to ask any library-related questions.

License

Everything licensed with MIT license, unless otherwise specified.

Contributing

First of all, thank you for your interest in help with maintaining this library. Your support is greatly appreciated. So, in order to contribute, you firstly need to read our Contribution Guidelines. After that, you can read Contribution guide and pick any issue that you think you can help with.

[^type_note]: If you want to use non-primitive type, you'll need to create an import for it. Here's an example: ImportBuilder().set_class_name("dataclass").set_module_name("dataclasses"), which will convert into from dataclasses import dataclass. After you've created an import, you can pass it along with your type: .set_type(dataclass, ImportBuilder().set_class_name("dataclass").set_module_name("dataclasses")).

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

PythonPoet-0.1.3.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

PythonPoet-0.1.3-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file PythonPoet-0.1.3.tar.gz.

File metadata

  • Download URL: PythonPoet-0.1.3.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for PythonPoet-0.1.3.tar.gz
Algorithm Hash digest
SHA256 a928aa0b47ff798bfafd0653035a4437f2ec80f0d711c5de356810b822c41422
MD5 b1b0c1749f21272021cb05c360825763
BLAKE2b-256 0c54af7464cedfa7b7c169fb81364439ec9b91d6785125d336fab35ba37cd09e

See more details on using hashes here.

File details

Details for the file PythonPoet-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: PythonPoet-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for PythonPoet-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 394c0c19ca7a64837801f6cb65e8ceb49e5ad4bc1bfab5bf1bf5d6fe9e90326b
MD5 80e9622b5413cd84f0d342f4d48bedf7
BLAKE2b-256 a1ddb861ca3c943f2442010c9532f18346fdf8fe93655f81ae41b6456d7705e5

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