Skip to content

Commit

Permalink
DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
eriknw authored and MridulS committed Mar 31, 2024
1 parent ddfbd5c commit 7210b01
Showing 1 changed file with 20 additions and 36 deletions.
56 changes: 20 additions & 36 deletions networkx/utils/backends.py
Expand Up @@ -1024,6 +1024,24 @@ def _convert_graph(
preserve_graph_attrs,
)
if cache:
warning_message = (
f"Using cached graph for {backend_name!r} backend in "
f"call to {self.name}.\n\nFor the cache to be consistent "
"(i.e., correct), the input graph must not have been "
"manually mutated since the cached graph was created. "
"Examples of manually mutating the graph data structures "
"resulting in an inconsistent cache include:\n\n"
" >>> G[u][v][key] = val\n\n"
"and\n\n"
" >>> for u, v, d in G.edges(data=True):\n"
" ... d[key] = val\n\n"
"Using methods such as `G.add_edge(u, v, weight=val)` "
"will correctly clear the cache to keep it consistent. "
"You may also use `G.__networkx_cache__.clear()` to "
"manually clear the cache, or set `G.__networkx_cache__` "
"to None to disable caching for G. Enable or disable "
"caching via `nx.config.cache_converted_graphs` config."
)
# Do a simple search for a cached graph with compatible data.
# For example, if we need a single attribute, then it's okay
# to use a cached graph that preserved all attributes.
Expand All @@ -1034,24 +1052,7 @@ def _convert_graph(
(graph_key, True) if graph_key is not True else (True,),
):
if (rv := cache.get(compat_key)) is not None:
warnings.warn(
f"Using cached graph for {backend_name!r} backend in "
f"call to {self.name}.\n\nFor the cache to be consistent "
"(i.e., correct), the input graph must not have been "
"manually mutated since the cached graph was created. "
"Examples of manually mutating the graph data structures "
"resulting in an inconsistent cache include:\n\n"
" >>> G[u][v][key] = val\n\n"
"and\n\n"
" >>> for u, v, d in G.edges(data=True):\n"
" ... d[key] = val\n\n"
"Using methods such as `G.add_edge(u, v, weight=val)` "
"will correctly clear the cache to keep it consistent. "
"You may also use `G.__networkx_cache__.clear()` to "
"manually clear the cache, or set `G.__networkx_cache__` "
"to None to disable caching for G. Enable or disable "
"caching via `nx.config.cache_converted_graphs` config."
)
warnings.warn(warning_message)
return rv
if edge_key is not True and node_key is not True:
# Iterate over the items in `cache` to see if any are compatible.
Expand All @@ -1078,24 +1079,7 @@ def _convert_graph(
continue
if graph_key and not gkey:
continue
warnings.warn(
f"Using cached graph for {backend_name!r} backend in "
f"call to {self.name}.\n\nFor the cache to be consistent "
"(i.e., correct), the input graph must not have been "
"manually mutated since the cached graph was created. "
"Examples of manually mutating the graph data structures "
"resulting in an inconsistent cache include:\n\n"
" >>> G[u][v][key] = val\n\n"
"and\n\n"
" >>> for u, v, d in G.edges(data=True):\n"
" ... d[key] = val\n\n"
"Using methods such as `G.add_edge(u, v, weight=val)` "
"will correctly clear the cache to keep it consistent. "
"You may also use `G.__networkx_cache__.clear()` to "
"manually clear the cache, or set `G.__networkx_cache__` "
"to None to disable caching for G. Enable or disable "
"caching via `nx.config.cache_converted_graphs` config."
)
warnings.warn(warning_message)
return val

backend = _load_backend(backend_name)
Expand Down

0 comments on commit 7210b01

Please sign in to comment.