Skip to main content

SimpleWorkerNet

Python-клиент для REST API WorkerNet с типизацией ответов, SmartData и графовой топологией сети.

Tag Supported Python versions Licence


Содержание


Особенности

Область Что даёт
SmartData Автокастинг JSON → объекты, метаданные пути, fluent-фильтры
BaseModel Рекурсивный кастинг Union / Optional / List / вложенных моделей
WorkerNetClient Сессии, авто-GET/POST при длинном URL, ретраи
Логирование Стандартный logging, без собственного управления
Кэш полей LFU / LRU / FIFO, dirty-flag, предзагрузка из моделей
Топология CGraph + FNGraph, фильтры, линейные цепочки, save/load
Attenuation Расчёт оптических затуханий по CGraph (fiber / splitter / splice / adapter)
Координаты WGS84 ↔ Mercator (default) / local ENU / UTM
Графика SVG → PNG (Wand / Cairo / Inkscape / WeasyPrint)
Cleanup CLI cleanup-simpleworkernet — логи, кэш, конфиг

Структура пакета

src/simpleworkernet/
├── __init__.py              # публичный API, предзагрузка кэша моделей
├── __main__.py
├── __version__.py
├── cli.py                   # точка входа cleanup-simpleworkernet
├── core/
│   ├── client.py            # WorkerNetClient (сессии, GET/POST, ретраи)
│   ├── config.py            # ConfigManager + пути XDG/AppData
│   ├── cache.py             # SmartDataCache (LFU/LRU/FIFO)
│   ├── logger.py            # фасад над stdlib logging
│   ├── constants.py         # DEBUG/INFO/WARNING/ERROR/CRITICAL
│   ├── exceptions.py
│   └── typing.py
├── models/
│   ├── base.py              # BaseModel, BaseCategory, smart_model, CollapsedField
│   ├── primitives.py        # GeoPoint, vStr, vMoney, vINN, …
│   ├── operators.py         # Operator, Where
│   └── categories/          # API-категории WorkerNet
│       ├── customer.py, device.py, fiber.py, node.py, …
│       └── (≈30 модулей)
├── smartdata/
│   ├── core.py              # SmartData — fluent filters/aggregates
│   ├── helpers.py
│   ├── metadata.py          # PathSegment, MetaData
│   └── processor.py         # кастинг JSON → модели
├── utils/
│   ├── app_name.py          # get_app_name (хеш процесса)
│   ├── decorators.py        # api_method, timer, retry, …
│   ├── graphics.py          # SVG/PNG
│   └── topology/
│       ├── topology.py      # фасад Topology
│       ├── cache.py         # DataCache (инстанс, не синглтон)
│       ├── constants.py     # TYPE_OLT, TYPE_FIBER, …
│       ├── keys.py          # ObjKey, Interface
│       ├── models.py        # CGraphVertex/Edge, FNGraphVertex/Edge
│       ├── context.py       # BuildContext
│       ├── linear.py        # LinearPathFinder
│       ├── merge.py         # merge_cgraphs / merge_fngraphs
│       ├── graphs/
│       │   ├── cgraph.py    # CGraph (интерфейсы + коммутации)
│       │   └── fngraph.py   # FNGraph (узлы + fiber_id)
│       ├── builders/
│       │   ├── base.py      # GraphBuilder
│       │   └── handlers.py  # Fiber/Splitter/Cross/… handlers
│       └── attenuation/
│           ├── calculator.py
│           ├── catalog.py
│           ├── models.py    # AttenuationSegment, PathReport
│           ├── length.py    # resolve_fiber_length_m
│           ├── template.py  # generate_template из live API
│           └── defaults.json
└── scripts/
    └── uninstall.py         # cleanup логики (OS-aware)

Публичный импорт

from simpleworkernet import (
    WorkerNetClient,
    SmartData, Where, Operator,
    BaseModel, smart_model, CollapsedField,
    vStr, GeoPoint, GeoPointArray, vMoney,
    config_manager, log, cache,
    Topology, CGraph, FNGraph,
    cleanup,
    save_svg, load_svg, svg_to_png,
)
from simpleworkernet.utils.topology import (
    Attenuation, AttenuationCatalog, PathReport, DataCache,
)

Установка

Базовый пакет:

pip install simpleworkernet

Или из GitHub:

pip install git+https://github.com/busy4beaver/simpleworkernet.git

Опциональные зависимости:

pip install python-igraph
pip install pyproj
pip install Wand
Пакет Зачем
python-igraph графовая топология (CGraph / FNGraph)
pyproj проекция UTM
Wand (ImageMagick) SVG → PNG (один из бэкендов)

Быстрый старт

from simpleworkernet import WorkerNetClient, Where, Operator

with WorkerNetClient("my.workernet.ru", "your-api-key") as client:
    customers = client.Module.get_user_list()
    active = customers.where("state_id", 2)
    print(active.count())

    # fluent-фильтры
    rich = customers.where("balance", Operator.GE, 1000)
    moscow = customers.where("city", "Москва")

Контекстный менеджер открывает/закрывает сессию автоматически. Без with — вызывайте client.session() / client.closeSession().


Конфигурация

config_manager — синглтон процесса. Изменения применяются сразу; persistence — только через save().

Значения по умолчанию

Параметр Default Описание
cache.enabled True SmartDataCache включён
cache.max_size 200000 макс. записей
cache.evict_strategy "lru" lru / lfu / fifo
cache.evict_threshold 0.95 порог заполнения для eviction
cache.evict_percent 0.25 доля удаляемых записей
cache.auto_save True автосохранение при выходе
default_timeout 30 таймаут HTTP (сек)
max_retries 3 число повторов
user_agent "SimpleWorkerNet/1.0" User-Agent
smartdata_max_depth 100 макс. глубина кастинга

Примеры

from simpleworkernet import config_manager

config_manager.cache_enabled = True
config_manager.cache_max_size = 200000
config_manager.cache_evict_strategy = "lfu"

config_manager.default_timeout = 60
config_manager.save()          # записать в config.json

config_manager.show_config()  # печать текущих значений
config_manager.reset(save=True)  # сброс на defaults

Массовое обновление:

config_manager.update(
    cache={"max_size": 100000, "evict_strategy": "fifo"},
    save=True,
)

Основные компоненты

WorkerNetClient

from simpleworkernet import WorkerNetClient

client = WorkerNetClient(
    host="my.workernet.ru",
    apikey="key",
    protocol="https",   # default
    port=443,           # default
)
client.session()

# категории API — атрибуты клиента
users = client.Module.get_user_list()
nodes = client.Node.get()
fibers = client.Fiber.get_list(cable_line_type_id=1)

client.closeSession()

SmartData

Ответы API автоматически оборачиваются в SmartData:

users = client.Module.get_user_list()

users.count()
users.to_list()                    # list[model]
users.where("state_id", 2)
users.where("balance", Operator.GE, 500)
users.select("id", "name")
users.first()
users.map(lambda u: u.name)

BaseModel / smart_model

from simpleworkernet import BaseModel, smart_model, vStr

@smart_model
class DeviceInfo(BaseModel):
    id: int
    name: vStr
    parent_id: int | None = None

Рекурсивный кастинг Union / Optional / List / вложенных моделей из сырого JSON.


Логирование

Библиотека не управляет handlers и уровнями. Сообщения идут через стандартный logging (логгер workernet.<app>), клиент настраивает вывод сам:

import logging
from simpleworkernet import log

logging.basicConfig(level=logging.DEBUG)

log.info("старт")
log.debug("детали")
log.warning("внимание")
log.error("ошибка")

# или точечно:
# logging.getLogger("workernet").setLevel(logging.INFO)

Кэширование

cache — синглтон SmartDataCache (метаданные полей моделей).

from simpleworkernet import cache, config_manager

config_manager.cache_enabled = True
cache.stats()       # размер, hits/misses
cache.clear()
cache.save()
cache.load()

При старте пакета (если кэш включён) выполняется предзагрузка схем из всех category-моделей.

Для топологии используется отдельный DataCache (не синглтон) — см. ниже.


Каталоги данных

Пути совпадают с core.config и scripts/uninstall. <app> — имя приложения с хешем (get_app_name(with_hash=True)).

ОС Config Cache Logs
Linux ~/.config/simpleworkernet/<app>/ ~/.cache/simpleworkernet/<app>/ ~/.local/share/simpleworkernet/<app>/logs/ (legacy)
Windows %APPDATA%\\simpleworkernet\\<app>\\ %LOCALAPPDATA%\\simpleworkernet\\<app>\\ %APPDATA%\\simpleworkernet\\<app>\\logs\\ (legacy)
macOS ~/Library/Application Support/simpleworkernet/<app>/ ~/Library/Caches/simpleworkernet/<app>/ ~/Library/Logs/simpleworkernet/<app>/ (legacy)

Файл конфигурации: config.json в Config-директории.

Директория Logs больше не используется для записи; cleanup-simpleworkernet --logs-only удаляет только старые (legacy) файлы.


Очистка данных

CLI:

cleanup-simpleworkernet
cleanup-simpleworkernet --force
cleanup-simpleworkernet --dry-run
cleanup-simpleworkernet --list
cleanup-simpleworkernet --logs-only
cleanup-simpleworkernet --cache-only
cleanup-simpleworkernet --config-only

Из Python:

from simpleworkernet import cleanup

cleanup(force=True, mode="all")    # logs | cache | config | all
cleanup(force=True, mode="cache")

Графика

from simpleworkernet import save_svg, load_svg, svg_to_png

save_svg(svg_string, "scheme.svg")
data = load_svg("scheme.svg")
svg_to_png("scheme.svg", "scheme.png", width=1200)

Бэкенды (первый доступный): Wand → Cairo → Inkscape → WeasyPrint.


Координаты

GeoPoint / GeoPointArray — WGS84 и проекции в плоские метры.

По умолчанию: projection="mercator" (Web Mercator).

Проекции

projection Описание Зависимости
"mercator" (default) Web Mercator; relative → метры через × cos(lat) нет
"local" East/North относительно center; Y = истинный север нет
"utm" UTM-зона по долготе; опционально correct_grid_north pyproj

Масштаб Mercator

Web Mercator завышает наземные расстояния в 1/cos(φ).

При relative-режиме (center задан, absolute=False) и auto_scale_mercator=True (default):

  1. считаются «сырые» mercator-координаты точки и центра;
  2. берётся разность (delta);
  3. delta умножается на cos(center.lat).

Так центр остаётся точно (0, 0), без фиктивного сдвига, а расстояния на субкилометровых базисах совпадают с projection="local" (сфера R=6378137) с точностью ≪ 0.1 %.

Для подложки тайлов OSM/Google используйте absolute=True и auto_scale_mercator=False (сырые EPSG:3857-метры).

from simpleworkernet import GeoPoint, GeoPointArray

origin = GeoPoint(55.75, 37.62)
p = GeoPoint(55.76, 37.63)

# default: mercator relative + metric scale
x, y = p.to_xy(center=origin)

# local ENU (строго East/North)
x, y = p.to_xy(center=origin, projection="local")

# сырой mercator (для тайлов), без ×cos
x, y = p.to_xy(center=origin, auto_scale_mercator=False)

# абсолютный mercator (EPSG:3857-like)
x, y = p.to_xy(absolute=True, auto_scale_mercator=False)

# UTM
x, y = p.to_xy(center=origin, projection="utm")  # нужен pyproj

back = GeoPoint.from_xy(x, y, center=origin)

arr = GeoPointArray([(55.0, 37.0), (57.0, 39.0)])
xy_list = arr.to_xy()          # center = centroid, mercator
sw, ne = arr.bounds()
print(p.distance_to(origin))   # км (haversine)

Доп. параметры: scale, offset, rotation_deg, absolute, correct_grid_north (UTM).


Графовая топология

Зависимость: pip install python-igraph

from simpleworkernet import WorkerNetClient, Topology
from simpleworkernet.utils.topology import DataCache

client = WorkerNetClient("my.workernet.ru", "key")
cache = DataCache()          # можно шарить между Topology
topo = Topology(client, cache=cache)

topo.build_from_cross("98d9d368-…", port=7)
topo.build_from_device("olt", 12345, port=1)
topo.build_from_customer(customer_id)
topo.build_from_node(node_id)
topo.build_from_fiber(fiber_id)
topo.build_from_splitter(splitter_id)
topo.build_from_cwdm(cwdm_id)
topo.build_from_cable(cable_id)

customers = topo.get_customers()
linear = topo.topology_from_commutation("customer", customers[0])

topo.save_to_file("topo.json")
topo2 = Topology.load_from_file("topo.json", client=client, cache=cache)
Граф Вершины Рёбра
CGraph интерфейсы (obj_type + obj_id + side + port) коммутации
FNGraph node_id fiber_id

Фильтры при build: included_fibers, excluded_fibers, excluded_nodes.


Оптические затухания (Attenuation)

Расчёт по запросу (не при build) по уже построенному CGraph.

from simpleworkernet.utils.topology import (
    Topology, Attenuation, AttenuationCatalog,
)
from simpleworkernet.utils.topology.attenuation.template import generate_template

# 1) топология
topo = Topology(client)
topo.build_from_device("olt", 12345, port=1)
linear = topo.topology_from_commutation("customer", customer_id)

# 2) каталог профилей (defaults + overrides)
cat = AttenuationCatalog.with_defaults()
# cat = AttenuationCatalog.from_json("my_profiles.json")
# cat.force_fiber(23682, 0.20)
# cat.force_splitter_port(35196, port=3, db=10.5)

# 3) расчёт
att = Attenuation(
    linear.cgraphs[0],
    catalog=cat,
    wavelength=1550,
    cache=topo._cache,
    client=client,
)
report = att.olt_to_customer(customer_id)
# или: att.along_linear()
print(report.to_table())
print(report.total_db, report.by_kind())

Сегменты пути

kind Источник
fiber α(дБ/км) × L(км); L: opticalen2 → opticalen → geo×k → geo_api
splitter порт OUT (side=2): force → instance → catalog → ratio → estimate
adapter internal-ребро кросса
splice / connector внешние стыки
force override на connect_id / fiber / splitter port

Defaults (defaults.json)

Параметр 1310 nm 1490 nm 1550 nm
fiber дБ/км 0.35 0.28 0.22
splice 0.05 дБ
connector 0.30 дБ
adapter 0.20 дБ
geo_slack_k 1.03
splitter_excess 0.5 дБ

Типовые ratio-профили сплиттеров: 1x2_50/50, 1x2_5/95, …, 1x4_equal, 1x8_equal.

Каталог и шаблон

# шаблон из live API (кабели + сплиттеры)
cat = generate_template(client, cache=topo._cache, path="attenuation.json")
# пользователь дозаполняет ports / db_per_km, затем:
cat = AttenuationCatalog.from_json("attenuation.json")

Удобные запросы

Метод Назначение
path(src, dst) shortest path + сумма сегментов
along_linear() обход линейного CGraph
olt_to_customer(id) downstream OLT→абонент
customer_to_olt(id) upstream
olt_to_splitter_out(id, port) до выхода сплиттера
budget_summary() / worst_customer() по всем абонентам графа

PathReport: total_db, segments, to_table(), to_dict(), by_kind(), warnings, missing.


Тесты

Offline (unit)

Не требуют API. Покрывают примитивы, координаты, SmartData, operators, topology (CGraph/FNGraph/handlers/linear/merge/attenuation/cache) на синтетических данных.

pytest tests/ -m "not integration" -v

Только координаты:

pytest tests/test_coordinates.py -v

Только топология:

pytest tests/topology/ -v

Integration (live API)

Нужны доступный WorkerNet и ключ.

pytest tests/ -v --wn-host=my.workernet.ru --wn-apikey=YOUR_KEY

Или через окружение:

export WORKERNET_HOST=my.workernet.ru
export WORKERNET_APIKEY=YOUR_KEY
export WORKERNET_PROTOCOL=https
export WORKERNET_PORT=443
export WORKERNET_TEST_NODE_ID=123
export WORKERNET_TEST_CUSTOMER_ID=456
pytest tests/ -v

CLI-опции pytest (см. tests/conftest.py):

Опция Env Описание
--wn-host WORKERNET_HOST хост API
--wn-apikey WORKERNET_APIKEY API-ключ
--wn-protocol WORKERNET_PROTOCOL http / https (default https)
--wn-port WORKERNET_PORT порт (default 443)
--nodeid WORKERNET_TEST_NODE_ID ID узла для live topology
--customerid WORKERNET_TEST_CUSTOMER_ID ID абонента

Без host/apikey integration-тесты автоматически skip.

Маркер: @pytest.mark.integration.


☕ Поддержать проект

YooMoney Boosty

Metadata

Release files for simpleworkernet 1.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for simpleworkernet 1.0.8
File Size Uploaded Provenance Actions
simpleworkernet-1.0.8.tar.gz 157.2 kB With provenance Details for simpleworkernet-1.0.8.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for simpleworkernet 1.0.8
File Interpreter ABI Platform Actions
simpleworkernet-1.0.8-py3-none-any.whl Python 3 none any Details for simpleworkernet-1.0.8-py3-none-any.whl

Total release size: 347.7 kB

Release files / simpleworkernet-1.0.8.tar.gz

Download URL simpleworkernet-1.0.8.tar.gz
Size 157.2 kB
Tags Source
SHA-256 checksum
How to use checksums
0e007927d63b23c62bfef89aa92c179b71e62d258ddf6dfd8abb341160103e60
BLAKE2b-256 checksum
How to use checksums
0cc2da8ec07e7ef498d11329b944f2c2778433deb0bd23cc22f2a8ad43caf418
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 4, 2026.

Transparency log

Release files / simpleworkernet-1.0.8-py3-none-any.whl

Download URL simpleworkernet-1.0.8-py3-none-any.whl
Size 190.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
461340f21f336ff4a15cd31b366131aa53031a29716f0d8cb2462a9866093c06
BLAKE2b-256 checksum
How to use checksums
7b351dc2392dd10c76211e23217e3935a137c6f6978ef3c0bb468ee16beae484
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 4, 2026.

Transparency log

Security for simpleworkernet 1.0.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for simpleworkernet-1.0.8.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 4, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page