A simple library for routing with a tree-like structure
Project description
SILVANUS
The simple library for different routing with a tree-like structure
Paradigms:
- Each element is changeable almost beyond recognition, but a combination of them is possible due to common protocols that are well described out of the box.
- Many things are done out of the box (for example, the almost universal SimpleRouter and its two RouterIterators, which implement both the data queue interface with filters and the data bus interface with filters )
- The most universal protocols, which is why it is recommended to write small wrappers on top of routing for frameworks.
Usage
data queue interface with filters using FirstTrueRouterIterator, and data bus interface with filters using AllTrueRouterIterator
import asyncio
from dataclasses import dataclass
from silvanus.routing.simple import SimpleRouter
from silvanus.strategy.routers import FirstTrueRouterIterator, AllTrueRouterIterator
from silvanus.structures import RoutingData
@dataclass(frozen=True)
class SimpleFilter: # Our custom simple filter
value: int
async def __call__(self, data: RoutingData) -> bool:
return data.request_data.get("value", None) == self.value
# silvanus supports all data types as a result of routing,
# but the most useful for frameworks will be the return of functions.
def some_handler(text: str) -> str:
return text * 2
router = SimpleRouter()
router1 = SimpleRouter([SimpleFilter(value=10)], data="wrong")
router2 = SimpleRouter([SimpleFilter(value=15), ], data=some_handler)
router3 = SimpleRouter([SimpleFilter(value=15), ], data="yes!")
router.add_routers([router1, router2, router3])
async def main():
result = await router.route(
data=RoutingData(
request_data={"value": 15}
),
iterator=FirstTrueRouterIterator() # get only first True result
)
assert result("silvanus-") == "silvanus-silvanus-" # our some_handler
result_2 = await router.route(
data=RoutingData(
request_data={"value": 15}
),
iterator=AllTrueRouterIterator() # get all True result
)
assert result_2[0](result_2[1]) == "yes!yes!" # our some_handler and text from second data
asyncio.run(main())
Examples:
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
silvanus-0.1.0b0.tar.gz
(4.8 kB
view details)
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 silvanus-0.1.0b0.tar.gz.
File metadata
- Download URL: silvanus-0.1.0b0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.13.5 Darwin/25.0.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eebd2c1a9b40d6afd4555985f93b1dae6ec2bce34ea0966d03fe30ef1ba21db4
|
|
| MD5 |
cdd061a34dbc768673b6dbced58321c3
|
|
| BLAKE2b-256 |
3ebcc6ad94b73834aafad40f4d8616396b3b8a0dd027c059d864a7e3a1d58227
|
File details
Details for the file silvanus-0.1.0b0-py3-none-any.whl.
File metadata
- Download URL: silvanus-0.1.0b0-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.13.5 Darwin/25.0.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
741d1532ae78c3087d218d59aac559f036502b13b38254426bcce0d5be60a53d
|
|
| MD5 |
c52be8288c2f6e94766f5c8421b8c86e
|
|
| BLAKE2b-256 |
65e0a18a06a4c970f1699272740d4e6a2594b5e5358f93c1d7e935d42cc1fe44
|