A python library for substituting values in a script.
Project description
foo2bar
foo2bar is a Python library designed to substitute values in variable assignments within a script. It enables you to analyze and modify Python code by replacing variable assignments with new values, providing a cleaner and more efficient approach compared to using regular expressions.
It provides a cleaner approach to value substitution compared to using regular expressions.
Features
- Parse and analyze Python scripts.
- Substitute variable assignments with new values.
- Support for different scopes (global, class, method).
- Safe evaluation of expressions.
Installation
To install foo2bar, use pip:
pip install foo2bar
Quickstart
To quickly get started with foo2bar, let's consider an example. Suppose you have a script named my_script.py:
from foo import do_stuff
my_duration = 10 # duration in seconds
print(do_stuff(my_duration))
You want to create a copy of this script, my_new_script.py, where my_duration is set to 0.5. You can achieve this by running the following command in your terminal:
foo2bar my_script.py raw --output my_new_script.py --my_duration 0.5
After running the command, the newly created my_new_script.py will look like this:
from foo import do_stuff
my_duration = 0.5 # duration in seconds
print(do_stuff(my_duration))
Usage
Command Line Interface
foo2bar provides a command line interface for substituting values in a script.
Substitute Raw
Raw substitution allows you to replace variable assignments with the provided values. Lists must be passed with brackets, dictionaries with curly braces, and strings with additional quotes.
foo2bar <script_path> raw --output <output_path> --my_int 12323 --my_str ''foo bar'' --my_list '[baz, bat]' --my_dict '{a: 2.35, b: None}' --my_none "None"
This feature supports also more complex injections, like 'abs(5 - 10)'.
Warning: In order to inject a string, quotes must be escaped or doubled properly.
Substitute Typed [experimental]
Typed is syntactic sugar to interpret inputs with their types.
foo2bar <script_path> typed --output <output_path> --x 12323 --s "foo bar" --my_typed_list "baz" "bat"
Python API
You can also use foo2bar as a Python library:
from foo2bar.wrapper import CodeWrapper
# Load a script
wrapper = CodeWrapper.from_file("path/to/your_script.py")
# Substitute values
wrapper.substitute_assign_values_global({"x": "100", "y": "200"})
# Get the modified code
print(wrapper.code)
Development
Running Tests
To run the tests, use the following command:
python -m unittest discover tests
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
License
This project is licensed under the MIT License.
Contact
For any questions or inquiries, please contact Martin Favin-Lévêque at mrtichou.dev@gmail.com.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file foo2bar-0.0.2.tar.gz.
File metadata
- Download URL: foo2bar-0.0.2.tar.gz
- Upload date:
- Size: 14.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1151aafc195d493df32da8a81c8f04a4873b34277d1be651e488795fda72846f
|
|
| MD5 |
51301b9033f9f99ba0067a34c3bd41f9
|
|
| BLAKE2b-256 |
70235dc2f64875e713ee0ab161d1f3e91f1ebdaddd565cd9dc994aaa0716a71b
|
File details
Details for the file foo2bar-0.0.2-py3-none-any.whl.
File metadata
- Download URL: foo2bar-0.0.2-py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
201d925bb76373c9ac2d7df2697dc86bcdb920a15b20e6299064df929c6a2317
|
|
| MD5 |
13a8aec760c0fef320bd0e40440af80c
|
|
| BLAKE2b-256 |
d6ba5cc2104a05955453759de1c5e0a1d8984d5d7f18a64b4c360aa152cf8312
|