Automatically create modules instead of failing with ModuleNotFound
Project description
module-found
module-found is a fun, experimental Python project that takes the magic of Python's import system to a whole new level. Ever tried to import a module that doesn't exist? No worries, module-found has your back. Using OpenAI, this package dynamically generates the module and its functions on-the-fly.
In the gif above, all modules imported were generated by AI.
Disclaimer
module-found is purely for educational and entertainment purposes. It's not intended for production use. If you're here for serious business, you might want to look elsewhere. But if you're here for a ridiculous and creative example of what you can do with Python's import machinery, you're in the right place!
Installation
You can install module-found directly from PyPI:
pip install module-found
Environment Variables
To make the most out of module-found, you'll need to configure a couple of environment variables:
MODULE_FOUND_KEY
: Your OpenAI API key. This is required for generating modules and functions on-the-fly.MODULE_FOUND_MODEL
: (Optional) The model to use for generating code. Defaults to 'gpt-4o-mini' if not specified.
Make sure to set these environment variables before using module-found. You can do this in your shell:
export MODULE_FOUND_KEY="your-openai-api-key"
export MODULE_FOUND_MODEL="gpt-4o"
Alternatively, you can pass these values directly to the setup() function in your code:
from module_found import setup
setup(api_key="your-openai-api-key", model="gpt-4o")
Automatic Setup
When you install module-found via pip, it automatically enables itself thanks to a module_found.pth file included in the package. This file ensures that module_found.setup()
is called as soon as the package is installed, so you can start importing non-existent modules right away.
NOTE: Automatic setup does not work when installing in development mode.
Usage
Once installed and configured, you can start importing any module, and module-found will handle the rest. Here's an example:
>>> from cs_interview_questions import pascal_triangle
>>> pascal_triangle(10)
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1], [1, 6, 15, 20, 15, 6, 1], [1, 7, 21, 35, 35, 21, 7, 1], [1, 8, 28, 56, 70, 56, 28, 8, 1], [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]]
Each module generated by module-found has an attribute called code
which can be used to check out the code that was generated. Here's an example:
>>> from emojifier import emojify
>>> print(emojify(msg='hello world'))
👋 🌍
>>> import emojifier
>>> print(emojifier.code)
def emojify(**kwargs):
msg = kwargs.get('msg', '')
emoji_dict = {
'hello': '👋',
'world': '🌍'
}
for word, emoji in emoji_dict.items():
msg = msg.replace(word, emoji)
return msg
How It Works
module-found leverages Python's sys.meta_path to hook into the import system. When you try to import a module that doesn't exist, module-found steps in, generates a LazyModule object, then, when a function is called it generates its code using OpenAI's API taking into account the passed arguments and keywords arguments.
Here's a brief overview of the key components:
- AiFinder: This is the class responsible for catching failed imports and delegating the module creation to AiLoader.
- AiLoader: Handles the actual module creation using OpenAI and loads it into your Python environment.
- LazyModule and LazyFunction: These are responsible for dynamically generating constants and functions within the newly created modules.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file module_found-0.2.1.tar.gz
.
File metadata
- Download URL: module_found-0.2.1.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.8.10 Linux/5.15.153.1-microsoft-standard-WSL2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f4bc360e91cf180e6c780d4619664f0edec656621ba2e3dc30ba50441b9a989 |
|
MD5 | 8ceac8fda17c244983dd41405d75dbc7 |
|
BLAKE2b-256 | 1108ca7d71846a2db3e8b06a800275d7a2f8a8b9c1d811c5379ea646f0899465 |
File details
Details for the file module_found-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: module_found-0.2.1-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.8.10 Linux/5.15.153.1-microsoft-standard-WSL2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 44f985582d3a665db2fe6c47c44b0b24e5367e268c95833a953b294d31de9954 |
|
MD5 | d81c685f72d18d31f4fde19f28833fc1 |
|
BLAKE2b-256 | 603a5678accc1405853eb988e7a5807048173d5bfbdaf66eea3cf278cc775ad4 |