A Python package for generating source code in C, C++, C#, and Java from python code.
Project description
CodeGen
CodeGen is a Python library for programmatically generating source code in various languages, including C, C++, C#, and Java. It provides an object-oriented API to construct code elements like classes, methods, and variables, which can then be rendered into source files.
This library is particularly useful for:
- Generating boilerplate code from a data model.
- Creating cross-language implementations from a single source.
- Automating the creation of data transfer objects (DTOs), hardware abstraction layers (HALs), or other repetitive code structures.
Core Concepts
The library is built around a set of classes (e.g., OClass, OMethod) that represent different parts of a program. You create instances of these classes and assemble them to define your desired code structure. The OFile class manages the final output to a file, adapting the syntax based on the file extension.
Modifiers
Code elements like classes, methods, and arguments can be assigned modifiers. These are string constants that control visibility and behavior.
- Visibility:
PUBLIC,PRIVATE,PROTECTED - Behavior:
STATIC,CONST,FINAL,OVERRIDE - Special:
GETTER,SETTER(for automatic property generation),TEST(for test classes/methods),DBVAR,EXTERNAL
File Generation
OFile(fname, namespace="")
The OFile class is the main entry point for creating a source file.
fname: The name of the file to be generated (e.g.,"myclass.cpp"). The file extension determines the output language.namespace: (Optional) The namespace for the code (used in C# and C++).
You can write raw strings or OBase objects to the file using the left-shift << operator. The class automatically handles indentation.
f = OFile("config.h")
f << "#define VERSION 1.0"
f.close()
Writer Functions
Several helper functions simplify the process of writing complete classes to files:
write_file_cs(classes, fname, namespace, includes=[]): Writes a list of classes to a single C# file.write_file_c(class_obj, path, cincludes, hincludes): Writes a class to a pair of.cand.hfiles.write_file_cpp(class_obj, path): Writes a class to a pair of.cppand.hppfiles.
Code Generation Classes
The following classes are used to build the code structure.
OClass(name, mods={PUBLIC})
Represents a class definition. You can add members (methods, arguments) to it using the << operator.
my_class = OClass("MyClass")
my_class << OMethod("myMethod", "void")
OMethod(name, ctype, args=[], mods={PUBLIC})
Represents a method or function.
name: The method name.ctype: The return type.args: A list ofOArgobjects for the parameters.mods: A set of modifiers (e.g.,{PUBLIC, STATIC}).
Method body code can be added as a list of strings.
# void sayHello(const char* name) { ... }
method = OMethod("sayHello", "void", [OArg("name", "const char*")])
method << 'printf("Hello, %s\\n", name);'
OArg(name, ctype, mods={PRIVATE}, initial=None)
Represents a variable, class member, or function argument.
name: The variable name.ctype: The variable type.initial: An optional initial value.
OEnum(name, mods={PUBLIC}, items=[])
Represents an enumeration.
# enum Color { RED, GREEN, BLUE };
color_enum = OEnum("Color", items=["RED", "GREEN", "BLUE"])
OStruct(name, mods={PUBLIC})
Represents a C-style struct.
# typedef struct { int x; int y; } Point_t;
point_struct = OStruct("Point")
point_struct << OArg("x", "int")
point_struct << OArg("y", "int")
Example: Generating a C++ Class
This example demonstrates how to generate a simple C++ class with a header and source file.
from codegen import *
# Define the class structure
user_class = OClass("User")
user_class.implements = ["IComparable"] # For C# or Java
# Add member variables
user_class << OArg("name", "string", {PRIVATE, GETTER, SETTER})
user_class << OArg("userId", "int", {PRIVATE, GETTER})
# Add a constructor
constructor = OMethod("User", "", [OArg("name", "string"), OArg("id", "int")])
constructor << "this->name = name;"
constructor << "this->userId = id;"
user_class << constructor
# Add a method
method = OMethod("print", "void", mods={PUBLIC, CONST})
method << 'cout << "User ID: " << userId << ", Name: " << name << endl;'
user_class << method
# Write to .hpp and .cpp files
write_file_cpp(user_class, "./output/")
This will generate output/User.hpp and output/User.cpp with the corresponding class definition, member implementations, and getter/setter methods.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py2code-0.2.0-py3-none-any.whl.
File metadata
- Download URL: py2code-0.2.0-py3-none-any.whl
- Upload date:
- Size: 19.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
29a121c1cd57445fff33039057f544531e6b38aa6829fbf042b61a8d9cfd9362
|
|
| MD5 |
722e817083d9fd4b1f037402ca9aceea
|
|
| BLAKE2b-256 |
fda897218ebfefd7e6a23eb58f19d2f6805963eb3c15a015b219490edd70d21b
|