Skip to main content

A preprocessor to turn python code with braces into python

Project description

CurlyPy: Python with Brackets

CurlyPy is a preprocessor that translates Python code written using curly braces ({}) into standard Python, making indentation obsolete. It provides the flexibility to use semicolons (;) as optional statement separators, making Python syntax more familiar to developers accustomed to languages like C, Java, or JavaScript.

With CurlyPy, you can write Python code using braces to define code blocks, eliminating the need for indentation while still retaining Python's powerful features.

Key Features

  • Curly Braces for Code Blocks: Use {} to denote the start and end of code blocks, removing the need for indentation.
  • Semi-Mandatory Semicolons: Semicolons (;) are supported as optional separators between statements, but they are still not strictly required after every statement. They will, however, be required if you are writing multiple instructions in the same line.
  • Flexible Syntax: Write Python code with a more structured format, closer to languages like C, Java, or JavaScript, while keeping all the strengths of Python.
  • Compatible with Python: CurlyPy preprocesses your code into standard Python, so it can be executed by any Python interpreter.
  • Dictionary and Set Types: The standard Python dictionary and set types are available in CurlyPy, using type hints.

Valid CurlyPy syntax:

# Dictionaries and sets are defined using type hints
dictionary_test: dict[str, str] = {
   "foo":"bar",
   "baz":{
      "qux":"quux"
   }
}
set_test: set[str] = {"foo", "bar", "baz"}
print(dictionary_test["foo"], set_test)

CurlyPy makes even this syntax possible!

def check_even_odd(num) { print(f"{num} is {'even' if num % 2 == 0 else 'odd'};"); }; check_even_odd(10); check_even_odd(7);

Installation

Install from PyPI:

pip install curlypy

Usage

Once you have CurlyPy installed, you can preprocess your Python files written with curly braces and optional semicolons into standard Python.

Command line usage:

# Example usage
>> curlypy path/to/cpy/file

# To get help
>> curlypy -h
usage: curlypy [-h] [--output OUTPUT] [--norun] [--force] [--keep] filename ...

Translate and run python code with braces

positional arguments:
  filename         The filename to translate.
  args             Arguments to pass to the translated code.

options:
  -h, --help       show this help message and exit
  --output OUTPUT  The output filename. Defaults to _curlypy_<filename>.py
  --norun          Set this flag if you dont want to run the translated code directly after translating.
  --force          Set this flag if you want to force the translation. i.e. dont perform any checks. Can output non working code. Defaults to False.
  --keep           Set this flag if you want to keep the translated file after running it.

Module usage:

# some_file.py
from curlypy import CurlyPyTranslator

translator = CurlyPyTranslator(filename="curlypython.cpy")
translated_code: str = translator.translate()

with open("translated.py", "+w") as out:
    out.write(translated_code)
# Then run translated.py using python

CurlyPy will convert your code with brackets into traditional Python with correct indentation and block structures.

Example

Here's how Python code with curly braces and semicolons looks with CurlyPy:

def HelloWorld(name: str) {
	if name {
		print (f"Hello {name}!");
	}
	else {
		print ("Hello World!");
	}
}

With curlypy, it will be converted into regular Python:

def HelloWorld(name: str) :
   if name :
      print (f"Hello {name}!")
   else :
      print ("Hello World!")

Why CurlyPy?

Python's indentation-based syntax is great for readability but may feel unfamiliar to developers used to brace-based languages like C, Java, or JavaScript. CurlyPy gives you the freedom to write Python code with curly braces, making it easier for those developers to transition to Python without abandoning the structured code block formatting they're used to.

CurlyPy doesn't take away Python's flexibility—if you love type hints and it's strong typing, you can keep using them as is. CurlyPy opens up new possibilities for those who prefer braces.

CurlyPy:

# For loop
for n in range(10){
    if n % 3 == 0 and n % 5 == 0 {
        print("FizzBuzz")
	}
    elif n % 3 == 0 {
		print("Fizz")
	}
    elif n % 5 == 0 {
		print("Buzz")
	}
    else{
        print(n)
	}
}

Translated Python:

# For loop
for n in range(10):
   if n % 3 == 0 and n % 5 == 0 :
      print("FizzBuzz")
   elif n % 3 == 0 :
      print("Fizz")
   elif n % 5 == 0 :
      print("Buzz")
   else:
      print(n)

Error Checking

CurlyPy performs a number of checks before translating your code, to ensure it runs correctly after translation.

simple_int: int = 42
simple_str: str = 'hello'
simple_dict: dict[str, dict] = {'a': {1:2, 'b': {2:3}}
simple_float: float = 3.14
simple_bool: bool = True

raises: curlypy.errors.CurlyPySyntaxError: Unmatched '{' on line 3, col 32

Upcoming improvements

  • Advanced preprocessing features
  • Advanced error checking
  • Possibly a new name
  • More in depth documentation

Contributing

Contributions are welcome! If you want to contribute to CurlyPy or report an issue, please feel free to open an issue or submit a pull request.

If you run into an issue or an edge case, please open an issue and help us improve CurlyPy!

Steps to Contribute:

  1. Fork the repository
  2. Create a new branch for your feature or bug fix
  3. Submit a pull request with a clear description of the change

License

This project is licensed under the GNU General Public License. See the LICENSE file for details.


Enjoy writing Python with the structure and familiarity of braces with CurlyPy!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

curlypy-0.1.1.tar.gz (22.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

curlypy-0.1.1-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file curlypy-0.1.1.tar.gz.

File metadata

  • Download URL: curlypy-0.1.1.tar.gz
  • Upload date:
  • Size: 22.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.4

File hashes

Hashes for curlypy-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ace640f06c7f638e0c9f082654fe531d0dd6460d34752c6bbc9cde061eb2cedc
MD5 60b086594cd343d82c95e8884e8ff111
BLAKE2b-256 1b6d26953a6e049e96c3db5ddc034c3916d8fcbb30f7ffca49928e069f745b62

See more details on using hashes here.

File details

Details for the file curlypy-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: curlypy-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.4

File hashes

Hashes for curlypy-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e5cda27e8407f48b9ad5f74a520766a1df45b713d903a7856d3dbf91addca69a
MD5 9dee90bfec9b43a43bd787276ffa615f
BLAKE2b-256 9ec01e71f6e32d35386aa4603c7e72f97e5e35e5ecb3eac4755716fff43cfac0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page