https://github.com/savirsingh/pytocpp
Convert Python Code To C++ Code
https://github.com/savirsingh/pytocpp
cpp python pytocpp
Last synced: about 2 months ago
JSON representation
Convert Python Code To C++ Code
- Host: GitHub
- URL: https://github.com/savirsingh/pytocpp
- Owner: savirsingh
- Created: 2022-08-14T19:50:24.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-04-11T22:27:48.000Z (almost 3 years ago)
- Last Synced: 2024-08-11T09:25:49.056Z (over 1 year ago)
- Topics: cpp, python, pytocpp
- Language: Python
- Homepage:
- Size: 139 KB
- Stars: 13
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Py To CPP
## Convert Python Code To C++

Py To CPP searches files for Python-unique syntax and converts it to the C++ equivalent.
Clone the repository:
```
git clone https://github.com/savirsingh/pytocpp
```
Go to the directory and start up the conversion script:
```
python convert.py
```
#### There may be some bugs, and this project certainly isn't ready for professional use. It can be helpful for testing, personal use, and competitive programming, since it can generate large C++ files in seconds based on your Python code.
### Status:
| Statements/Syntax | Status |
| ------------- | ------------- |
| If/Else If/Else | ✅ |
| For Loops | ✅ |
| While Loops | ✅ |
| Print Statements | ✅ |
| Input (int/str) | ✅ |
| Variable Assignments (int/str/bool) | ✅ |
| Format Conversions | ✅ |
| Single Line Comments | ✅ |
| Multiline Comments | ❌ |
Feel free to contribute and add/improve any of these features.
My email address is kopichiki@gmail.com if you need it.
### Keep these things in mind:
- Don't use unnecessary brackets in your output/print statement.
#### DO NOT code like this (❌):
```
a = 100
b = 10
for i in range(10):
print((a+b)*10)
```
#### Code like this (✅):
```
a = 100
b = 10
c = a+b
for i in range(10):
print(c*10)
```
- Don't rely entirely on Py To CPP.
#### It's important to have some C++ skills, even when using this automated converter. It will help you debug potential issues and/or optimization opportunities for very large script conversions.
### Take a look at the examples included in this repository (both Python and C++).
## What it can do (in depth):
- Convert print statements to std::cout (by simply replacing "print(" with "cout << " and replacing ")" with either " << endl;" or another string of your choice).
- Convert both string inputs and integer inputs to std::cin (through removal of " = input()" and addition of " >> cin").
- Convert for loops by replacing relevant syntax and adding "}" to the next unindented line.
- Convert lists to std::vector by (inefficiently) analysing list's contents (int/str).
- Convert integers to strings and vice versa by detecting and replacing the relevant syntax if "input" is not present in the line.
- Convert pass statements to ";" (by literally finding "pass" and changing it to ";").
- Output new C++ file contents in shell window.
- Ask to save C++ file (.cpp) in the same directory.
- Check if files actually exist in directory, if not, return an error and restart.
The main conversion script is [convert.py](https://github.com/savirsingh/pytocpp/blob/main/convert.py) and the main C++ starter template (boilerplate) the code currently uses is [template.cpp](https://github.com/savirsingh/pytocpp/blob/main/template.cpp).
Thank you for spending the time to look at my repository. I hope it benefits you and you learn something new from it! I feel it's pretty amazing that I was able to make this without using any artificial intelligence packages (I didn't want it to have any dependencies).
I must admit, I will be using this in future!
- Savir Singh (kopichiki@gmail.com)