Skip to content

Commit 3e2e6cb

Browse files
committed
first commit
0 parents  commit 3e2e6cb

File tree

6 files changed

+342
-0
lines changed

6 files changed

+342
-0
lines changed

README.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Alien Language Translator 🛸
2+
3+
[![PyPI Version](https://img.shields.io/pypi/v/alien_language)](https://pypi.org/project/alien_language/)
4+
[![License](https://img.shields.io/pypi/l/alien_language)](https://github.com/ishanoshada/alien-language/blob/main/LICENSE)
5+
[![Python Versions](https://img.shields.io/pypi/pyversions/alien_language)](https://pypi.org/project/alien_language/)
6+
[![GitHub Stars](https://img.shields.io/github/stars/ishanoshada/alien-language)](https://github.com/ishanoshada/alien-language/stargazers)
7+
[![GitHub Issues](https://img.shields.io/github/issues/ishanoshada/alien-language)](https://github.com/ishanoshada/alien-language/issues)
8+
9+
**Alien Language Translator** is a fun and creative Python package that translates Python code into an alien-like syntax. Whether you're learning Python, teaching programming, or just looking for a fun way to experiment with code, this package is for you!
10+
11+
---
12+
13+
## Features ✨
14+
15+
- **Translate Python to Alien Language**: Convert Python code into a unique alien-like syntax.
16+
- **Translate Alien Language to Python**: Convert alien code back into Python.
17+
- **Command-Line Interface (CLI)**: Easily translate files or code snippets via the command line.
18+
- **Interactive Mode**: Translate Python code in real-time using an interactive shell.
19+
- **Lightweight and Easy to Use**: No dependencies, just pure Python fun!
20+
21+
---
22+
23+
## Installation 🛠️
24+
25+
You can install the `alien_language` package via pip:
26+
27+
```bash
28+
pip install alien-language
29+
```
30+
31+
---
32+
33+
## Usage 🚀
34+
35+
### 1. Translate Python to Alien Language
36+
37+
To translate a Python file (`my_script.py`) into alien language and save the output to `alien_script.alien`:
38+
39+
```bash
40+
alien-translate --to-alien --input my_script.py --output alien_script.alien
41+
```
42+
43+
### 2. Translate Alien Language to Python
44+
45+
To translate an alien language file (`alien_script.alien`) back into Python and save the output to `my_script.py`:
46+
47+
```bash
48+
alien-translate --to-python --input alien_script.alien --output my_script.py
49+
```
50+
51+
### 3. Interactive Mode
52+
53+
Start the interactive mode to translate Python code into alien language in real-time:
54+
55+
```bash
56+
alien-translate --interactive
57+
```
58+
59+
Example:
60+
61+
```plaintext
62+
>>> print("Hello, Alien!")
63+
zorp("Hello, Alien!")
64+
```
65+
66+
Type `exit` to quit the interactive mode.
67+
68+
---
69+
70+
## Examples 📚
71+
72+
### Python Code
73+
74+
```python
75+
def greet(name):
76+
if name == "Alien":
77+
print("Hello, Alien!")
78+
else:
79+
print("Hello, Human!")
80+
```
81+
82+
### Translated Alien Code
83+
84+
```plaintext
85+
plorp greet(name) [
86+
glorp name ==== "Alien" <
87+
zorp("Hello, Alien!")
88+
florp <
89+
zorp("Hello, Human!")
90+
>
91+
]
92+
```
93+
94+
---
95+
96+
## Contributing 🤝
97+
98+
We welcome contributions! If you'd like to contribute to the project, please follow these steps:
99+
100+
1. Fork the repository on [GitHub](https://github.com/ishanoshada/alien-language).
101+
2. Create a new branch for your feature or bugfix.
102+
3. Commit your changes and push them to your fork.
103+
4. Submit a pull request with a detailed description of your changes.
104+
105+
For bug reports or feature requests, please open an issue on the [GitHub Issues](https://github.com/ishanoshada/alien-language/issues) page.
106+
107+
---
108+
109+
## License 📜
110+
111+
This project is licensed under the **MIT License**. See the [LICENSE](LICENSE) file for details.
112+
113+
---
114+
115+
## Support 💖
116+
117+
If you find this project useful, please consider giving it a ⭐️ on [GitHub](https://github.com/ishanoshada/alien-language). Your support helps us improve and maintain the project!
118+
119+
---
120+
121+
## Contact 📧
122+
123+
For questions or feedback, feel free to reach out:
124+
125+
- **Author**: Ishan Oshada
126+
- **Email**: ishan.kodithuwakku.offical@gmail.com
127+
- **GitHub**: [ishanoshada](https://github.com/ishanoshada)
128+
- **Project URL**: [https://github.com/ishanoshada/alien-language](https://github.com/ishanoshada/alien-language)
129+

alien_language/__init__.py

Whitespace-only changes.

alien_language/translator.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import re
2+
3+
# Define the alien language mapping
4+
alien_language = {
5+
# Keywords
6+
"print": "zorp",
7+
"if": "glorp",
8+
"elif": "glorpflorp",
9+
"else": "florp",
10+
"for": "blorp",
11+
"while": "slorp",
12+
"def": "plorp",
13+
"return": "vorp",
14+
"import": "zorport",
15+
"from": "froop",
16+
"class": "clorp",
17+
"try": "trorp",
18+
"except": "exorp",
19+
"with": "worp",
20+
"as": "azorp",
21+
"and": "&zorp",
22+
"or": "|zorp",
23+
"not": "!zorp",
24+
"in": "inzorp",
25+
"is": "iszorp",
26+
"None": "Norp",
27+
"True": "Troorp",
28+
"False": "Flooorp",
29+
30+
# Symbols
31+
"+": "~",
32+
"-": "~~",
33+
"*": "~~~",
34+
"/": "~~~~",
35+
"=": "==",
36+
"==": "====",
37+
"!=": "!!==",
38+
"<": "<<",
39+
">": ">>",
40+
"<=": "<<==",
41+
">=": ">>==",
42+
"(": "[", # Translate ( to [
43+
")": "]", # Translate ) to ]
44+
"{": "<",
45+
"}": ">",
46+
"[": "[[", # Translate [ to [[
47+
"]": "]]", # Translate ] to ]]
48+
",": "..",
49+
":": "::",
50+
".": "°",
51+
}
52+
53+
54+
55+
def translate_to_alien(code):
56+
try:
57+
lines = code.split('\n')
58+
alien_code = []
59+
for line in lines:
60+
# Handle indentation
61+
indentation = re.match(r'^\s*', line).group()
62+
# Translate the rest of the line
63+
translated_line = line.strip()
64+
for key, value in alien_language.items():
65+
translated_line = re.sub(r'\b' + re.escape(key) + r'\b', value, translated_line)
66+
# Fix parentheses to square brackets
67+
translated_line = translated_line.replace("(", "[").replace(")", "]")
68+
alien_code.append(indentation + translated_line)
69+
return '\n'.join(alien_code)
70+
except Exception as e:
71+
return f"Translation error: {e}"
72+
73+
74+
75+
def translate_to_python(code):
76+
try:
77+
lines = code.split('\n')
78+
python_code = []
79+
for line in lines:
80+
# Handle indentation
81+
indentation = re.match(r'^\s*', line).group()
82+
# Translate the rest of the line
83+
translated_line = line.strip()
84+
for key, value in alien_language.items():
85+
translated_line = re.sub(r'\b' + re.escape(value) + r'\b', key, translated_line)
86+
# Fix brackets
87+
translated_line = translated_line.replace("[", "(").replace("]", ")")
88+
python_code.append(indentation + translated_line)
89+
return '\n'.join(python_code)
90+
except Exception as e:
91+
return f"Translation error: {e}"
92+
93+
94+
def interactive_mode():
95+
print("Enter Python code to translate to Alien language. Type 'exit' to quit.")
96+
while True:
97+
code = input(">>> ")
98+
if code.lower() == "exit":
99+
break
100+
translated_code = translate_to_alien(code)
101+
print(translated_code)
102+
103+
def main():
104+
import argparse
105+
106+
parser = argparse.ArgumentParser(description="Translate Python code to Alien language and vice versa.")
107+
parser.add_argument("--to-alien", action="store_true", help="Translate Python code to Alien language.")
108+
parser.add_argument("--to-python", action="store_true", help="Translate Alien language code to Python.")
109+
parser.add_argument("--input", type=str, help="Input file to translate.")
110+
parser.add_argument("--output", type=str, help="Output file to save the translation.")
111+
parser.add_argument("--interactive", action="store_true", help="Start interactive mode.")
112+
args = parser.parse_args()
113+
114+
if args.interactive:
115+
interactive_mode()
116+
return
117+
118+
if not args.input:
119+
print("Error: Input file is required.")
120+
return
121+
122+
try:
123+
with open(args.input, 'r') as file:
124+
code = file.read()
125+
except FileNotFoundError:
126+
print(f"Error: File '{args.input}' not found.")
127+
return
128+
129+
if args.to_alien:
130+
translated_code = translate_to_alien(code)
131+
elif args.to_python:
132+
translated_code = translate_to_python(code)
133+
else:
134+
print("Error: Specify --to-alien or --to-python.")
135+
return
136+
137+
if args.output:
138+
try:
139+
with open(args.output, 'w') as file:
140+
file.write(translated_code)
141+
print(f"Translation saved to '{args.output}'.")
142+
except Exception as e:
143+
print(f"Error writing to file: {e}")
144+
else:
145+
print(translated_code)
146+
147+
if __name__ == "__main__":
148+
main()

setup.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from setuptools import setup, find_packages
2+
from pathlib import Path
3+
4+
# Read the long description from README.md
5+
this_directory = Path(__file__).parent
6+
long_description = (this_directory / "README.md").read_text(encoding="utf-8")
7+
8+
setup(
9+
name="alien_language",
10+
version="0.2.2",
11+
packages=find_packages(),
12+
entry_points={
13+
"console_scripts": [
14+
"alien-translate=alien_language.translator:main",
15+
],
16+
},
17+
description="A Python to Alien Language translator. Translate Python code into a fun, alien-like syntax!",
18+
long_description=long_description,
19+
long_description_content_type="text/markdown", # Specify the format of the long description
20+
author="Ishan Oshada",
21+
author_email="ishan.kodithuwakku.offical@gmail.com",
22+
url="https://github.com/ishanoshada/alien-language",
23+
project_urls={
24+
"Source Code": "https://github.com/ishanoshada/alien-language",
25+
"Bug Tracker": "https://github.com/ishanoshada/alien-language/issues",
26+
"Documentation": "https://github.com/ishanoshada/alien-language#readme",
27+
},
28+
keywords=[
29+
"alien language",
30+
"python translator",
31+
"fun programming",
32+
"code translation",
33+
"python to alien",
34+
"programming language",
35+
],
36+
classifiers=[
37+
"Development Status :: 4 - Beta",
38+
"Intended Audience :: Developers",
39+
"Intended Audience :: Education",
40+
"License :: OSI Approved :: MIT License",
41+
"Programming Language :: Python :: 3",
42+
"Programming Language :: Python :: 3.7",
43+
"Programming Language :: Python :: 3.8",
44+
"Programming Language :: Python :: 3.9",
45+
"Programming Language :: Python :: 3.10",
46+
"Programming Language :: Python :: 3.11",
47+
"Operating System :: OS Independent",
48+
"Topic :: Software Development :: Libraries :: Python Modules",
49+
"Topic :: Software Development :: Interpreters",
50+
"Topic :: Education",
51+
"Topic :: Games/Entertainment",
52+
],
53+
license="MIT", # Explicitly specify the license
54+
55+
)

tests/alien_script.alien

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
plorp greet[[name]:
2+
glorp name == "Alien":
3+
zorp["Hello, Alien!"]
4+
florp:
5+
zorp["Hello, Human!"]

tests/my_script.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def greet(name):
2+
if name == "Alien":
3+
print("Hello, Alien!")
4+
else:
5+
print("Hello, Human!")

0 commit comments

Comments
 (0)