[PR #1292] [CLOSED] add pycro #1147

Closed
opened 2025-11-06 13:10:17 -06:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/vinta/awesome-python/pull/1292
Author: @mak12776
Created: 5/12/2019
Status: Closed

Base: masterHead: master


📝 Commits (1)

📊 Changes

1 file changed (+1 additions, -0 deletions)

View changed files

📝 README.md (+1 -0)

📄 Description

What is this Python project?

Pycro is a python integrated macro preprocessor. It will interpret input texts
and generates a corresponding python code that will generate the intended
result, if we compile and execute that.

usage example

imagine we have this main.c file:


#include <stdio.h>

//# names = ['Oliver', 'Jack', 'Harry', 'James', 'John']

int main()
{
	//@ for name in names:
	printf("Hello ${name}!\n");
	//@ end for
	return 0;
}

we open this file and pass it to generate_code function:

#!/usr/bin/python3

import pycro

env = pycro.CompilerEnvironment(language = 'c')

with open('main.c') as infile:
    with open('main.c.py', 'w') as outfile:
        pycro.generate_code(infile, outfile, env)

another file will be created named main.c.py:

__outfile__.write('\n');
__outfile__.write('#include <stdio.h>\n');
__outfile__.write('\n');
names = ['Oliver', 'Jack', 'Harry', 'James', 'John']
__outfile__.write('\n');
__outfile__.write('int main()\n');
__outfile__.write('{\n');
for name in names:
	__outfile__.write('\tprintf("Hello ');__outfile__.write(str(name));__outfile__.write('!\\n");\n');
__outfile__.write('\treturn 0;\n');
__outfile__.write('}\n');
__outfile__.write('\n');

now we can compile and execute main.c.py using the following code:

#!/usr/bin/python3

import pycro

env = pycro.ExecutorEnvironment()

with open('main.c.py') as infile:
    with open('_main.c', 'w') as outfile:

        code_object = pycro.compile_generated_code(infile.read(), infile.name)

        pycro.execute_code_object(code_object, outfile, env)

the generated result saved as _main.c:


#include <stdio.h>


int main()
{
	printf("Hello Oliver!\n");
	printf("Hello Jack!\n");
	printf("Hello Harry!\n");
	printf("Hello James!\n");
	printf("Hello John!\n");
	return 0;
}

it's time to compile _main.c:

$ gcc _main.c -o main

and run ./main:

$ ./main:

Hello Oliver!
Hello Jack!
Hello Harry!
Hello James!
Hello John!

What's the difference between this Python project and similar ones?

I'm tired to compare them here, but it's not complete.

--

Anyone who agrees with this pull request could vote for it by adding a 👍 to it, and usually, the maintainer will merge it when votes reach 20.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/vinta/awesome-python/pull/1292 **Author:** [@mak12776](https://github.com/mak12776) **Created:** 5/12/2019 **Status:** ❌ Closed **Base:** `master` ← **Head:** `master` --- ### 📝 Commits (1) - [`d15ef0f`](https://github.com/vinta/awesome-python/commit/d15ef0fe04ee882e9f3262cd43d3807f4f36908b) add pycro ### 📊 Changes **1 file changed** (+1 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `README.md` (+1 -0) </details> ### 📄 Description ## What is this Python project? [Pycro][pycro-link] is a python integrated macro preprocessor. It will interpret input texts and generates a corresponding python code that will generate the intended result, if we compile and execute that. ## usage example imagine we have this `main.c` file: ```c #include <stdio.h> //# names = ['Oliver', 'Jack', 'Harry', 'James', 'John'] int main() { //@ for name in names: printf("Hello ${name}!\n"); //@ end for return 0; } ``` we open this file and pass it to `generate_code` function: ```python #!/usr/bin/python3 import pycro env = pycro.CompilerEnvironment(language = 'c') with open('main.c') as infile: with open('main.c.py', 'w') as outfile: pycro.generate_code(infile, outfile, env) ``` another file will be created named `main.c.py`: ```python __outfile__.write('\n'); __outfile__.write('#include <stdio.h>\n'); __outfile__.write('\n'); names = ['Oliver', 'Jack', 'Harry', 'James', 'John'] __outfile__.write('\n'); __outfile__.write('int main()\n'); __outfile__.write('{\n'); for name in names: __outfile__.write('\tprintf("Hello ');__outfile__.write(str(name));__outfile__.write('!\\n");\n'); __outfile__.write('\treturn 0;\n'); __outfile__.write('}\n'); __outfile__.write('\n'); ``` now we can compile and execute `main.c.py` using the following code: ```python #!/usr/bin/python3 import pycro env = pycro.ExecutorEnvironment() with open('main.c.py') as infile: with open('_main.c', 'w') as outfile: code_object = pycro.compile_generated_code(infile.read(), infile.name) pycro.execute_code_object(code_object, outfile, env) ``` the generated result saved as `_main.c`: ```c #include <stdio.h> int main() { printf("Hello Oliver!\n"); printf("Hello Jack!\n"); printf("Hello Harry!\n"); printf("Hello James!\n"); printf("Hello John!\n"); return 0; } ``` it's time to compile `_main.c`: ``` $ gcc _main.c -o main ``` and run `./main`: `$ ./main`: ``` Hello Oliver! Hello Jack! Hello Harry! Hello James! Hello John! ``` ## What's the difference between this Python project and similar ones? I'm tired to compare them [here][compare-link], but it's not complete. -- Anyone who agrees with this pull request could vote for it by adding a :+1: to it, and usually, the maintainer will merge it when votes reach **20**. [pycro-link]: https://github.com/mak12776/pycro [compare-link]: https://github.com/mak12776/tests/tree/master/python/template-engines --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2025-11-06 13:10:17 -06:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/awesome-python#1147