rebdhuhn
🇩🇪 Dieses Repository enthält ein Python-Paket namens rebdhuhn, das genutzt werden kann, um aus .docx-Dateien extrahierte maschinenlesbare Tabellen, die einen Entscheidungsbaum (EBD) modellieren, in echte Graphen zu konvertieren. Diese Entscheidungsbäume sind Teil eines regulatorischen Regelwerks für die deutsche Energiewirtschaft und kommen in der Eingangsprüfung der Marktkommunikation zum Einsatz.
🇬🇧 This repository contains the source code of the Python package rebdhuhn.
Rationale
Assume, that you scraped the Entscheidungsbaumdiagramm tables by EDI@Energy from their somewhat "digitized" PDF/DOCX files.
(To do so, you can use the package ebdamame.)
Also assume, that the result of your scraping is a rebdhuhn.models.EbdTable.
The package rebdhuhn contains logic to convert your scraped data into a graph.
This graph can then be exported e.g. as SVG and/or UML.
ebdamame and rebdhuhn combined are the core of our ebd_toolchain which scrapes EBD.docx files from the edi_energy_mirror and pushes them to machine_readable-entscheidungsbaumdiagramme.
How to use rebdhuhn?
Install the package from pypi:
pip install rebdhuhn
Create an Instance of EbdTable
EbdTable contains the raw data by BDEW in a machine-readable format.
Creating instances of EbdTable is out of scope for this package.
Ask Hochfrequenz for support on this topic.
In the following example we hard code the information.
from rebdhuhn.graph_conversion import convert_table_to_graph
from rebdhuhn.models import EbdCheckResult, EbdTable, EbdTableMetaData, EbdTableRow, EbdTableSubRow, EbdGraph
ebd_table: EbdTable # this is the result of scraping the docx file
ebd_table = EbdTable( # this data shouldn't be handwritten
metadata=EbdTableMetaData(
ebd_code="E_0003",
chapter="MaBiS",
section="7.39 AD: Bestellung der Aggregationsebene der Bilanzkreissummenzeitreihe auf Ebene der Regelzone",
ebd_name="Bestellung der Aggregationsebene RZ prüfen",
role="ÜNB",
),
rows=[
EbdTableRow(
step_number="1",
description="Erfolgt der Eingang der Bestellung fristgerecht?",
sub_rows=[
EbdTableSubRow(
check_result=EbdCheckResult(result=False, subsequent_step_number=None),
result_code="A01",
note="Fristüberschreitung",
),
EbdTableSubRow(
check_result=EbdCheckResult(result=True, subsequent_step_number="2"),
result_code=None,
note=None,
),
],
),
EbdTableRow(
step_number="2",
description="Erfolgt die Bestellung zum Monatsersten 00:00 Uhr?",
sub_rows=[
EbdTableSubRow(
check_result=EbdCheckResult(result=False, subsequent_step_number=None),
result_code="A02",
note="Gewählter Zeitpunkt nicht zulässig",
),
EbdTableSubRow(
check_result=EbdCheckResult(result=True, subsequent_step_number="Ende"),
result_code=None,
note=None,
),
],
),
],
)
assert isinstance(ebd_table, EbdTable)
ebd_graph = convert_table_to_graph(ebd_table)
assert isinstance(ebd_graph, EbdGraph)
Export as PlantUML
from rebdhuhn import convert_graph_to_plantuml
plantuml_code = convert_graph_to_plantuml(ebd_graph)
with open("e_0003.puml", "w+", encoding="utf-8") as uml_file:
uml_file.write(plantuml_code)
The file e_0003.puml now looks like this:
@startuml
...
if (<b>1: </b> Erfolgt der Eingang der Bestellung fristgerecht?) then (ja)
else (nein)
:A01;
note left
Fristüberschreitung
endnote
kill;
endif
if (<b>2: </b> Erfolgt die Bestellung zum Monatsersten 00:00 Uhr?) then (ja)
end
else (nein)
:A02;
note left
Gewählter Zeitpunkt nicht zulässig
endnote
kill;
endif
@enduml
Export the graph as SVG
To export the graph as SVG, you need a Kroki instance. You can either:
- Use the public instance at
https://kroki.io - Run a local instance via Docker:
docker run -p 8125:8000 yuzutech/kroki:0.36.0
Then use
from rebdhuhn import convert_plantuml_to_svg_kroki
from rebdhuhn.kroki import Kroki
kroki_client = Kroki()
svg_code = convert_plantuml_to_svg_kroki(plantuml_code, kroki_client)
with open("e_0003.svg", "w+", encoding="utf-8") as svg_file:
svg_file.write(svg_code)
Explain the Outcomes (Which Checks Lead to a Result Code?)
explain_outcomes answers "what does this result code imply?" for every outcome of an EBD and "which result codes are
still reachable?" for every answer to every decision step:
from rebdhuhn import convert_table_to_graph, explain_outcomes
graph = convert_table_to_graph(ebd_table) # ebd_table is an instance of EbdTable (see above)
outcomes = explain_outcomes(graph, max_routes=20)
a05 = next(outcome for outcome in outcomes.outcomes if outcome.key == "A05")
questions = {step.step_number: step.question for step in outcomes.steps} # the legend: every step exactly once
print([(step.step_number, questions[step.step_number], step.answers) for step in a05.certain_steps]) # on every route
print(a05.route_count, a05.routes) # routes are None if there are more than max_routes of them
print(outcomes.model_dump_json(indent=2)) # self-contained and deterministic
The question texts are only in steps; certain_steps, possible_steps, routes and decisions refer to a step
by its step_number. certain_steps lie on every route to the outcome, possible_steps only on some. The number of
routes is counted without enumerating them (E_0594 has hundreds of thousands), so it is cheap even for the biggest
EBDs.
What if ... (assume)
assume answers decision steps in advance and analyses the EBD under that assumption:
# "If I always answer 'nein' at step 17, does step 39 still matter?"
assumed = explain_outcomes(graph, assume={"17": False})
print("39" in assumed.unreachable_steps)
a05 = next(outcome for outcome in assumed.outcomes if outcome.key == "A05")
print(a05.route_count == 0) # if "node 39" is a result code: can it still be produced?
print([outcome.key for outcome in assumed.outcomes if outcome.route_count != 0]) # codes still reachable
- An assumption holds on every pass of a loop.
- Answers excluded by an assumption are missing from
decisions, while the entries of unreachable steps stay there (checkunreachable_steps). - Unknown steps, transition steps and other node keys raise a
ValueError.
Recipe "which outcomes does a change at step X affect?": the outcomes whose certain_steps or possible_steps contain
X (routes is capped by max_routes and None for loops, so use the step lists).
Some notes on the details:
- Some EBDs contain loops (iterations over a list of hits). There a step can be passed more than once:
answersis the answer given the last time the step is passed, andpossible_stepsalso contains steps that are only passed by going round a loop. route_countandroutesareNoneif routes to this outcome can loop.routesis alsoNoneif there are more thanmax_routesroutes (max_routes=0disables the route lists). An unreachable outcome hasroute_count == 0.- The outcome
Endemeans "the check ran to a bare Ende". It has no special rule: routes that passed a result code with subsequent steps (a transitional outcome) earlier count like any other route. Accordinglycan_reach_end_without_result_codesays that an end (or an outcome without result code) is reachable after an answer, although result codes with subsequent steps may have been produced on the way. - Result codes are grouped literally. If the EBD has a wildcard outcome
A**(has_wildcard_outcomeisTrue), a received code may also come from that outcome, so read its notes and steps too. multi_step_instructionsof the EBD (e.g. "Je Marktlokation erfolgen die nachstehenden Prüfungen") are passed through unchanged but not evaluated: routes and step implications are derived from the graph structure only.- Transition steps carry their
noteinsteps. - Limitation: a transitional outcome that lies on the way to a different outcome is not reported as a step.
Error Handling
rebdhuhn provides three base exception classes to help you distinguish between errors in different pipeline stages:
| Exception | Pipeline Stage | Description |
|---|---|---|
GraphConversionError |
table → graph | Errors during table-to-graph conversion. Affects both SVG and PlantUML. |
PlantumlConversionError |
graph → puml | Errors specific to PlantUML generation. |
SvgConversionError |
graph → dot → svg | Errors specific to SVG/DOT generation via Kroki. |
This allows you to handle PlantUML failures gracefully while still generating SVG output:
from rebdhuhn import (
convert_table_to_graph,
convert_graph_to_plantuml,
convert_graph_to_dot,
convert_dot_to_svg_kroki,
GraphConversionError,
PlantumlConversionError,
SvgConversionError,
)
from rebdhuhn.kroki import Kroki
# ebd_table is an instance of EbdTable (see above for how to create one)
kroki_client = Kroki() # requires a running Kroki instance
try:
graph = convert_table_to_graph(ebd_table)
except GraphConversionError:
# Table-to-graph conversion failed - neither SVG nor PlantUML will work
raise
# SVG generation (primary)
try:
dot_code = convert_graph_to_dot(graph)
svg = convert_dot_to_svg_kroki(dot_code, kroki_client)
except SvgConversionError:
print("SVG generation failed")
# PlantUML generation (secondary)
try:
puml_code = convert_graph_to_plantuml(graph)
except PlantumlConversionError:
print("PlantUML generation failed (non-critical)")
How to use this Repository on Your Machine (for development)
Please follow the instructions in our Python Template Repository . And for further information, see the uv documentation.
Running Tests
Tests use testcontainers to automatically start a Kroki instance when needed. Make sure Docker is installed and running. Tests that require Kroki will be skipped if Docker is not available.
Contribute
You are very welcome to contribute to this template repository by opening a pull request against the main branch.
Related Tools and Context
This repository is part of the Hochfrequenz Libraries and Tools for a truly digitized market communication.
Metadata
Release files for rebdhuhn 1.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| rebdhuhn-1.4.0.tar.gz | 182.4 kB | Without provenance | Details for rebdhuhn-1.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| rebdhuhn-1.4.0-py3-none-any.whl | Python 3 | none | any | Details for rebdhuhn-1.4.0-py3-none-any.whl |
Total release size: 252.6 kB
Release files / rebdhuhn-1.4.0.tar.gz
| Download URL | rebdhuhn-1.4.0.tar.gz |
|---|---|
| Size | 182.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
86970183d85eb2130e43279301b00c578da8382624a04b60dd1549166e4a023c
|
|
BLAKE2b-256 checksum How to use checksums |
b1fc0bd8dac461e5a42ae87a7caa4ca9d62e7fab1c33c9b8a292454dc2887658
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / rebdhuhn-1.4.0-py3-none-any.whl
| Download URL | rebdhuhn-1.4.0-py3-none-any.whl |
|---|---|
| Size | 70.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
03aa7273d385b9923c82c8fd2bd099447537c1abebfe835fcb0fb3bf63d6d040
|
|
BLAKE2b-256 checksum How to use checksums |
4b8b12934f55d9e144d0e3a13e0522bd28eb04ac4c5896d9b2dcad67ae26e25f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for rebdhuhn 1.4.0
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.