Skip to main content

Adding C-style for loops to Python.

Project description

cursed-for

Adding C-style for loops to Python, because you can.

>>> for (i = 0; i < 10; i += 2):
...     print(i)
0
2
4
6
8

Installation

pip install cursed-for

Usage

Add the following # coding comment at the top of the file in which you wish to curse:

# coding: cursed_for

Then write the cursed for-loops as needed. Check this file for an example.

For an interactive REPL, type cursed-for in your terminal:

$ cursed-for
Cursed Python REPL, 3.10.4 (main, Apr  2 2022, 09:04:19) [GCC 11.2.0]
>>> x = 10
>>> for (;x > 5;x -= 3):
...     print(x)
10
7

But why?

This was made because of a cursed idea I had one night, which I put on twitter:

And although this worked, I didn't like the look of it. But using the methods I used, I was confined by Python's syntax, which really doesn't support the kind of things I'd have liked to do. But then I stumbled upon a couple of packages which gave me exactly the tools I needed to commit this atrocity.

OK, but how is this possible?

I wrote a blog on the approaches I went through to implement this.

The rough iterations that happened during the development are documented in the approach folder. It contains both the AST manipulation method, and the "truly cursed" method.

The "old way" usage

The first version (as shown in the original tweet) is also present in the repository, for archival purposes.

Note that this only really works in a REPL. To start it, run cursedfor.py in the terminal. It's a single file, you can get it by just downloading the one file if you want to.

>>> with _for(i := 0, i < 10, i += 2):
...     print(i)
0
2
4
6
8

Help, something is broken!

You can use the cursed-for-decode command to figure out what code is being generated by the module:

$ cat x.py                                                  
for (i = 1; i < 100; i += 1):
    print(i)

$ cursed-for-decode x.py
i = 1
while i < 100:
    print(i)
    i += 1

If the generated code seems wrong, file a bug report with the code snippet.

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

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

cursed_for-0.3.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file cursed_for-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: cursed_for-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for cursed_for-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0be08e190228c2e7a7c9d5871f029a1ef8e8c4b7ef51031ff6ed0cee363f234e
MD5 f99a9c344132114652b9b54dcdd8c5c0
BLAKE2b-256 f247a0f2b2d4bb464063fc3e3edcf019980cfa2b07f1cf0ab8b465c4bd7203d8

See more details on using hashes here.

Supported by

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