Skip to main content

Python library to generate and execute code dynamically

Project description

Pypi version Python versions Black


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"]

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

codeg-0.2.2.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

codeg-0.2.2-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file codeg-0.2.2.tar.gz.

File metadata

  • Download URL: codeg-0.2.2.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.27.1 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10

File hashes

Hashes for codeg-0.2.2.tar.gz
Algorithm Hash digest
SHA256 e4df66d6aaf484c09cd7c36f6cbd1e294acc5c1a7fe75ac141e690900efd6251
MD5 ef95839a15df14b3c0bfd826e0c25f14
BLAKE2b-256 b29f2e32260020511c452ad3c965dd75e92d45f1af34c2d3a2ada7c503caac96

See more details on using hashes here.

File details

Details for the file codeg-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: codeg-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.27.1 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10

File hashes

Hashes for codeg-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 38962f0f7c0b6a0d2550b56062f984e22b8d6758f3ae141c19fdf2beb0813262
MD5 835107cf1b7ee8f08f43ddf5b46d1109
BLAKE2b-256 cd1776ae99eff0b603b56414ee43d8419db9b00b44f31ba9d4e1289e204f9af1

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