Decorated functions as entry points.
Project description
entrypoint
Decorated functions as entry points.
In python, an entry point can be thought of as an explicit function that gets called when the script is run directly from the console.
Defining an entry point requires some boilerplate code, which is abstracted away by this library.
Installing
Python 3.7 or above is required.
pip
Installing with pip
is quite simple:
$ pip install entrypoint
Alternatively, the library can be installed from source:
$ git clone https://github.com/nekitdev/entrypoint.git
$ cd entrypoint
$ python -m pip install .
poetry
Adding entrypoint
to dependencies:
$ poetry add entrypoint
Or directly specifying it in the configuration:
[tool.poetry.dependencies]
entrypoint = "^1.2.1"
Alternatively, the latest version can be included, installing from source:
[tool.poetry.dependencies.entrypoint]
git = "https://github.com/nekitdev/entrypoint.git"
Examples
Decorated
Declare main
function as an entry point:
from entrypoint import entrypoint
@entrypoint(__name__)
def main() -> None:
print("Hello, world!")
Run the script directly from the console:
$ python file.py
Hello, world!
When importing the module, main
does not get called:
>>> import file
>>> # no output
Note
Note that main
gets called immediately, before any code below can be executed:
@entrypoint(__name__)
def main() -> None:
print("-> in main")
print("<- outside")
$ python note.py
-> in main
<- outside
Direct
It is possible to run main
directly:
entrypoint(__name__).call(main)
This method allows to take control over where and when the function gets called.
Check
The library also provides is_main
function that resembles
the common (and standard) way of implementing entry points:
from entrypoint import is_main
if is_main(__name__):
print("Hello, world!")
Async
entrypoint
does not provide any specific functionality to run async functions.
Instead, you can specify, for example, a main
function that runs its async_main
counterpart:
import asyncio
async def async_main() -> None:
print("Hello, world!")
@entrypoint(__name__)
def main() -> None:
asyncio.run(async_main())
Documentation
Documentation is located here.
Support
Please send an email or refer to the official Discord server for support.
Changelog
Changelog can be found here.
Contributing
If you are interested in contributing to entrypoint
, make sure to take a look at the
Contributing Guide, as well as the Code of Conduct.
License
entrypoint
is licensed under the MIT License terms. See License for details.
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
Hashes for entrypoint-1.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b21b236e2545c3aa5d777272a0359afa7e677362f8e57487732768b936d1612c |
|
MD5 | 2684d2755387fcdc1c64dfea90f49081 |
|
BLAKE2b-256 | 4d2c716ef5ebd3b79a9b4f5302a8789187d8428a7ffb9fc64775dcc4d8a7f5ea |