codeg
Codeg (code generator) is a python library that allows you to generate python code dynamically.
This library is still a work in progress
Demonstration
Create simple class
Let's say you want to generate the following class dynamically
class Animal:
def __init__(self, name: str):
self.name = name
You can use the following code
import codeg
# Create the 'Animal' class
code_cls = codeg.cls("Animal")
# Add the init method
code__init = code_cls.method(
"__init__", parameters=[codeg.param("name", annotation=str)]
)
# Add a simple instruction
code__init.line("self.name = name")
# Print the generated code
code_cls.print()
# Get the generated code in a variable
script = code_cls.generate_code()
# Eval the code and get it on a variable
Animal = code_cls.build()
# Create an instance of our dynamically generated cls
animal = Animal("rex")
print(animal)
Generate script
It is possible to generate and execute a script
def double(i):
x = i * 2
return x
for i in range(10):
print(double(i))
Dynamically generate the script above
import codeg
# Create a new empty script
code_script = codeg.script()
# Create the 'double' function
code_function = code_script.function("double", ["i"])
code_function.line("x = i * 2")
code_function.return_("x")
# Create the 'for' loop
code_forloop = code_script.for_("i", "range(10)")
code_forloop.line("print(double(i))")
# Execute the script
build_dict = code_script.build()
# Get the 'double' function
double = build_dict["double"]
It is possible to chain the methods in the same line, since the methods line and return will return the current object:
# We can replace the generation of function with the following code
code_script.function("double", ["i"]).line("x = i * 2").return_("x")
By chaining the methods we can have the same script with less code:
import codeg
# Create a new empty script
code_script = codeg.script()
# Create the 'double' function
code_script.function("double", ["i"]).line("x = i * 2").return_("x")
# Create the 'for' loop
code_script.for_("i", "range(10)").line("print(double(i))")
# Execute the script
build_dict = code_script.build()
# Get the 'double' function
double = build_dict["double"]
Metadata
Release files for codeg 0.2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| codeg-0.2.3.tar.gz | 11.3 kB | Without provenance | Details for codeg-0.2.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| codeg-0.2.3-py3-none-any.whl | Python 3 | none | any | Details for codeg-0.2.3-py3-none-any.whl |
Total release size: 28.3 kB
Release files / codeg-0.2.3.tar.gz
| Download URL | codeg-0.2.3.tar.gz |
|---|---|
| Size | 11.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9f0ab5dbd4580a3c2d7c7f0158846039d0fe01c92be698b59af7e6009434c417
|
|
BLAKE2b-256 checksum How to use checksums |
1d4fbad22d82c60466cf649af482c006cb0b1c1c621b2717b082ca362743f09e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.4.2 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10
|
Release files / codeg-0.2.3-py3-none-any.whl
| Download URL | codeg-0.2.3-py3-none-any.whl |
|---|---|
| Size | 17.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
83c1ec40f02fb11f3be9aadc073e1fe6a6610aa4e834806bda4f96aee4d9f75e
|
|
BLAKE2b-256 checksum How to use checksums |
fb164b85a30d7f502a21119ed3a9a78cb6cf8f595a93fc411ae98670849539cc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.4.2 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10
|
Security for codeg 0.2.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.