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')
)

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!")')

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)

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.1.tar.gz (7.6 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.1-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: PythonPoet-0.1.1.tar.gz
  • Upload date:
  • Size: 7.6 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.1.tar.gz
Algorithm Hash digest
SHA256 7da6ca675f25b66b77dc2fd17af03288bd620d60a79b82ad058c981389b26112
MD5 5f9a220de93f6faf9a3adeb868e8f9db
BLAKE2b-256 1cfe12acb8156becbd4a1f72712b0e60994f42a985064c8ecaf6bebb185939fc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: PythonPoet-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.9 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e01bc6fb5a2933ca7f007232b6dbfbab8f8db44015bd4c2eaa26b93b00ab80cc
MD5 8d9785783ea14db6af891fc476840354
BLAKE2b-256 7f6c01d2cf1b71ec142871b4036b5e1a1ea866cc21117af194e4c55281e868b9

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