Managing complex routing logic can become difficult when you need to filter and rank multiple paths based on specific structural rules. It is hard to organize how a system decides which path to take when those rules are complex.
It filters and ranks different paths of execution based on specific patterns and structural constraints. It acts as a smart traffic controller for how a program moves through different options.
It simplifies handling complex routing logic by providing a clear way to filter and rank paths dynamically.
It was run inside an isolated container with no network access. This is the exact command and the real output it produced — captured process output, not written by a model.
$ python3 path_pattern_router.py (no output)
A screenshot of that run.
A clean run proves this does what is shown above, in a CPU-only sandbox. It is a small research demo — not a production tool, and nothing here was published anywhere.
All of it — 60 lines, one file, standard library only.
# Updated path_pattern_router.py with Route Grouping and Path Tagging
import glob
import os
from fnmatch import fnmatchcase
from collections import defaultdict
# Define routes with glob patterns, priorities, and tags
ROUTES = [
{"pattern": "src/**/api/**/*.py", "priority": 3, "tags": ["api", "service"]},
{"pattern": "tests/**/*_test.py", "priority": 2, "tags": ["test"]},
{"pattern": "src/**/services/**/*.py", "priority": 3, "tags": ["services"]},
{"pattern": "src/**/models/**/*.py", "priority": 3, "tags": ["models"]},
{"pattern": "src/**/utils/**/*.py", "priority": 3, "tags": ["utils"]},
{"pattern": "**/*.py", "priority": 1, "tags": ["general"]}
]
def match_route(path):
max_priority = 0
best_route = None
for route in ROUTES:
if fnmatchcase(path, route['pattern']):
if route['priority'] > max_priority:
max_priority = route['priority']
best_route = route
return best_route, max_priority
def main(directory='.'):
# Find all Python files
all_files = glob.glob(os.path.join(directory, '**/*.py'), recursive=True)
matched_files = []
for file_path in all_files:
relative_path = os.path.relpath(file_path, directory)
route, priority = match_route(relative_path)
if route:
matched_files.append((file_path, priority, route['tags']))
# Sort by priority descending
matched_files.sort(key=lambda x: x[1], reverse=True)
# Group files by tags
grouped = defaultdict(list)
for file_path, priority, tags in matched_files:
tag_key = ', '.join(tags)
grouped[tag_key].append((priority, file_path))
# Output original behavior (priority + path)
print("== Original Priority-Based Output ==")
for file_path, priority, tags in matched_files:
print(f'{priority}: {file_path}')
# Output new grouping by tags
print("\n== Path Tagging Grouping ==")
for tag_key in sorted(grouped.keys()):
print(f'\n--- {tag_key} ---')
for priority, file_path in grouped[tag_key]:
print(f'{priority}: {file_path}')
if __name__ == '__main__':
main()