| Name | exposedfunctionality JSON |
| Version |
0.3.17
JSON |
| download |
| home_page | None |
| Summary | tool to expose functionalities to multiple tools |
| upload_time | 2024-10-22 11:13:00 |
| maintainer | None |
| docs_url | None |
| author | Julian Kimmig |
| requires_python | >=3.10 |
| license | MIT |
| keywords |
|
| VCS |
 |
| bugtrack_url |
|
| requirements |
No requirements were recorded.
|
| Travis-CI |
No Travis.
|
| coveralls test coverage |
|
# ExposedFunctionality
ExposedFunctionality is a Python library designed to facilitate the interaction between backend code and frontend interfaces. It enables developers to expose backend methods and variables in a structured and secure way, making it easier to integrate with front-end systems or API endpoints. This library is particularly useful in scenarios where backend logic needs to be accessed or manipulated from a front-end application or through web API calls.
## Features
- **Function Exposition:** Expose backend functions with added metadata, such as parameter types, return types, and descriptions, making it easier for frontend applications to understand and use these functions.
- **Variable Exposition:** Expose backend variables in a controlled manner, with support for type enforcement, default values, and change events.
- **Docstring Parsing:** Automatically parse function docstrings to extract parameter and return value descriptions, further enriching the exposed method's metadata.
- **Type Safety:** Enforce type checks on exposed variables and function parameters to reduce runtime errors and ensure data integrity.
- **Event Handling:** Support for change events on exposed variables, allowing the frontend to react to changes in the backend state.
- **Middleware Integration:** Apply middleware functions to exposed variables for additional processing or validation before setting their values.
- **Dynamic Addition:** Dynamically add exposed functions and variables to instances or classes, enhancing flexibility in runtime object configuration.
## Installation
To install ExposedFunctionality, use pip:
```bash
pip install exposedfunctionality
```
## Usage
### Exposing Functions
To expose a backend function, use the `exposed_method` decorator. This allows you to specify metadata such as the method's name, input parameters, and output parameters.
```python
from exposedfunctionality import exposed_method
@exposed_method(name="calculate_sum", inputs=[{"name": "a", "type": "int"}, {"name": "b", "type": "int"}], outputs=[{"name": "result", "type": "int"}])
def add(a, b):
"""Calculate the sum of two numbers."""
return a + b
```
To retrieve exposed methods from an object (either an instance or a class), you can use the `get_exposed_methods` function provided by the `exposedfunctionality` package. This function scans an object for methods that have been decorated with `@exposed_method` and collects them into a dictionary, making it easy to access and utilize these methods programmatically, such as when dynamically generating API endpoints or interfaces.
### Example
Consider the following class with an exposed method:
```python
from exposedfunctionality import exposed_method, get_exposed_methods
class MathOperations:
@exposed_method(name="add", inputs=[{"name": "a", "type": "int"}, {"name": "b", "type": "int"}], outputs=[{"name": "sum", "type": "int"}])
def add_numbers(self, a, b):
"""Add two numbers."""
return a + b
```
To retrieve the exposed methods from an instance of `MathOperations`, you would do the following:
```python
math_operations = MathOperations()
exposed_methods = get_exposed_methods(math_operations)
for method_name, (method, metadata) in exposed_methods.items():
print(f"Method Name: {method_name}")
print(f"Metadata: {metadata}")
print(f"Function: {method}")
print("-----")
```
This will output something like:
```
Method Name: add
Metadata: {'name': 'add', 'input_params': [{'name': 'a', 'type': 'int', 'positional': True}, {'name': 'b', 'type': 'int', 'positional': True}], 'output_params': [{'name': 'sum', 'type': 'int'}], 'docstring': {'summary': 'Add two numbers.', 'original': 'Add two numbers.', 'input_params': [], 'output_params': [], 'exceptions': {}}}
Function: <bound method MathOperations.add_numbers of <__main__.MathOperations object at 0x7fcd1830f1f0>>
-----
```
The `get_exposed_methods` function is particularly useful for frameworks or libraries that need to dynamically discover which methods are available for external access, such as in web frameworks for automatically generating API routes or in GUI applications for dynamically creating user interface elements based on the backend logic.
### Exposing Variables
Expose backend variables using the `ExposedValue` descriptor. This enables type checking, default values, and change event handling.
```python
from exposedfunctionality.variables import ExposedValue
class Calculator:
result = ExposedValue("result", default=0, type_=int)
calculator = Calculator()
calculator.result = 5 # Sets the result and enforces type checking
```
### Listening to Variable Changes
You can listen to changes on an exposed variable by attaching an `OnChangeEvent`:
```python
def on_result_change(new_value, old_value):
print(f"Result changed from {old_value} to {new_value}")
calculator.result.add_on_change_callback(on_result_change)
calculator.result = 10 # Triggers the on_result_change callback
```
### Applying Middleware
Use middleware functions to process or validate variable values before they are set:
```python
from exposedfunctionality.variables.middleware import min_max_clamp
class RestrictedCalculator:
result = ExposedValue("result", default=0, type_=int, valuechecker=[min_max_clamp],max=100)
restricted_calculator = RestrictedCalculator()
restricted_calculator.result = 150 # The value will be clamped to 100
```
## Contributing
Contributions are welcome! Please feel free to submit pull requests, report bugs, or suggest features.
## License
This project is licensed under the MIT License. See the LICENSE file for details.
Raw data
{
"_id": null,
"home_page": null,
"name": "exposedfunctionality",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": null,
"author": "Julian Kimmig",
"author_email": "julian.kimmig@linkdlab.de",
"download_url": "https://files.pythonhosted.org/packages/7f/2e/0b696ec0ed65e4de9d0855f3c3d1f2c11a296ab2d134de18cee20aed251f/exposedfunctionality-0.3.17.tar.gz",
"platform": null,
"description": "# ExposedFunctionality\n\nExposedFunctionality is a Python library designed to facilitate the interaction between backend code and frontend interfaces. It enables developers to expose backend methods and variables in a structured and secure way, making it easier to integrate with front-end systems or API endpoints. This library is particularly useful in scenarios where backend logic needs to be accessed or manipulated from a front-end application or through web API calls.\n\n## Features\n\n- **Function Exposition:** Expose backend functions with added metadata, such as parameter types, return types, and descriptions, making it easier for frontend applications to understand and use these functions.\n- **Variable Exposition:** Expose backend variables in a controlled manner, with support for type enforcement, default values, and change events.\n- **Docstring Parsing:** Automatically parse function docstrings to extract parameter and return value descriptions, further enriching the exposed method's metadata.\n- **Type Safety:** Enforce type checks on exposed variables and function parameters to reduce runtime errors and ensure data integrity.\n- **Event Handling:** Support for change events on exposed variables, allowing the frontend to react to changes in the backend state.\n- **Middleware Integration:** Apply middleware functions to exposed variables for additional processing or validation before setting their values.\n- **Dynamic Addition:** Dynamically add exposed functions and variables to instances or classes, enhancing flexibility in runtime object configuration.\n\n## Installation\n\nTo install ExposedFunctionality, use pip:\n\n```bash\npip install exposedfunctionality\n```\n\n## Usage\n\n### Exposing Functions\n\nTo expose a backend function, use the `exposed_method` decorator. This allows you to specify metadata such as the method's name, input parameters, and output parameters.\n\n```python\nfrom exposedfunctionality import exposed_method\n\n@exposed_method(name=\"calculate_sum\", inputs=[{\"name\": \"a\", \"type\": \"int\"}, {\"name\": \"b\", \"type\": \"int\"}], outputs=[{\"name\": \"result\", \"type\": \"int\"}])\ndef add(a, b):\n \"\"\"Calculate the sum of two numbers.\"\"\"\n return a + b\n```\n\nTo retrieve exposed methods from an object (either an instance or a class), you can use the `get_exposed_methods` function provided by the `exposedfunctionality` package. This function scans an object for methods that have been decorated with `@exposed_method` and collects them into a dictionary, making it easy to access and utilize these methods programmatically, such as when dynamically generating API endpoints or interfaces.\n\n### Example\n\nConsider the following class with an exposed method:\n\n```python\nfrom exposedfunctionality import exposed_method, get_exposed_methods\n\nclass MathOperations:\n @exposed_method(name=\"add\", inputs=[{\"name\": \"a\", \"type\": \"int\"}, {\"name\": \"b\", \"type\": \"int\"}], outputs=[{\"name\": \"sum\", \"type\": \"int\"}])\n def add_numbers(self, a, b):\n \"\"\"Add two numbers.\"\"\"\n return a + b\n```\n\nTo retrieve the exposed methods from an instance of `MathOperations`, you would do the following:\n\n```python\nmath_operations = MathOperations()\n\nexposed_methods = get_exposed_methods(math_operations)\n\nfor method_name, (method, metadata) in exposed_methods.items():\n print(f\"Method Name: {method_name}\")\n print(f\"Metadata: {metadata}\")\n print(f\"Function: {method}\")\n print(\"-----\")\n```\n\nThis will output something like:\n\n```\nMethod Name: add\nMetadata: {'name': 'add', 'input_params': [{'name': 'a', 'type': 'int', 'positional': True}, {'name': 'b', 'type': 'int', 'positional': True}], 'output_params': [{'name': 'sum', 'type': 'int'}], 'docstring': {'summary': 'Add two numbers.', 'original': 'Add two numbers.', 'input_params': [], 'output_params': [], 'exceptions': {}}}\nFunction: <bound method MathOperations.add_numbers of <__main__.MathOperations object at 0x7fcd1830f1f0>>\n-----\n```\n\nThe `get_exposed_methods` function is particularly useful for frameworks or libraries that need to dynamically discover which methods are available for external access, such as in web frameworks for automatically generating API routes or in GUI applications for dynamically creating user interface elements based on the backend logic.\n\n### Exposing Variables\n\nExpose backend variables using the `ExposedValue` descriptor. This enables type checking, default values, and change event handling.\n\n```python\nfrom exposedfunctionality.variables import ExposedValue\n\nclass Calculator:\n result = ExposedValue(\"result\", default=0, type_=int)\n\ncalculator = Calculator()\ncalculator.result = 5 # Sets the result and enforces type checking\n```\n\n### Listening to Variable Changes\n\nYou can listen to changes on an exposed variable by attaching an `OnChangeEvent`:\n\n```python\ndef on_result_change(new_value, old_value):\n print(f\"Result changed from {old_value} to {new_value}\")\n\ncalculator.result.add_on_change_callback(on_result_change)\ncalculator.result = 10 # Triggers the on_result_change callback\n```\n\n### Applying Middleware\n\nUse middleware functions to process or validate variable values before they are set:\n\n```python\nfrom exposedfunctionality.variables.middleware import min_max_clamp\n\nclass RestrictedCalculator:\n result = ExposedValue(\"result\", default=0, type_=int, valuechecker=[min_max_clamp],max=100)\n\nrestricted_calculator = RestrictedCalculator()\nrestricted_calculator.result = 150 # The value will be clamped to 100\n```\n\n## Contributing\n\nContributions are welcome! Please feel free to submit pull requests, report bugs, or suggest features.\n\n## License\n\nThis project is licensed under the MIT License. See the LICENSE file for details.\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "tool to expose functionalities to multiple tools",
"version": "0.3.17",
"project_urls": {
"download": "https://pypi.org/project/exposedfunctionality/#files",
"homepage": "https://github.com/JulianKimmig/exposedfunctionality",
"source": "https://github.com/JulianKimmig/exposedfunctionality",
"tracker": "https://github.com/JulianKimmig/exposedfunctionality/issues"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "539c2c2c1b4e8cab9b768f8426b9fa2dd0fa0c1bf0cdf4b885b7e6e5cc5199a8",
"md5": "aeed1c720d1b638a787b99140ece23fe",
"sha256": "43fa34c6d1264b87d4ba0cd7404a0568a808dbc7eac535275302b2b9468a703d"
},
"downloads": -1,
"filename": "exposedfunctionality-0.3.17-py3-none-any.whl",
"has_sig": false,
"md5_digest": "aeed1c720d1b638a787b99140ece23fe",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 26748,
"upload_time": "2024-10-22T11:12:58",
"upload_time_iso_8601": "2024-10-22T11:12:58.734329Z",
"url": "https://files.pythonhosted.org/packages/53/9c/2c2c1b4e8cab9b768f8426b9fa2dd0fa0c1bf0cdf4b885b7e6e5cc5199a8/exposedfunctionality-0.3.17-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "7f2e0b696ec0ed65e4de9d0855f3c3d1f2c11a296ab2d134de18cee20aed251f",
"md5": "0bf137cb50269dcedea919aed0d037e9",
"sha256": "6b648988551b4480d0741d7b0f22dc62cbe65d253f74d2b7b9057de949d751f3"
},
"downloads": -1,
"filename": "exposedfunctionality-0.3.17.tar.gz",
"has_sig": false,
"md5_digest": "0bf137cb50269dcedea919aed0d037e9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 24059,
"upload_time": "2024-10-22T11:13:00",
"upload_time_iso_8601": "2024-10-22T11:13:00.334905Z",
"url": "https://files.pythonhosted.org/packages/7f/2e/0b696ec0ed65e4de9d0855f3c3d1f2c11a296ab2d134de18cee20aed251f/exposedfunctionality-0.3.17.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-22 11:13:00",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "JulianKimmig",
"github_project": "exposedfunctionality",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"lcname": "exposedfunctionality"
}