FrankenNetworkX

Guide/In production

Style Guide for Code That Uses fnx

1 min readFrom the README

On this page 6
Prefer fnx.X(G) to G.X() where both existDon't rely on private internalsCatch the broadest reasonable exceptionUse named kwargs for keyword-only parametersDon't iterate views during mutationSnapshot views before subprocess transfer

How to write code that flips cleanly between fnx and nx without surprises.

Prefer fnx.X(G) to G.X() where both exist

# Both work, but the module-level form is the documented contract.
fnx.shortest_path(G, "a", "z")        # preferred
G.shortest_path("a", "z")              # not all graph types expose this

Don't rely on private internals

The audit ledgers track the public surface. Anything starting with _ (e.g. fnx._fnx, fnx._sync_rust_edge_attrs, fnx.backend._SUPPORTED_ALGORITHMS) is not part of the contract and may move between releases.

Catch the broadest reasonable exception

# Brittle (won't catch NetworkXNoPath, since it's under NetworkXUnfeasible)
try:
    path = fnx.shortest_path(G, s, t)
except fnx.NetworkXError:   # ← does NOT catch NetworkXNoPath!
    ...

# Correct
try:
    path = fnx.shortest_path(G, s, t)
except fnx.NetworkXNoPath:
    ...
# Or, if you want a catch-all
except fnx.NetworkXException:
    ...

Use named kwargs for keyword-only parameters

# Good
fnx.shortest_path(G, "a", "z", weight="weight")
fnx.pagerank(G, alpha=0.85, max_iter=100, tol=1e-6)

# Bad: positional args past 2 are fragile, fnx mirrors nx's keyword-only contract
fnx.pagerank(G, 0.85, 100, 1e-6)   # works today, may not tomorrow

Don't iterate views during mutation

Same rule as NetworkX:

# Bad
for u in G.nodes():
    if G.degree(u) == 0:
        G.remove_node(u)        # raises RuntimeError mid-iteration

# Good
isolates = list(fnx.isolates(G))
for u in isolates:
    G.remove_node(u)

Snapshot views before subprocess transfer

G.subgraph([...]) returns a view, not a fresh graph. If you want to pickle it or pass it across an async boundary, call .copy():

sub = G.subgraph(important_nodes)         # view; aliases G
sub_copy = G.subgraph(important_nodes).copy()  # standalone graph
pickle.dumps(sub_copy)                    # ← this is what you want for IPC