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

Circular import #29

Open
jeffsilverm opened this issue Jan 13, 2019 · 3 comments
Open

Circular import #29

jeffsilverm opened this issue Jan 13, 2019 · 3 comments
Assignees

Comments

@jeffsilverm
Copy link
Owner

There is a circular import

ImportError while importing test module '/home/jeffs/python/nbmdt/test/test_utilities.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test/test_utilities.py:12: in <module>
    import utilities
utilities.py:13: in <module>
    from nbmdt import SystemDescription
nbmdt.py:21: in <module>
    import datalink  # OSI layer 2: # Media Access Control: arp, ndp
datalink.py:14: in <module>ImportError while importing test module '/home/jeffs/python/nbmdt/test/test_utilities.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test/test_utilities.py:12: in <module>
    import utilities
utilities.py:13: in <module>
    from nbmdt import SystemDescription
nbmdt.py:21: in <module>
    import datalink  # OSI layer 2: # Media Access Control: arp, ndp
datalink.py:14: in <module>
    from interface import Interface
interface.py:12: in <module>
    from utilities import OsCliInter, os_name, the_os
E   ImportError: cannot import name 'OsCliInter'

    from interface import Interface
interface.py:12: in <module>
    from utilities import OsCliInter, os_name, the_os
E   ImportError: cannot import name 'OsCliInter'
@jeffsilverm
Copy link
Owner Author

jeffsilverm commented Jan 13, 2019

I think the problem is that utilities can't finish importing until it has nbmdt.SystemDecription. But nbmdt imports datalink and so on.

I think the solution is to move class SystemDescription into utilities.

jeffs@jeffs-desktop:/home/jeffs/python/nbmdt  (development) *  $ fgrep -n SystemDescription *.py */*.py
nbmdt.py:92:    current_system: utilities.SystemDescription = utilities.SystemDescription.discover()
nbmdt.py:134:    current_system = utilities.SystemDescription(mode=mode)
nbmdt.py:168:    nominal_system_description = utilities.SystemDescription ( configuration_file="nominal.txt" )
nbmdt.py:169:    current_system_description = utilities.SystemDescription ( )
utilities.py:13:from nbmdt import SystemDescription
utilities.py:40:class SystemDescription(object):
utilities.py:62:        a constructor, and all it does is create a SystemDescription object.
utilities.py:84:    def system_description_from_file(cls, filename: str) -> 'SystemDescription':
utilities.py:86:        Read a system description from a file and create a SystemDescription object.
utilities.py:87:        I couldn't figure out how to return a SystemDescription object, so I return an object
utilities.py:96:        return SystemDescription(
utilities.py:112:        :return: a SystemDescription object.
utilities.py:125:        my_system: object = SystemDescription(
utilities.py:215:            # nominal = SystemDescription.describe_current_state()
utilities.py:247:        nominal_system: SystemDescription = SystemDescription.system_description_from_file(filename)
utilities.py:256:class SystemDescriptionFile(SystemDescription):
utilities.py:262:        """Create a SystemDescription object which has all of the information in a system configuration file"""
utilities.py:266:        super(SystemDescriptionFile, self).__init__(applications=c_dict["applications"],
test/test_nbmdt.py:26:class testSystemDescription():
test/test_nbmdt.py:29:# Copy the rest of the methods from class SystemDescription in nbmdt.py to here
test/test_nbmdt.py:44:        a constructor, and all it does is create a SystemDescription object.
test/test_utilities.py:13:from nbmdt import SystemDescription
test/test_utilities.py:20:sd_obj = utilities.SystemDescriptionFile( TEST_CONFIGURATION_FILE )
test/test_utilities.py:21:assert isinstance( sd_obj, SystemDescription ), f"sd_obj is not an instance of SystemDescription, it's {type(sd_obj)}"
jeffs@jeffs-desktop:/home/jeffs/python/nbmdt  (development) *  $ 

@jeffsilverm
Copy link
Owner Author

There is another problem, this time with OsCliInter:

effs@jeffs-desktop:/home/jeffs/python/nbmdt  (development) *  $ python3 nbmdt.py
Testing the __file__ special variable: /home/jeffs/python/nbmdt/application.py
Traceback (most recent call last):
  File "nbmdt.py", line 16, in <module>
    import utilities
  File "/home/jeffs/python/nbmdt/utilities.py", line 13, in <module>
    from nbmdt import SystemDescription
  File "/home/jeffs/python/nbmdt/nbmdt.py", line 22, in <module>
    import datalink  # OSI layer 2: # Media Access Control: arp, ndp
  File "/home/jeffs/python/nbmdt/datalink.py", line 14, in <module>
    from interface import Interface
  File "/home/jeffs/python/nbmdt/interface.py", line 12, in <module>
    from utilities import OsCliInter, os_name, the_os
ImportError: cannot import name 'OsCliInter'
jeffs@jeffs-desktop:/home/jeffs/python/nbmdt  (development) *  $ 

jeffs@jeffs-desktop:/home/jeffs/python/nbmdt  (development) *  $ fgrep OsCliInter *.py */*.py
application.py:            apps_str: str = utilities.OsCliInter.run_command(["ps", "-ax"])
datalink.py:from utilities import OsCliInter, os_name, the_os
datalink.py:            datalinks_str: str = OsCliInter.run_command(cls.DISCOVER_ALL_DATALINKS_COMMAND)
datalink.py:            raise NotImplemented(f"{OsCliInter.system} is not implemented yet in DataLink")
guts_of_interface.py:        lnk_str: str = OsCliInter.run_command(PhysicalLink.DISCOVER_LINK_COMMAND)
guts_of_interface.py:            raise NotImplemented(f"{OsCliInter.system} is not implemented yet in PhysicalLink")
guts_of_interface.py:            raise NotImplementedError(f"In DataLink.discover, {OsCliInter.system} is not implemented")
guts_of_interface.py:        lnk_str:str = OsCliInter.run_command(cls.DISCOVER_ADDR_COMMAND )
interface.py:from utilities import OsCliInter, os_name, the_os
interface.py:        results = OsCliInter.run_command([Interface.IP_COMMAND, "link", "list"])
physical.py:        results = utilities.OsCliInter.run_command(command=command)
physical.py:        if "linux" == utilities.OsCliInter.system:
physical.py:        elif "windows" == utilities.OsCliInter.system:
physical.py:        elif 'darwin' == utilities.OsCliInter.system:
physical.py:        elif 'java' == utilities.OsCliInter.system:
physical.py:    results_xml:str = utilities.OsCliInter.run_command(command=command)
utilities.py:class OsCliInter(object):
utilities.py:    # Since the system is going to be the same across the entire program, populate it when the OsCliInter object is
utilities.py:    # imported for the first time and then make it available to any object in class OsCliInter (which should not need
utilities.py:# of class OsCliInter or else the compiler will raise a NameError exception at compile time
utilities.py:os_name: str = OsCliInter.system.lower()
utilities.py:        print(f"In linux, the uname -a command output is \n{OsCliInter.run_command(['uname', '-a'])}\n.")
test/test_interface.py:@patch("utilities.OsCliInter.run_command")
test/test_interface.py:@patch("utilities.OsCliInter.run_command")
test/test_interface.py:@patch("utilities.OsCliInter.run_command")
test/test_interface.py:@patch("utilities.OsCliInter.run_command")
test/test_interface.py:@patch("utilities.OsCliInter.run_command")
jeffs@jeffs-desktop:/home/jeffs/python/nbmdt  (development) *  $ 

Resolve Issue 29 before proceeding.

@jeffsilverm jeffsilverm self-assigned this Nov 8, 2019
@jeffsilverm
Copy link
Owner Author

The easy way to test this is to have utilities.py run by itself.

    jeffs@evil-kali:~/nbmdt$ python3 utilities.py 
    Enter an issue that I changed the values of the constants from integers to strings for reliability
    Traceback (most recent call last):
      File "utilities.py", line 14, in <module>
        import application
      File "/home/jeffs/nbmdt/application.py", line 20, in <module>
        import utilities
      File "/home/jeffs/nbmdt/utilities.py", line 16, in <module>
        import datalink
      File "/home/jeffs/nbmdt/datalink.py", line 15, in <module>
        from utilities import OsCliInter, os_name, the_os
    ImportError: cannot import name 'OsCliInter' from 'utilities' (/home/jeffs/nbmdt/utilities.py)
    jeffs@evil-kali:~/nbmdt$ 

Why is this happening?

jeffs@evil-kali:~/nbmdt$ fgrep OsCliInter utilities.py 
class OsCliInter(object):
# Since the system is going to be the same across the entire program, populate it when the OsCliInter object is
# imported for the first time and then make it available to any object in class OsCliInter (which should not need
# of class OsCliInter or else the compiler will raise a NameError exception at compile time
os_name: str = OsCliInter.system.lower()
        print(f"In linux, the uname -a command output is \n{OsCliInter.run_command(['uname', '-a'])}\n.")
jeffs@evil-kali:~/nbmdt$ 

Figure out to import an inner class

jeffsilverm added a commit that referenced this issue Nov 8, 2019
jeffsilverm added a commit that referenced this issue Nov 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant