Skip to content

Commit 3367421

Browse files
authored
Fix docstrings style (#75)
1 parent 90f3781 commit 3367421

33 files changed

Lines changed: 234 additions & 491 deletions

ngraph/blueprints.py

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
@dataclass
1313
class Blueprint:
14-
"""
15-
Represents a reusable blueprint for hierarchical sub-topologies.
14+
"""Represents a reusable blueprint for hierarchical sub-topologies.
1615
1716
A blueprint may contain multiple groups of nodes (each can have a node_count
1817
and a name_template), plus adjacency rules describing how those groups connect.
@@ -35,8 +34,7 @@ class Blueprint:
3534

3635
@dataclass
3736
class DSLExpansionContext:
38-
"""
39-
Carries the blueprint definitions and the final Network instance
37+
"""Carries the blueprint definitions and the final Network instance
4038
to be populated during DSL expansion.
4139
4240
Attributes:
@@ -49,8 +47,7 @@ class DSLExpansionContext:
4947

5048

5149
def expand_network_dsl(data: Dict[str, Any]) -> Network:
52-
"""
53-
Expands a combined blueprint + network DSL into a complete Network object.
50+
"""Expands a combined blueprint + network DSL into a complete Network object.
5451
5552
Overall flow:
5653
1) Parse "blueprints" into Blueprint objects.
@@ -164,8 +161,7 @@ def _expand_group(
164161
group_def: Dict[str, Any],
165162
inherited_risk_groups: Set[str] | None = None,
166163
) -> None:
167-
"""
168-
Expands a single group definition into either:
164+
"""Expands a single group definition into either:
169165
- Another blueprint's subgroups, or
170166
- A direct node group (with node_count, etc.),
171167
- Possibly replicating itself if group_name has bracket expansions.
@@ -319,8 +315,7 @@ def _expand_blueprint_adjacency(
319315
adj_def: Dict[str, Any],
320316
parent_path: str,
321317
) -> None:
322-
"""
323-
Expands adjacency definitions from within a blueprint, using parent_path
318+
"""Expands adjacency definitions from within a blueprint, using parent_path
324319
as the local root. This also handles optional expand_vars for repeated adjacency.
325320
326321
Recognized adjacency keys:
@@ -352,8 +347,7 @@ def _expand_blueprint_adjacency(
352347

353348

354349
def _expand_adjacency(ctx: DSLExpansionContext, adj_def: Dict[str, Any]) -> None:
355-
"""
356-
Expands a top-level adjacency definition from 'network.adjacency'. If 'expand_vars'
350+
"""Expands a top-level adjacency definition from 'network.adjacency'. If 'expand_vars'
357351
is provided, we expand the source/target as templates repeatedly.
358352
359353
Recognized adjacency keys:
@@ -388,8 +382,7 @@ def _expand_adjacency(ctx: DSLExpansionContext, adj_def: Dict[str, Any]) -> None
388382
def _expand_adjacency_with_variables(
389383
ctx: DSLExpansionContext, adj_def: Dict[str, Any], parent_path: str
390384
) -> None:
391-
"""
392-
Handles adjacency expansions when 'expand_vars' is provided.
385+
"""Handles adjacency expansions when 'expand_vars' is provided.
393386
We substitute variables into the 'source' and 'target' templates to produce
394387
multiple adjacency expansions. Then each expansion is passed to _expand_adjacency_pattern.
395388
@@ -451,8 +444,7 @@ def _expand_adjacency_pattern(
451444
link_params: Dict[str, Any],
452445
link_count: int = 1,
453446
) -> None:
454-
"""
455-
Generates Link objects for the chosen adjacency pattern among matched nodes.
447+
"""Generates Link objects for the chosen adjacency pattern among matched nodes.
456448
457449
Supported Patterns:
458450
* "mesh": Connect every source node to every target node
@@ -531,8 +523,7 @@ def _create_link(
531523
link_params: Dict[str, Any],
532524
link_count: int = 1,
533525
) -> None:
534-
"""
535-
Creates and adds one or more Links to the network, applying capacity, cost,
526+
"""Creates and adds one or more Links to the network, applying capacity, cost,
536527
disabled, risk_groups, and attrs from link_params if present.
537528
538529
Args:
@@ -566,8 +557,7 @@ def _create_link(
566557

567558

568559
def _process_direct_nodes(net: Network, network_data: Dict[str, Any]) -> None:
569-
"""
570-
Processes direct node definitions (network_data["nodes"]) and adds them to the network
560+
"""Processes direct node definitions (network_data["nodes"]) and adds them to the network
571561
if they do not already exist. If the node name already exists, we do nothing.
572562
573563
Allowed top-level keys for each node: {"disabled", "attrs", "risk_groups"}.
@@ -609,8 +599,7 @@ def _process_direct_nodes(net: Network, network_data: Dict[str, Any]) -> None:
609599

610600

611601
def _process_direct_links(net: Network, network_data: Dict[str, Any]) -> None:
612-
"""
613-
Processes direct link definitions (network_data["links"]) and adds them to the network.
602+
"""Processes direct link definitions (network_data["links"]) and adds them to the network.
614603
615604
Each link dict must contain {"source", "target"} plus optionally
616605
{"link_params", "link_count"}. No other top-level keys allowed.
@@ -653,8 +642,7 @@ def _process_direct_links(net: Network, network_data: Dict[str, Any]) -> None:
653642

654643

655644
def _process_link_overrides(net: Network, network_data: Dict[str, Any]) -> None:
656-
"""
657-
Processes the 'link_overrides' section of the network DSL, updating
645+
"""Processes the 'link_overrides' section of the network DSL, updating
658646
existing links with new parameters. Overrides are applied in order if
659647
multiple items match the same link.
660648
@@ -691,8 +679,7 @@ def _process_link_overrides(net: Network, network_data: Dict[str, Any]) -> None:
691679

692680

693681
def _process_node_overrides(net: Network, network_data: Dict[str, Any]) -> None:
694-
"""
695-
Processes the 'node_overrides' section of the network DSL, updating
682+
"""Processes the 'node_overrides' section of the network DSL, updating
696683
existing nodes with new attributes in bulk. Overrides are applied in order
697684
if multiple items match the same node.
698685
@@ -740,8 +727,7 @@ def _update_links(
740727
link_params: Dict[str, Any],
741728
any_direction: bool = True,
742729
) -> None:
743-
"""
744-
Updates all Link objects between nodes matching 'source' and 'target' paths
730+
"""Updates all Link objects between nodes matching 'source' and 'target' paths
745731
with new parameters (capacity, cost, disabled, risk_groups, attrs).
746732
747733
If any_direction=True, both (source->target) and (target->source) links
@@ -802,8 +788,7 @@ def _update_nodes(
802788
disabled_val: Any = None,
803789
risk_groups_val: Any = None,
804790
) -> None:
805-
"""
806-
Updates attributes on all nodes matching a given path pattern.
791+
"""Updates attributes on all nodes matching a given path pattern.
807792
808793
- If 'disabled_val' is not None, sets node.disabled to that boolean value.
809794
- If 'risk_groups_val' is not None, *replaces* the node's risk_groups with that new set.
@@ -833,8 +818,7 @@ def _update_nodes(
833818
def _apply_parameters(
834819
subgroup_name: str, subgroup_def: Dict[str, Any], params_overrides: Dict[str, Any]
835820
) -> Dict[str, Any]:
836-
"""
837-
Applies user-provided parameter overrides to a blueprint subgroup.
821+
"""Applies user-provided parameter overrides to a blueprint subgroup.
838822
839823
Example:
840824
If 'spine.node_count' = 6 is in params_overrides,
@@ -864,8 +848,7 @@ def _apply_parameters(
864848
def _apply_nested_path(
865849
node_def: Dict[str, Any], path_parts: List[str], value: Any
866850
) -> None:
867-
"""
868-
Recursively applies a path like ["attrs", "role"] to set node_def["attrs"]["role"] = value.
851+
"""Recursively applies a path like ["attrs", "role"] to set node_def["attrs"]["role"] = value.
869852
Creates intermediate dicts as needed.
870853
871854
Args:
@@ -888,8 +871,7 @@ def _apply_nested_path(
888871

889872

890873
def _expand_name_patterns(name: str) -> List[str]:
891-
"""
892-
Parses and expands bracketed expressions in a group name. For example:
874+
"""Parses and expands bracketed expressions in a group name. For example:
893875
894876
"fa[1-3]" -> ["fa1", "fa2", "fa3"]
895877
"dc[1,3,5-6]" -> ["dc1", "dc3", "dc5", "dc6"]
@@ -930,8 +912,7 @@ def _expand_name_patterns(name: str) -> List[str]:
930912

931913

932914
def _parse_range_expr(expr: str) -> List[str]:
933-
"""
934-
Parses a bracket expression that might have commas, single values, and dash ranges.
915+
"""Parses a bracket expression that might have commas, single values, and dash ranges.
935916
For example: "1-3,5,7-9" -> ["1", "2", "3", "5", "7", "8", "9"].
936917
937918
Args:
@@ -955,8 +936,7 @@ def _parse_range_expr(expr: str) -> List[str]:
955936

956937

957938
def _join_paths(parent_path: str, rel_path: str) -> str:
958-
"""
959-
Joins two path segments according to NetGraph's DSL conventions:
939+
"""Joins two path segments according to NetGraph's DSL conventions:
960940
961941
- If rel_path starts with '/', we strip the leading slash and treat it as
962942
appended to parent_path if parent_path is not empty.
@@ -983,8 +963,7 @@ def _join_paths(parent_path: str, rel_path: str) -> str:
983963
def _check_no_extra_keys(
984964
data_dict: Dict[str, Any], allowed: set[str], context: str
985965
) -> None:
986-
"""
987-
Checks that data_dict only has keys in 'allowed'. Raises ValueError if not.
966+
"""Checks that data_dict only has keys in 'allowed'. Raises ValueError if not.
988967
989968
Args:
990969
data_dict (Dict[str, Any]): The dict to check.
@@ -1000,8 +979,7 @@ def _check_no_extra_keys(
1000979

1001980

1002981
def _check_adjacency_keys(adj_def: Dict[str, Any], context: str) -> None:
1003-
"""
1004-
Ensures adjacency definitions only contain recognized keys.
982+
"""Ensures adjacency definitions only contain recognized keys.
1005983
1006984
Recognized adjacency keys are:
1007985
{"source", "target", "pattern", "link_count", "link_params",
@@ -1025,9 +1003,8 @@ def _check_adjacency_keys(adj_def: Dict[str, Any], context: str) -> None:
10251003

10261004

10271005
def _check_link_params(link_params: Dict[str, Any], context: str) -> None:
1028-
"""
1029-
Checks that link_params only has recognized keys:
1030-
{"capacity", "cost", "disabled", "risk_groups", "attrs"}.
1006+
"""Checks that link_params only has recognized keys:
1007+
{"capacity", "cost", "disabled", "risk_groups", "attrs"}.
10311008
"""
10321009
recognized = {"capacity", "cost", "disabled", "risk_groups", "attrs"}
10331010
extra = set(link_params.keys()) - recognized

ngraph/cli.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
def _run_scenario(path: Path, output: Optional[Path]) -> None:
1212
"""Run a scenario file and store results as JSON."""
13+
1314
yaml_text = path.read_text()
1415
scenario = Scenario.from_yaml(yaml_text)
1516
scenario.run()
@@ -23,7 +24,12 @@ def _run_scenario(path: Path, output: Optional[Path]) -> None:
2324

2425

2526
def main(argv: Optional[List[str]] = None) -> None:
26-
"""Entry point for the ``ngraph`` command."""
27+
"""Entry point for the ``ngraph`` command.
28+
29+
Args:
30+
argv: Optional list of command-line arguments. If ``None``, ``sys.argv``
31+
is used.
32+
"""
2733
parser = argparse.ArgumentParser(prog="ngraph")
2834
subparsers = parser.add_subparsers(dest="command", required=True)
2935

ngraph/components.py

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
@dataclass
1111
class Component:
12-
"""
13-
A generic component that can represent chassis, line cards, optics, etc.
12+
"""A generic component that can represent chassis, line cards, optics, etc.
1413
Components can have nested children, each with their own cost, power, etc.
1514
1615
Attributes:
@@ -44,8 +43,7 @@ class Component:
4443
children: Dict[str, Component] = field(default_factory=dict)
4544

4645
def total_cost(self) -> float:
47-
"""
48-
Computes the total (recursive) cost of this component, including children,
46+
"""Computes the total (recursive) cost of this component, including children,
4947
multiplied by this component's count.
5048
5149
Returns:
@@ -57,8 +55,7 @@ def total_cost(self) -> float:
5755
return single_instance_cost * self.count
5856

5957
def total_power(self) -> float:
60-
"""
61-
Computes the total *typical* (recursive) power usage of this component,
58+
"""Computes the total *typical* (recursive) power usage of this component,
6259
including children, multiplied by this component's count.
6360
6461
Returns:
@@ -70,8 +67,7 @@ def total_power(self) -> float:
7067
return single_instance_power * self.count
7168

7269
def total_power_max(self) -> float:
73-
"""
74-
Computes the total *peak* (recursive) power usage of this component,
70+
"""Computes the total *peak* (recursive) power usage of this component,
7571
including children, multiplied by this component's count.
7672
7773
Returns:
@@ -83,8 +79,7 @@ def total_power_max(self) -> float:
8379
return single_instance_power_max * self.count
8480

8581
def total_capacity(self) -> float:
86-
"""
87-
Computes the total (recursive) capacity of this component,
82+
"""Computes the total (recursive) capacity of this component,
8883
including children, multiplied by this component's count.
8984
9085
Returns:
@@ -96,8 +91,7 @@ def total_capacity(self) -> float:
9691
return single_instance_capacity * self.count
9792

9893
def as_dict(self, include_children: bool = True) -> Dict[str, Any]:
99-
"""
100-
Returns a dictionary containing all properties of this component.
94+
"""Returns a dictionary containing all properties of this component.
10195
10296
Args:
10397
include_children (bool): If True, recursively includes children.
@@ -127,8 +121,7 @@ def as_dict(self, include_children: bool = True) -> Dict[str, Any]:
127121

128122
@dataclass
129123
class ComponentsLibrary:
130-
"""
131-
Holds a collection of named Components. Each entry is a top-level "template"
124+
"""Holds a collection of named Components. Each entry is a top-level "template"
132125
that can be referenced for cost/power/capacity lookups, possibly with nested children.
133126
134127
Example (YAML-like):
@@ -155,8 +148,7 @@ class ComponentsLibrary:
155148
components: Dict[str, Component] = field(default_factory=dict)
156149

157150
def get(self, name: str) -> Optional[Component]:
158-
"""
159-
Retrieves a Component by its name from the library.
151+
"""Retrieves a Component by its name from the library.
160152
161153
Args:
162154
name (str): Name of the component.
@@ -169,8 +161,7 @@ def get(self, name: str) -> Optional[Component]:
169161
def merge(
170162
self, other: ComponentsLibrary, override: bool = True
171163
) -> ComponentsLibrary:
172-
"""
173-
Merges another ComponentsLibrary into this one. By default (override=True),
164+
"""Merges another ComponentsLibrary into this one. By default (override=True),
174165
duplicate components in `other` overwrite those in the current library.
175166
176167
Args:
@@ -186,8 +177,7 @@ def merge(
186177
return self
187178

188179
def clone(self) -> ComponentsLibrary:
189-
"""
190-
Creates a deep copy of this ComponentsLibrary.
180+
"""Creates a deep copy of this ComponentsLibrary.
191181
192182
Returns:
193183
ComponentsLibrary: A new, cloned library instance.
@@ -196,8 +186,7 @@ def clone(self) -> ComponentsLibrary:
196186

197187
@classmethod
198188
def from_dict(cls, data: Dict[str, Any]) -> ComponentsLibrary:
199-
"""
200-
Constructs a ComponentsLibrary from a dictionary of raw component definitions.
189+
"""Constructs a ComponentsLibrary from a dictionary of raw component definitions.
201190
202191
Args:
203192
data (Dict[str, Any]): Raw component definitions.
@@ -212,8 +201,7 @@ def from_dict(cls, data: Dict[str, Any]) -> ComponentsLibrary:
212201

213202
@classmethod
214203
def _build_component(cls, name: str, definition_data: Dict[str, Any]) -> Component:
215-
"""
216-
Recursively constructs a single Component from a dictionary definition.
204+
"""Recursively constructs a single Component from a dictionary definition.
217205
218206
Args:
219207
name (str): Name of the component.
@@ -269,8 +257,7 @@ def _build_component(cls, name: str, definition_data: Dict[str, Any]) -> Compone
269257

270258
@classmethod
271259
def from_yaml(cls, yaml_str: str) -> ComponentsLibrary:
272-
"""
273-
Constructs a ComponentsLibrary from a YAML string. If the YAML contains
260+
"""Constructs a ComponentsLibrary from a YAML string. If the YAML contains
274261
a top-level 'components' key, that key is used; otherwise the entire
275262
top-level is treated as component definitions.
276263

0 commit comments

Comments
 (0)