Created
October 29, 2020 03:35
-
-
Save svank/9fbb080ac00641265f62d1e4089d2bf5 to your computer and use it in GitHub Desktop.
Brute-force imports in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from importlib import import_module\n", | |
"from itertools import product\n", | |
"import string\n", | |
"import time" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"max_length = 30\n", | |
"target_module = \"numpy\"\n", | |
"import_as = \"np\"\n", | |
"valid_characters = '._' + string.ascii_lowercase" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Searched through length 1 after 0.00 s\n", | |
"Searched through length 2 after 0.07 s\n", | |
"Searched through length 3 after 1.91 s\n", | |
"Searched through length 4 after 48.43 s\n", | |
"Finished after 780.18 s\n" | |
] | |
} | |
], | |
"source": [ | |
"start = time.time()\n", | |
"done = False\n", | |
"for length in range(1, max_length):\n", | |
" # Generate all possible combinations of N characters, in any order, with repetition\n", | |
" for module_name in product(valid_characters, repeat=length):\n", | |
" module_name = ''.join(module_name)\n", | |
" \n", | |
" # Skip an easter-egg module that prints a message when imported\n", | |
" if module_name == 'this':\n", | |
" continue\n", | |
" \n", | |
" try:\n", | |
" # Try to import the module name\n", | |
" module = import_module(module_name)\n", | |
" \n", | |
" # If it worked, check if it's the module we want\n", | |
" if module.__name__ == target_module:\n", | |
" # If so, insert it into the global namespace\n", | |
" globals()[import_as] = module\n", | |
" done = True\n", | |
" break\n", | |
" except (ModuleNotFoundError, ImportError, TypeError):\n", | |
" pass\n", | |
" if done:\n", | |
" print(f\"Finished after {time.time() - start:.2f} s\")\n", | |
" break\n", | |
" print(f\"Searched through length {length} after {time.time() - start:.2f} s\")\n", | |
"else:\n", | |
" print(\"Search failed\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.arange(10)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.8" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment