Skip to main content

insert block end marks into python code for better formatting and copy paste experience

Project description

PyEnd

Python's significant indentation is a horrible thing that ruins the language for many people.

It's now fixed.

PyEnd introduces end to mark the end of blocks:

from pyend import end

if "tabs" > "spaces":
	print("∎")
end

Why is that useful?

Because it is also a formatter. You can convert indentation-based code (both the file you are working on and the clipboard) into keyword-delimited code. You can then paste away and afterwards reformat your file automatically instead of manually. It should also be quite helpful for refactoring, when the code structure changes and everything has to be re-indented.

Status

This project is still an experiment. Feel free to play around with it and give feedback but don't use it in production code. You may have noticed there are no unit tests yet. Formatting code (especially with significant indentation) is actually not so straight forward and can easily mess things up. That being said, the formatter does some validation in the end. The formatted code is tokenized again and compared to the tokenized original input.

Installation

pip install pyend

Hasn't this whole thing been done before?

In contrast to pindent, it works with Python3 and in contrast to pybrace and pybraces, the output is still valid Python.

Does it stop there?

It sure does not! It also uses tabs instead of spaces for indentation. Because using tabs instead of spaces is much better. However, if you want to be wrong, you can set the --convert-tabs-to-spaces-despite-tabs-being-objectively-better-than-spaces flag, which will convert indentation tabs into 11 spaces. Should you not like 11 spaces for indentation and you would rather enforce your personal taste onto everyone else, you can set the --use-this-many-spaces-per-tab-cuz-as-a-spacist-i-want-uniformity-but-i-dont-want-the-default parameter to whatever your heart desires.

Why are tabs better than spaces?

At the core of the spacecrafters' arguments lies the conviction that their pristine code must look the same everywhere. This idea is just doomed to failure. While a typical space users' code probably looks something like this on their 79 column console or on their printed handouts:

space user code

the exact same code will render like this on my monitor:

my code

so why even bother with indentation width consistency?

Furthermore, Developers Who Use Spaces Make More Money Than Those Who Use Tabs. This is because space invaders need higher monetary compensation to make up for the fact that developers who use tabs are happier in general because they make better life choices.

That should settle it.

Does PyEnd fix all the bad decisions in Python's language design?

Unfortunately not. Empty blocks will still need a pass:

if None:
	pass
end

Furthermore, myList[0:5] still absolutely counter-intuitively only goes up to myList[4] and there is also this atrocity. But hey, it's a start!

What about line breaks?

Line breaks are a Pandora's box that this project is not touching. You have to sprinkle the line breaks in manually (you're much better at this than any tool anyway), or you use another formatter on top of PyEnd.

But in my project I have a variable named end. Am I forced to live under van Rossum's dictatorship then?

PyEnd only recognizes an end on an otherwise empty line (except for comments) as block end mark. Using end otherwise is a bit ugly as every block-ending end then becomes a reference to your variable but it is possible.

This is a great project! You are very clever and handsome! Can you also do this for YAML?

Thanks but no. YAML is screwed up beyond repair, starting with its very funny name and not ending with its ambiguity. Use JSON. It has its problems as well but I feel they aren't really relevant in practice and I also just like it more 😌

Disclosure

If I didn't consider Python to be a great language, I wouldn't invest so much time in this project. But significant indentation has been an absolute PITA and the urge to do something about it has been bugging me with every paste. I finally gave in.

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

pyend-0.0.2.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

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

pyend-0.0.2-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file pyend-0.0.2.tar.gz.

File metadata

  • Download URL: pyend-0.0.2.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for pyend-0.0.2.tar.gz
Algorithm Hash digest
SHA256 d8863f12a784d1b8fd79d8b050d4029bab15dc80d08059a68037a56e268fa3bd
MD5 6af88d257b861cad3bec64998c841402
BLAKE2b-256 2f21c22fe4f01d730e669bb38457bebdd190b75bc425751783a3694f81c12bfe

See more details on using hashes here.

File details

Details for the file pyend-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pyend-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for pyend-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0ba7f05b4376278700880f7daf7753d04235310f000164a8318df01f9a9f5315
MD5 da26bcb5d1138f259a5a7fb63305f0e4
BLAKE2b-256 29d2a62ba191377e5636ecfe097828b651f023cf4a8c9ec3e2a98160e639b2e0

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