A module for '.pyv' file
Project description
PYV
Install
pip install pyv_file_running
Usage
There is a new file extension .pyv which is used to write python code with conditional compilation.
print("start")
$ version <= 3.7; platform == "win32"
print("old windows")
$ version == 3.8
print("python 3.8")
$ platform == "win32"
print("windows")
$ _
print("other")
$
s = f"""{
$ version <= 9.99
"this is contain"
$
}
"""
ss = """
$ version == 9.99
this is still here
$
"""
print("end")
The special syntax $ is used to write conditional compilation code.
The syntax is as follows:
$ condition
code
$
The condition only supports "version", "platform", and "implement" three keywords, and supports the following operators: ==, !=, <=, >=, <, >. For "platform" and "implement", it only support "==" or "!=". The version must contain the major and minor version number (e.g. 3.7).
The "$ _" means that if the condition above are not satisfied, the code below will be analyse.
The "$" means the end of the analyse.
If the syntan appeared in string, it will be ignored.
Comment
You can also use "#" to write comments.
$ version <= 3.7; platform == "win32" # this is a comment
code
$
Exec the file
You can use this module to exec it:
import pyv
global_dict = {}
local_dict = {}
pyv.exec_pyv_file("test.pyv", global_dict, local_dict)
Set a new environment
You can set a new environment:
import pyv
pyv.set_new_env(version=(3, 8), platform="win32", implement="cpython")
Then you can use this environment in the .pyv file:
import pyv
new_env = pyv.set_new_env(version=(3, 8), platform="win32", implement="cpython")
with open("test.pyv", "r") as f:
code = f.read()
final_code = pyv.preprocess_pyv(code, new_env)
with open("test.py", "w") as f:
f.write(final_code)
Write the file to python
You can use this module to write the .pyv file to python file:
import pyv
pyv.witer_pyv_file_to_py("test.pyv", "test.py")
If you have the code string, you can also write it to python file:
import pyv
code = """
$ version <= 3.7; platform == "win32"
print("old_python")
$ _
print("other")
$
"""
pyv.witer_pyv_to_py(code, "test.py")
All apis
pyv.set_new_env(version: tuple[int, int] | None=None, platform: str | None=None, implement: str | None=None) -> dict[str, Any]: ...
pyv.preprocess_pyv(source: str, env=None) -> str: ...
pyv.exec_pyv_source(source: str, filename="<pyv>", globals=None, locals=None, env=None) -> None: ...
pyv.exec_pyv_file(path: str, globals=None, locals=None, env=None) -> None: ...
pyv.witer_pyv_to_py(source: str, filename: str, env=None) -> None: ...
pyv.witer_pyv_file_to_py(path: str, file_name: str, env=None) -> None: ...
License
MIT
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 pyv_file_running-1.0.2.tar.gz.
File metadata
- Download URL: pyv_file_running-1.0.2.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33c480e634af6c0c270f65be78e9bcf773b606ed0a8571642f1f865b9bc0fa15
|
|
| MD5 |
2d06080f4404c90c3028c2c130f6b6c7
|
|
| BLAKE2b-256 |
669d175e581fb341968aee2e4aba2b97344f66e688bec3efe1e2a7ebe29c35f4
|
File details
Details for the file pyv_file_running-1.0.2-py3-none-any.whl.
File metadata
- Download URL: pyv_file_running-1.0.2-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a51d20428e23d379adf047e95618eb1bd6313cee3534412bf197f77bad44d63
|
|
| MD5 |
d73d836e72375229e0e81bb3e222cbb1
|
|
| BLAKE2b-256 |
bc299cd625a95f1a1e28acbe8b3f0c9045d4bf61b1d2c790b50fa3ac3a1f0e03
|