diff --git a/__init__.py b/__init__.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..eb5e7d3cba4e2ec949ce90832e3d8651063b94dd 100644 --- a/__init__.py +++ b/__init__.py @@ -0,0 +1,22 @@ +""" +Library base importing definitions. +""" + +# Import files/values we want to be available to library users. +from .resources.basic_graph.edge import Edge as BasicEdge +from .resources.basic_graph.node import Node as BasicNode +from .resources.basic_graph.graph import Graph as BasicGraph +from .resources.directed_graph.edge import Edge as DirectedEdge +from .resources.directed_graph.node import Node as DirectedNode +from .resources.directed_graph.graph import Graph as DirectedGraph +from .resources.state_machine.edge import Edge as StateMachineEdge +from .resources.state_machine.node import Node as StateMachineNode +from .resources.state_machine.graph import Graph as StateMachineGraph + + +# Define imports when using the * flag on this library. +__all__ = [ + BasicEdge, BasicNode, BasicGraph, + DirectedEdge, DirectedNode, DirectedGraph, + StateMachineEdge, StateMachineNode, StateMachineGraph, +] diff --git a/documents/references.md b/documents/references.md index 15690296b2fe876a3b7e7111507d30f2579a793b..18f386e39ffac3b54fede19b2f5e00080b5ba31a 100644 --- a/documents/references.md +++ b/documents/references.md @@ -8,6 +8,13 @@ All references to external logic. Includes anything from stack overflow links to ## New References References new to this project. +### Python Project as a Library +Various recommendations for using a project as a library for other projects: +* Don't configure extra handlers inside the library: <https://docs.python-guide.org/writing/logging/> +* General Library Rules, Mostly Concerning \_\_init\_\_.py: +<https://axialcorps.wordpress.com/2013/08/29/5-simple-rules-for-building-great-python-packages/> +* Many Recommendations for Project Structure: <https://docs.python-guide.org/writing/structure/> + ### Git Submodules Since I don't know how Python projects are exported to be a pip library (and I'm not even sure if that's a good use case for this project), it's being set up as a submodule project for now. diff --git a/resources/basic_graph/edge.py b/resources/basic_graph/edge.py index 4d40220e2f04c336b7fb98724833a8f65fdae2b1..57de3be77cb14d2d418724a023484302c27b904e 100644 --- a/resources/basic_graph/edge.py +++ b/resources/basic_graph/edge.py @@ -14,15 +14,27 @@ Essentially the "node connection" in a Graph data structure. # User Class Imports. import resources.basic_graph.node as basic_node -from resources import logging as init_logging -# Initialize Logger. -logger = init_logging.get_logger(__name__) +# Initialize logging. +# Since this is a library, we specifically only modify logging if the library is being run alone. +try: + # First, try to import the variable specific to our library logging. + from resources.logging import graph_library_logger + + # It worked, so we know the project is being run stand alone, probably as a unittest. + # Proceed to configure logging. + from resources.logging import get_logger as init_logging + logger = init_logging(__name__) +except ModuleNotFoundError: + # Above import failed. Project is being run as a library. + # Just import existing logger and do not modify. + import logging as init_logging + logger = init_logging.getLogger('graph_library') class Edge(): - def __init__(self, name): + def __init__(self, name, *args, **kwargs): # Check for passed arg types. if name is None: raise TypeError('Edge must be passed a string-convertable name.') diff --git a/resources/basic_graph/graph.py b/resources/basic_graph/graph.py index c8902497a3871e2bd8f8e9fb65237f3aa3326224..34a7a3249de24dc5c0e19d5b435acebf0f3e2668 100644 --- a/resources/basic_graph/graph.py +++ b/resources/basic_graph/graph.py @@ -15,15 +15,27 @@ from random import randint # User Class Imports. import resources.basic_graph.edge as basic_edge import resources.basic_graph.node as basic_node -from resources import logging as init_logging -# Initialize Logger. -logger = init_logging.get_logger(__name__) +# Initialize logging. +# Since this is a library, we specifically only modify logging if the library is being run alone. +try: + # First, try to import the variable specific to our library logging. + from resources.logging import graph_library_logger + + # It worked, so we know the project is being run stand alone, probably as a unittest. + # Proceed to configure logging. + from resources.logging import get_logger as init_logging + logger = init_logging(__name__) +except ModuleNotFoundError: + # Above import failed. Project is being run as a library. + # Just import existing logger and do not modify. + import logging as init_logging + logger = init_logging.getLogger('graph_library') class Graph(): - def __init__(self): + def __init__(self, *args, **kwargs): # Lists of nodes and edges in graph. self._edges = {} self._nodes = {} diff --git a/resources/basic_graph/node.py b/resources/basic_graph/node.py index 2dfdb508e72b102340043b995068df8d64d77c7d..a811174e0714bc4bbc08a9028fc5870ba8918142 100644 --- a/resources/basic_graph/node.py +++ b/resources/basic_graph/node.py @@ -14,15 +14,27 @@ Essentially the "vertex" in a Graph data structure. # User Class Imports. import resources.basic_graph.edge as basic_edge -from resources import logging as init_logging -# Initialize Logger. -logger = init_logging.get_logger(__name__) +# Initialize logging. +# Since this is a library, we specifically only modify logging if the library is being run alone. +try: + # First, try to import the variable specific to our library logging. + from resources.logging import graph_library_logger + + # It worked, so we know the project is being run stand alone, probably as a unittest. + # Proceed to configure logging. + from resources.logging import get_logger as init_logging + logger = init_logging(__name__) +except ModuleNotFoundError: + # Above import failed. Project is being run as a library. + # Just import existing logger and do not modify. + import logging as init_logging + logger = init_logging.getLogger('graph_library') class Node(): - def __init__(self, name): + def __init__(self, name, *args, **kwargs): # Check for passed arg types. if name is None: raise TypeError('Node must be passed a string-convertable name.') diff --git a/resources/directed_graph/edge.py b/resources/directed_graph/edge.py index 4302a2519ffa65d4144762f8d9f0c39bab7f4033..0f1efef0de73ba415599181bd8b423f6f4dc0148 100644 --- a/resources/directed_graph/edge.py +++ b/resources/directed_graph/edge.py @@ -15,11 +15,23 @@ Essentially the "node connection" in a Graph data structure. # User Class Imports. import resources.basic_graph.edge as basic_edge import resources.directed_graph.node as directed_node -from resources import logging as init_logging -# Initialize Logger. -logger = init_logging.get_logger(__name__) +# Initialize logging. +# Since this is a library, we specifically only modify logging if the library is being run alone. +try: + # First, try to import the variable specific to our library logging. + from resources.logging import graph_library_logger + + # It worked, so we know the project is being run stand alone, probably as a unittest. + # Proceed to configure logging. + from resources.logging import get_logger as init_logging + logger = init_logging(__name__) +except ModuleNotFoundError: + # Above import failed. Project is being run as a library. + # Just import existing logger and do not modify. + import logging as init_logging + logger = init_logging.getLogger('graph_library') class Edge(basic_edge.Edge): diff --git a/resources/directed_graph/graph.py b/resources/directed_graph/graph.py index 97097eb0c8b5c0e5f27c2f02703d39f4086f1978..20753a8eee8636f298ed19f013eb7f842e2fb0d8 100644 --- a/resources/directed_graph/graph.py +++ b/resources/directed_graph/graph.py @@ -16,11 +16,23 @@ import math import resources.basic_graph.graph as basic_graph import resources.directed_graph.edge as directed_edge import resources.directed_graph.node as directed_node -from resources import logging as init_logging -# Initialize Logger. -logger = init_logging.get_logger(__name__) +# Initialize logging. +# Since this is a library, we specifically only modify logging if the library is being run alone. +try: + # First, try to import the variable specific to our library logging. + from resources.logging import graph_library_logger + + # It worked, so we know the project is being run stand alone, probably as a unittest. + # Proceed to configure logging. + from resources.logging import get_logger as init_logging + logger = init_logging(__name__) +except ModuleNotFoundError: + # Above import failed. Project is being run as a library. + # Just import existing logger and do not modify. + import logging as init_logging + logger = init_logging.getLogger('graph_library') class Graph(basic_graph.Graph): diff --git a/resources/directed_graph/node.py b/resources/directed_graph/node.py index 634d49e7cde6a63c2f20f8daf6fb66c9ff99b1d0..801c751037d017866efe3c0bb6e4094267aeb6f4 100644 --- a/resources/directed_graph/node.py +++ b/resources/directed_graph/node.py @@ -15,11 +15,23 @@ Essentially the "vertex" in a Graph data structure. # User Class Imports. import resources.basic_graph.node as basic_node import resources.directed_graph.edge as directed_edge -from resources import logging as init_logging -# Initialize Logger. -logger = init_logging.get_logger(__name__) +# Initialize logging. +# Since this is a library, we specifically only modify logging if the library is being run alone. +try: + # First, try to import the variable specific to our library logging. + from resources.logging import graph_library_logger + + # It worked, so we know the project is being run stand alone, probably as a unittest. + # Proceed to configure logging. + from resources.logging import get_logger as init_logging + logger = init_logging(__name__) +except ModuleNotFoundError: + # Above import failed. Project is being run as a library. + # Just import existing logger and do not modify. + import logging as init_logging + logger = init_logging.getLogger('graph_library') class Node(basic_node.Node): diff --git a/resources/state_machine/edge.py b/resources/state_machine/edge.py index 6a1fbdca0ab8b50fca624a0aa897857b0ce6d1e6..36a93100ef00b6ef788b6a6d1341cc0a37fe42d4 100644 --- a/resources/state_machine/edge.py +++ b/resources/state_machine/edge.py @@ -14,11 +14,23 @@ Essentially the "node connection" in a Graph data structure. # User Class Imports. import resources.directed_graph.edge as directed_edge import resources.state_machine.node as sm_node -from resources import logging as init_logging -# Initialize Logger. -logger = init_logging.get_logger(__name__) +# Initialize logging. +# Since this is a library, we specifically only modify logging if the library is being run alone. +try: + # First, try to import the variable specific to our library logging. + from resources.logging import graph_library_logger + + # It worked, so we know the project is being run stand alone, probably as a unittest. + # Proceed to configure logging. + from resources.logging import get_logger as init_logging + logger = init_logging(__name__) +except ModuleNotFoundError: + # Above import failed. Project is being run as a library. + # Just import existing logger and do not modify. + import logging as init_logging + logger = init_logging.getLogger('graph_library') class Edge(directed_edge.Edge): diff --git a/resources/state_machine/graph.py b/resources/state_machine/graph.py index 499c8894ff2f830653aad1e9450a0aef79093e4f..fe5430f54319ae79b5838e2a8fff212d7d4b99f0 100644 --- a/resources/state_machine/graph.py +++ b/resources/state_machine/graph.py @@ -10,17 +10,28 @@ Parent structure containing various Nodes and Edges. """ # System Imports. -import math # User Class Imports. import resources.directed_graph.graph as directed_graph import resources.state_machine.edge as sm_edge import resources.state_machine.node as sm_node -from resources import logging as init_logging -# Initialize Logger. -logger = init_logging.get_logger(__name__) +# Initialize logging. +# Since this is a library, we specifically only modify logging if the library is being run alone. +try: + # First, try to import the variable specific to our library logging. + from resources.logging import graph_library_logger + + # It worked, so we know the project is being run stand alone, probably as a unittest. + # Proceed to configure logging. + from resources.logging import get_logger as init_logging + logger = init_logging(__name__) +except ModuleNotFoundError: + # Above import failed. Project is being run as a library. + # Just import existing logger and do not modify. + import logging as init_logging + logger = init_logging.getLogger('graph_library') class Graph(directed_graph.Graph): diff --git a/resources/state_machine/node.py b/resources/state_machine/node.py index 965e83700dbf24249b305376c9e1b78001d5224f..3be00b6db21b73efd6d7417f15529244687a2923 100644 --- a/resources/state_machine/node.py +++ b/resources/state_machine/node.py @@ -14,11 +14,23 @@ Essentially the "vertex" in a Graph data structure. # User Class Imports. import resources.directed_graph.node as directed_node import resources.state_machine.edge as sm_edge -from resources import logging as init_logging -# Initialize Logger. -logger = init_logging.get_logger(__name__) +# Initialize logging. +# Since this is a library, we specifically only modify logging if the library is being run alone. +try: + # First, try to import the variable specific to our library logging. + from resources.logging import graph_library_logger + + # It worked, so we know the project is being run stand alone, probably as a unittest. + # Proceed to configure logging. + from resources.logging import get_logger as init_logging + logger = init_logging(__name__) +except ModuleNotFoundError: + # Above import failed. Project is being run as a library. + # Just import existing logger and do not modify. + import logging as init_logging + logger = init_logging.getLogger('graph_library') class Node(directed_node.Node):