Skip to content

Instantly share code, notes, and snippets.

@ManDeJan
Forked from cjsmeele/better_python3
Created June 24, 2020 20:00
Show Gist options
  • Save ManDeJan/696c9a62f6fff862fc1dff025d0d70e4 to your computer and use it in GitHub Desktop.
Save ManDeJan/696c9a62f6fff862fc1dff025d0d70e4 to your computer and use it in GitHub Desktop.
better_python3
#!/usr/bin/env perl
# This adds the post-inc, post-dec, pre-inc and pre-dec operators to python!
# Additionally, it supports the -O3 optimization feature.
# Test! :)
my $optimized = 0;
if (@ARGV and $ARGV[0] eq '-O3') {
shift;
$optimized = 1;
}
open my $python, '|-', '/usr/bin/python3', '-' or die "boooo $1";
while (<>) {
s/^(\s*)([^#]*)(\w+)\s*\+\+(.*)/$1$2$3$4\n$1$3 += 1/;
s/^(\s*)([^#]*)\+\+\s*(\w+)(.*)/$1$3 += 1\n$1$2$3$4/;
s/^(\s*)([^#]*)(\w+)\s*--(.*)/$1$2$3$4\n$1$3 -= 1/;
s/^(\s*)([^#]*)--\s*(\w+)(.*)/$1$3 -= 1\n$1$2$3$4/;
next if $optimized and rand > 0.8;
print $python $_;
}
# Example python script:
#
# y = 0
# for i in range(0, 5):
# print(i, ++y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment