codeg-0.2.3
توضیحات
Python library to generate and execute code dynamically
| ویژگی | مقدار |
|---|---|
| سیستم عامل | OS Independent |
| نام فایل | codeg-0.2.3 |
| نام | codeg |
| نسخه کتابخانه | 0.2.3 |
| نگهدارنده | ['Nazime LAKEHAL'] |
| ایمیل نگهدارنده | ['nazime.lkh@gmail.com'] |
| نویسنده | Nazime LAKEHAL |
| ایمیل نویسنده | nazime.lkh@gmail.com |
| آدرس صفحه اصلی | https://github.com/nazime/codeg |
| آدرس اینترنتی | https://pypi.org/project/codeg/ |
| مجوز | MIT |
[](https://pypi.org/project/codeg/) [](https://pypi.org/project/codeg/) [](https://github.com/psf/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
```python
class Animal:
def __init__(self, name: str):
self.name = name
```
You can use the following code
```python
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
```python
def double(i):
x = i * 2
return x
for i in range(10):
print(double(i))
```
Dynamically generate the script above
```python
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:
```python
# 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:
```python
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"]
```
نیازمندی
| مقدار | نام |
|---|---|
| - | attr |
| - | black |
| - | coverage |
| - | hypothesis |
| >=4.3.0 | pytest |
| - | coloring |
| - | sphinx |
| - | sphinx-rtd-theme |
| - | sphinxcontrib.napoleon |
| - | pre-commit |
| - | sphinx |
| - | sphinx-rtd-theme |
| - | sphinxcontrib.napoleon |
| - | coverage |
| - | hypothesis |
| >=4.3.0 | pytest |
| - | coloring |
| - | coverage |
| - | hypothesis |
| >=4.3.0 | pytest |
| - | coloring |
| - | sphinx |
| - | sphinx-rtd-theme |
| - | sphinxcontrib.napoleon |
| - | pre-commit |
| - | tox |
| - | codecov |
زبان مورد نیاز
| مقدار | نام |
|---|---|
| >=3.6 | Python |
نحوه نصب
نصب پکیج whl codeg-0.2.3:
pip install codeg-0.2.3.whl
نصب پکیج tar.gz codeg-0.2.3:
pip install codeg-0.2.3.tar.gz