Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] Autogenerating lazy initialization everywhere #4496

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Erotemic
Copy link
Contributor

@Erotemic Erotemic commented Dec 27, 2020

Forked from the conversation in #4349 this is a test to see how easy it is to switch to mkinit for __init__.py file management. This tests to see if lazy imports work. So far I have pytest running with 134 failed, 4115 passed.

The top level __init__.py looks somewhat like this now:

__getattr__ = lazy_import(
    __name__,
    submodules={
        "algorithms",
        "classes",
        "conftest",
        "convert",
        "convert_matrix",
        "drawing",
        "exception",
        "generators",
        "linalg",
        "readwrite",
        "relabel",
        "release",
        "testing",
        "tests",
        "utils",
        "version",
    },
    submod_attrs={
        "algorithms": [
            "CGInverseLaplacian",
            "CurrentEdge",
            "DiGraphMatcher",
            "EdgeComponentAuxGraph",
            "Edmonds",
       ...
            "vitality",
            "volume",
            "voronoi",
            "voronoi_cells",
            "voterank",
            "voterank_alg",
            "weakly_connected",
            "weakly_connected_components",
            "weighted",
            "weighted_projected_graph",
            "weisfeiler_lehman_graph_hash",
            "wiener",
            "wiener_index",
            "within_inter_cluster",
            "write_edgelist",
        ],
        "classes": [
            "AdjacencyView",
            "AtlasView",
            "DegreeView",
            "DiDegreeView",
            "DiGraph",
            "DiMultiDegreeView",
            "EdgeDataView",
            "EdgeView",
       ...
            "selfloop_edges",
            "set_edge_attributes",
            "set_node_attributes",
            "show_diedges",
            "show_edges",
            "show_multidiedges",
            "show_multiedges",
            "show_nodes",
            "subgraph",
            "subgraph_view",
            "tests",
            "to_directed",
            "to_undirected",
        ],
        "conftest": [
            "add_nx",
            "collect_ignore",
            "has_matplotlib",
            "has_numpy",
            "has_ogr",
       ...
            "pytest_addoption",
            "pytest_collection_modifyitems",
            "pytest_configure",
            "set_warnings",
        ],
        "convert": [
            "from_dict_of_dicts",
            "from_dict_of_lists",
            "from_edgelist",
            "to_dict_of_dicts",
            "to_dict_of_lists",
            "to_edgelist",
            "to_networkx_graph",
        ],
        "convert_matrix": [
            "from_numpy_array",
            "from_numpy_matrix",
            "from_pandas_adjacency",
            "from_pandas_edgelist",
            "from_scipy_sparse_matrix",
            "to_numpy_array",
            "to_numpy_matrix",
            "to_numpy_recarray",
            "to_pandas_adjacency",
            "to_pandas_edgelist",
            "to_scipy_sparse_matrix",
        ],
        "drawing": [
            "bipartite_layout",
            "circular_layout",
            "draw",
            "draw_circular",
            "draw_kamada_kawai",
            "draw_networkx",
            "draw_networkx_edge_labels",
            "draw_networkx_edges",
            "draw_networkx_labels",
            "draw_networkx_nodes",
...
            "spring_layout",
            "tests",
            "to_agraph",
            "to_pydot",
            "view_pygraphviz",
            "write_dot",
        ],
        "exception": [
            "AmbiguousSolution",
            "ExceededMaxIterations",
            "HasACycle",
            "NetworkXAlgorithmError",
            "NetworkXError",
            "NetworkXException",
            "NetworkXNoCycle",
            "NetworkXNoPath",
            "NetworkXNotImplemented",
            "NetworkXPointlessConcept",
            "NetworkXUnbounded",
            "NetworkXUnfeasible",
            "NodeNotFound",
            "PowerIterationFailedConvergence",
        ],
        "generators": [
            "LCF_graph",
            "LFR_benchmark_graph",
            "atlas",
            "balanced_tree",
            "barabasi_albert_graph",
            "barbell_graph",
            "binomial_graph",
            "binomial_tree",
            "bull_graph",
            "caveman_graph",
            "chordal_cycle_graph",
            "chvatal_graph",
            "circulant_graph",
            "circular_ladder_graph",
            "classic",
            "cographs",...
            "triads",
            "triangular_lattice_graph",
            "trivial_graph",
            "truncated_cube_graph",
            "truncated_tetrahedron_graph",
            "turan_graph",
            "tutte_graph",
            "uniform_random_intersection_graph",
            "watts_strogatz_graph",
            "waxman_graph",
            "wheel_graph",
            "windmill_graph",
        ],
        "linalg": [
            "adj_matrix",
            "adjacency_matrix",
            "adjacency_spectrum",
            "algebraic_connectivity",
            "algebraicconnectivity",
            "attr_matrix",
            "attr_sparse_matrix",
...
            "modularity_matrix",
            "modularity_spectrum",
            "modularitymatrix",
            "normalized_laplacian_matrix",
            "normalized_laplacian_spectrum",
            "spectral_ordering",
            "spectrum",
            "tests",
        ],
        "readwrite": [
            "GraphMLReader",
            "GraphMLWriter",
            "adjacency",
            "adjacency_data",
            "adjacency_graph",
            "adjlist",
            "cytoscape",
            "cytoscape_data",
            "cytoscape_graph",
            "edgelist",
            "forest_str",
            "from_graph6_bytes",
            "from_sparse6_bytes",
            "generate_adjlist",
            "generate_edgelist",
            "generate_gexf",
            "generate_gml",
            "generate_graphml",
            "generate_multiline_adjlist",
            "generate_pajek",
            "gexf",
            "gml",
            "gpickle",
...            "write_p2g",
            "write_pajek",
            "write_shp",
            "write_sparse6",
            "write_weighted_edgelist",
            "write_yaml",
        ],
        "relabel": [
            "convert_node_labels_to_integers",
            "relabel_nodes",
        ],
        "testing": [
            "almost_equal",
            "assert_edges_equal",
            "assert_graphs_equal",
            "assert_nodes_equal",
            "run",
            "test",
            "tests",
            "utils",
        ],
        "utils": [
            "BinaryHeap",
            "MappedQueue",
            "MinHeap",
            "PairingHeap",
            "PythonRandomInterface",
            "UnionFind",
            "arbitrary_element",
            ...
            "reverse_cuthill_mckee_ordering",
            "reversed",
            "tests",
            "to_tuple",
            "union_find",
            "weighted_choice",
            "zipf_rv",
        ],
        "version": [
            "date",
            "date_info",
            "dev",
            "vcs_info",
            "version",
            "version_info",
        ],
    },
)


def __dir__():
    return __all__


__all__ = [
    "AdjacencyView",
    "AmbiguousSolution",
    "AtlasView",
    "BinaryHeap",
    "CGInverseLaplacian",
    "CurrentEdge",
    "DegreeView",
    "DiDegreeView",
    "DiGraph",
    "DiGraphMatcher",
    "DiMultiDegreeView",
    "EdgeComponentAuxGraph",
    "EdgeDataView",
    "EdgeView",
    "Edmonds",
    "ExceededMaxIterations",
    "FilterAdjacency",
    "FilterAtlas",
    "FilterMultiAdjacency",
    "FilterMultiInner",
...
    "write_adjlist",
    "write_dot",
    "write_edgelist",
    "write_gexf",
    "write_gml",
    "write_gpickle",
    "write_graph6",
    "write_graphml",
    "write_graphml_lxml",
    "write_graphml_xml",
    "write_multiline_adjlist",
    "write_p2g",
    "write_pajek",
    "write_shp",
    "write_sparse6",
    "write_weighted_edgelist",
    "write_yaml",
    "zipf_rv",
]

Base automatically changed from master to main March 4, 2021 18:20
@stefanv
Copy link
Contributor

stefanv commented Nov 22, 2022

It'd be great if mkinit could support the new stubs loader as well.

See also #6048

@Erotemic
Copy link
Contributor Author

Erotemic commented Dec 3, 2022

@stefanv Initial support for using lazy_loader inside mkinit is added in the 1.0.0 branch: Erotemic/mkinit#32 (which I will release when tests pass).

Using mkinit --lazy_loader will now use the lazy_loader module instead of using its own boilerplate. Not quite sure how you were thinking of integrating stubs support with mkinit. Please make an issue on the mkinit repo if you can provide more details on your thoughts there.

@stefanv
Copy link
Contributor

stefanv commented Dec 4, 2022

Thanks, Jon! I'm out of office for a bit, but will check when I get back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants