Minipy
The minipy
module provides functionalities for minifying and obfuscating Python source code.
This functionality is provided by the Minifier
class.
The Minifier
class provides an interface for both minifying and obfuscating Python source code.
It also provides options to configure how they are performed.
Please consult its documentation for details.
Command-Line Interface
You can use Minipy from the command-line by invoking the minipy
command followed by the name of the Python file you wish to minify.
If you omit the file, then Minipy reads from stdin
.
$ minipy file.py
You can obfuscate a file by passing the --obfuscate
flag.
$ minipy file.py --obfuscate
The obfuscate flag can be accompanied by zero or more -s
flags, each of which takes as its argument the name of a symbol to suppress obfuscation on.
This prevents unwanted obfuscation of public symbols.
Programming Interface
- class minipy.Minifier
Bases:
object
Python code minification, obfuscation, and configuration options.
Minipy is a Python code minification and obfuscation package for Python. This class encapsulates that behavior.
Minipy does not guarantee obfuscation of every symbol. This is because Minipy is designed to operate without the expectation of type annotations and therefore cannot be certain obfuscation is appropriate in all cases. If obfuscation of a symbol would be ambiguous then Minipy will err on the side of caution and not obfuscate it.
Additionally, operations that interact with locals, globals, or object properties by string name are not semantically understood by Minipy and will require manual suppression via
suppress()
to prevent unwanted obfuscation. Further, symbols intended to be unobfuscated, like public API functions, classes and their properties, must be suppressed as well to prevent unwanted obfuscation.- Variables:
indent (int) – Number of space characters used to indent the minified Python code (must be a positive integer). Defaults to 1.
obfuscate (bool) – Determines whether the code is obfuscated or not. Defaults to False.
- property indent: int
Gets the value of indent.
- Returns:
The value of indent.
- Return type:
int
- minify(source: str) str
Minify Python source code.
This method minifies the given Python source code using the attributes configured on the class instance. In the normal usage
- Parameters:
source (str) – Python source code.
- Returns:
Minified source code.
- Return type:
str
- Raises:
SyntaxError – if
source
has malformed Python syntax
- suppress(symbol: str) None
Prevent the symbol from being obfuscated.
When obfuscation is enabled any symbol may be substituted for a shorter replacement. Calling this method with the specified symbol prevents Minipy from replacing said symbol. This can be beneficial for symbols that are part of the public interface of a package or for other symbols which should otherwise not be obfuscated.
- Parameters:
symbol (str) – An identifier that might name a variable, class, or function.
- unsuppress(symbol: str) None
Allow the symbol to be obfuscated.
This removes suppressions added with
suppress()
.- Parameters:
symbol (str) – An identifier that might name a variable, class, or function.
Code Examples
A complete example of minifying source code is demonstrated in the following code snippet. Note that the code snippet includes a code comment which, if uncommented, will obfuscate the code in addition to minifying it.
from minipy import Minifier
c = Minifier()
#c.obfuscate = True # Uncomment to obfuscate the code.
c.minify(("def factorial(num):\n"
" if num < 1:\n"
" return 1\n"
" else:\n"
" return n * factorial(n - 1)"))