Skip to main content

Establish your variable names in collaboration with MIT

Project description

sutdobfs

a gift from a senior to the final batch of students taking the last round of Digital World in 2020

Singapore University of Technical Difficulties Obfuscator

Is normal Python code too boring? Do you want to make your code more d a n k? Don't want your friend to copy your Python homework? Want to make your Digital World Prof's life hard when grading your 1D/2D assignments (and get zero in the process)?

Introducing sutdobfs, the SUTD Obfuscator for Python. With this tool, easily turn your variable and (inner) function names into something established in collaboration with MIT.

Before (99 bottles of beer):

def main():
    def sing(b, end):
        print(b or 'No more', 'bottle' + ('s' if b - 1 else ''), end)

    for i in range(99, 0, -1):
        sing(i, 'of beer on the wall,')
        sing(i, 'of beer,')
        print('Take one down, pass it around,')
        sing(i - 1, 'of beer on the wall.\n')

After (99 bottles of DANK MEMES):

def main():
    def professional_practice_programme(established_in_collaboration_with_MIT, professional_practice_programme_copy):
        print(established_in_collaboration_with_MIT or 'No more', 'bottle' + ('s' if established_in_collaboration_with_MIT - 1 else ''), professional_practice_programme_copy)

    for eleven_to_one_student_to_faculty_ratio in range(99, 0, -1):
        professional_practice_programme(eleven_to_one_student_to_faculty_ratio, 'of beer on the wall,')
        professional_practice_programme(eleven_to_one_student_to_faculty_ratio, 'of beer,')
        print('Take one down, pass it around,')
        professional_practice_programme(eleven_to_one_student_to_faculty_ratio - 1, 'of beer on the wall.\n')

The best part? This tool actually produces real functioning code you can submit on Vocareum! Now you don't have to worry about getting hit with plagiarism warnings anymore.

Installation and Usage

Using on Vocareum

For maximum dank, why not use it directly on Vocareum itself?

In the Terminal window of your Vocareum workspace, enter the following:

pip install --user sutdobfs

Now you can meme your homework files in the Vocareum workspace:

python -m sutdobfs your_homework_file.py

This produces a new file in your workspace called your_homework_file.sutd.py, filled with glorious dank memes.

Because Vocareum workspaces are ephemeral (i.e. they may be destroyed when you leave the workspace), you may need to rerun the installation command if you leave Vocareum and come back later.

Local Installs

Open your terminal (or anaconda prompt if you installed anaconda – find it in your start menu) and type the following

pip install sutdobfs

Usage is the same:

python -m sutdobfs your_file.py

This outputs the obfuscated file in your the same directory called your_file.sutd.py. The output file name and location can be changed in Advanced Usage.

On non-conda Python installations on macOS, you msut replace pip with pip3 andpython with python3. This is because Apple still ships macOS with a non-removable installation of Python 2, which will be called when you just use the word python (unless you change your path variables).

How this works

sutdobfs uses the tokenizer module in the Python standard library to parse through source files. sutdobfs will scan through your code and identify variable and function names that are safe to rename: only names in the local and enclosed scopes will be renamed. Candidate replacements are pulled from a "dictionary" (actually a .txt file) of memes to replace these variable names. In case of a name collision (too few memes), _copy will be appended to the end of the variable name. Finally, a new Python file (same filename ending with .sutd.py in the same directory by default) containing the memed names is be created.

The default list of memes can be found in the memes.txt file. Feel free to add more memes to the list using GitHub! If you're new to GitHub, this is a great way to learn how to use GitHub to collaborate – read the contributing guide for more information.

Advanced Usage

Custom output path

Simply add another argument to the command line to customize the path of the output file:

python -m sutdobfs input_file.py output_file.py

Random Names for Memes

By default, names are chosen using hashing: that means the same variable name will always result in the same meme (for the same meme dictionary). If you would like a random meme to be chosen every time you run the obfsucator, add the --random option:

python -m sutdobfs input_file.py --random

Sequential Names for Memes

To guarantee that all memes in the dictionary are used before memes are recycled, pass the --sequential (or --seq) argument:

python -m sutdobfs input_file.py --seq

This will assign memes based on the order sutdobfs encounters names in your source code. This can be combined with the --random option:

python -m sutdobfs input_file.py --seq --random

Custom Meme Dictionaries

You can specify your own text file containing memes to be used in the replacement process:

python -m sutdobfs input_file.py --memes your_meme.txt

Python 3 supports unicode characters in other languages (but not emoji). Get creative!

Here's an example using the jojo.txt meme dictionary:

def main():
    def even_speedwagon_is_afraid(ORA_ORA_ORA_ORA, オラオラオラオラオラオラ):
        print(ORA_ORA_ORA_ORA or 'No more', 'bottle' + ('s' if ORA_ORA_ORA_ORA - 1 else ''), オラオラオラオラオラオラ)

    for ムダムダムダムダムダムダ in range(99, 0, -1):
        even_speedwagon_is_afraid(ムダムダムダムダムダムダ, 'of beer on the wall,')
        even_speedwagon_is_afraid(ムダムダムダムダムダムダ, 'of beer,')
        print('Take one down, pass it around,')
        even_speedwagon_is_afraid(ムダムダムダムダムダムダ - 1, 'of beer on the wall.\n')

Note that your custom filename cannot be the same as the built-in ones found in the meme folder, otherwise the built-in files will be used instead.

Limitations

At the moment, this tool cannot meme f-strings, because the tokenzier module reads f-strings as a single giant string. I am working hard on a f-string lexer, in the meantime, please use the older str.format method instead.

This tool will break if your code attempts to perform imports in a local scope. I will not fix this, because you're not supposed to use the import keyword like that anyway.

This tool is offered on a best effort basis with absolutely no warranty. If you find a bug or have a suggestion, please open an issue on this GitHub repository and include the sample file that you tried to meme.

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

sutdobfs-1.0.3.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

sutdobfs-1.0.3-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file sutdobfs-1.0.3.tar.gz.

File metadata

  • Download URL: sutdobfs-1.0.3.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.6

File hashes

Hashes for sutdobfs-1.0.3.tar.gz
Algorithm Hash digest
SHA256 f861d487f66835555ff9a671a644ae885fd32c70949dee085cb5aeca492f3652
MD5 22daa02e1ec26855b5e6ea5296adcff4
BLAKE2b-256 f6d05cdb0abbdcbcd685e45d8c74e613cf13acdb956333f9dfe5d35c112ed7bc

See more details on using hashes here.

File details

Details for the file sutdobfs-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: sutdobfs-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.6

File hashes

Hashes for sutdobfs-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9d5bd20965c2aaa95e32880213a1316e151d24bf6764d3f7915b318620ab1014
MD5 eccb1a320a995c26691ea65a03e69c30
BLAKE2b-256 abe3a211fe4a4f7ef3ee97830e64de0476aa8fa1c88afae0d2c7629314ca05f6

See more details on using hashes here.

Supported by

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