I’ve got a class file that I have written that is basically a helper library so I can use it across multiple tools. Generally speaking the Python community seems to recommend that imports are at the top of the script and that the imports should support the requirements of the classfile. However, when doing that I don’t really see it working that way. Python throws errors like modules aren’t imported. So here I have a small script:
#!/usr/bin/python import logging import logging.handlers import RPi.GPIO as GPIO import sys import time from cellHandler import CellHandler # Global Variables power_gpio = 4 # This is the GPIO pin from RPi that triggers the SIM to startup # Set up logging my_logger = logging.getLogger("SantaTracker") my_logger.setLevel(logging.DEBUG) # Set the logging level here handler = logging.handlers.SysLogHandler(address = '/dev/log') handler.ident = "SantaTracaker: " my_logger.addHandler(handler) # Psuedo main() def main(): print("Starting up the cellular module") try: CH = CellHandler(power_gpio, "/dev/ttyS0", my_logger) CH.startup() time.sleep(10) print("Requesting GPS") bob = CH.get_gps() print(bob) except Exception as e: print(f"Unexpected Error: {e}") my_logger.error(f"Unexpected Error: {e}") if __name__=="__main__": my_logger.info('Starting up cellular module') my_logger.debug('Entering main()')
And in the class file I’ve tried several things. I started with this:
class CellHandler: NoStartupOnFail = False LastATRequest = '' LastATResponse = '' GPSTimeout = 30 def __init__(self, power_pin, serial_device, logger): self.powerpin = power_pin self.serial_device = serial_device self.logger = logger GPIO.setmode(GPIO.BCM)
and that doesn’t work: File “cellHandler.py”, line 24, in init GPIO.setmode(GPIO.BCM) ^
Or this:
class CellHandler: NoStartupOnFail = False LastATRequest = '' LastATResponse = '' GPSTimeout = 30 def __init__(self, power_pin, serial_device, logger): self.powerpin = power_pin self.serial_device = serial_device self.logger = logger PRi.GPIO.setmode(GPIO.BCM) File "cellHandler.py", line 25, in __init__ RPi.GPIO.setmode(GPIO.BCM) ^^^
and while this works, later in the class it doesn’t:
class CellHandler: NoStartupOnFail = False LastATRequest = '' LastATResponse = '' GPSTimeout = 30 def __init__(self, power_pin, serial_device, logger): import RPi.GPIO as GPIO self.powerpin = power_pin self.serial_device = serial_device self.logger = logger GPIO.setmode(GPIO.BCM) def startup(self): self.logger.debug("Initiating the SIM7600X startup process") print("Initiating the SIM7600X startup process") # Configure the GPIO pin self.logger.info('Configuing the RPi pins') self.logger.debug('Setting GPIO Mode') self.logger.debug('Setting warnings to False') GPIO.setwarnings(False) Traceback (most recent call last): File "startup.py", line 37, in <module> sys.exit(main()) ^^^^^^ File "startup.py", line 25, in main CH.startup() File "cellHandler.py", line 78, in startup GPIO.setwarnings(False) ^^^^ NameError: name 'GPIO' is not defined
So, could someone lend me some wisdom on how best to manage this? Because I actually have to import several modules that need to be used in this classfile.
submitted by /u/IamTheGorf
[link] [comments]
r/learnpython I’ve got a class file that I have written that is basically a helper library so I can use it across multiple tools. Generally speaking the Python community seems to recommend that imports are at the top of the script and that the imports should support the requirements of the classfile. However, when doing that I don’t really see it working that way. Python throws errors like modules aren’t imported. So here I have a small script: #!/usr/bin/python import logging import logging.handlers import RPi.GPIO as GPIO import sys import time from cellHandler import CellHandler # Global Variables power_gpio = 4 # This is the GPIO pin from RPi that triggers the SIM to startup # Set up logging my_logger = logging.getLogger(“SantaTracker”) my_logger.setLevel(logging.DEBUG) # Set the logging level here handler = logging.handlers.SysLogHandler(address = ‘/dev/log’) handler.ident = “SantaTracaker: ” my_logger.addHandler(handler) # Psuedo main() def main(): print(“Starting up the cellular module”) try: CH = CellHandler(power_gpio, “/dev/ttyS0”, my_logger) CH.startup() time.sleep(10) print(“Requesting GPS”) bob = CH.get_gps() print(bob) except Exception as e: print(f”Unexpected Error: {e}”) my_logger.error(f”Unexpected Error: {e}”) if __name__==”__main__”: my_logger.info(‘Starting up cellular module’) my_logger.debug(‘Entering main()’) And in the class file I’ve tried several things. I started with this: class CellHandler: NoStartupOnFail = False LastATRequest = ” LastATResponse = ” GPSTimeout = 30 def __init__(self, power_pin, serial_device, logger): self.powerpin = power_pin self.serial_device = serial_device self.logger = logger GPIO.setmode(GPIO.BCM) and that doesn’t work: File “cellHandler.py”, line 24, in init GPIO.setmode(GPIO.BCM) ^ Or this: class CellHandler: NoStartupOnFail = False LastATRequest = ” LastATResponse = ” GPSTimeout = 30 def __init__(self, power_pin, serial_device, logger): self.powerpin = power_pin self.serial_device = serial_device self.logger = logger PRi.GPIO.setmode(GPIO.BCM) File “cellHandler.py”, line 25, in __init__ RPi.GPIO.setmode(GPIO.BCM) ^^^ and while this works, later in the class it doesn’t: class CellHandler: NoStartupOnFail = False LastATRequest = ” LastATResponse = ” GPSTimeout = 30 def __init__(self, power_pin, serial_device, logger): import RPi.GPIO as GPIO self.powerpin = power_pin self.serial_device = serial_device self.logger = logger GPIO.setmode(GPIO.BCM) def startup(self): self.logger.debug(“Initiating the SIM7600X startup process”) print(“Initiating the SIM7600X startup process”) # Configure the GPIO pin self.logger.info(‘Configuing the RPi pins’) self.logger.debug(‘Setting GPIO Mode’) self.logger.debug(‘Setting warnings to False’) GPIO.setwarnings(False) Traceback (most recent call last): File “startup.py”, line 37, in <module> sys.exit(main()) ^^^^^^ File “startup.py”, line 25, in main CH.startup() File “cellHandler.py”, line 78, in startup GPIO.setwarnings(False) ^^^^ NameError: name ‘GPIO’ is not defined So, could someone lend me some wisdom on how best to manage this? Because I actually have to import several modules that need to be used in this classfile. submitted by /u/IamTheGorf [link] [comments]
I’ve got a class file that I have written that is basically a helper library so I can use it across multiple tools. Generally speaking the Python community seems to recommend that imports are at the top of the script and that the imports should support the requirements of the classfile. However, when doing that I don’t really see it working that way. Python throws errors like modules aren’t imported. So here I have a small script:
#!/usr/bin/python import logging import logging.handlers import RPi.GPIO as GPIO import sys import time from cellHandler import CellHandler # Global Variables power_gpio = 4 # This is the GPIO pin from RPi that triggers the SIM to startup # Set up logging my_logger = logging.getLogger("SantaTracker") my_logger.setLevel(logging.DEBUG) # Set the logging level here handler = logging.handlers.SysLogHandler(address = '/dev/log') handler.ident = "SantaTracaker: " my_logger.addHandler(handler) # Psuedo main() def main(): print("Starting up the cellular module") try: CH = CellHandler(power_gpio, "/dev/ttyS0", my_logger) CH.startup() time.sleep(10) print("Requesting GPS") bob = CH.get_gps() print(bob) except Exception as e: print(f"Unexpected Error: {e}") my_logger.error(f"Unexpected Error: {e}") if __name__=="__main__": my_logger.info('Starting up cellular module') my_logger.debug('Entering main()')
And in the class file I’ve tried several things. I started with this:
class CellHandler: NoStartupOnFail = False LastATRequest = '' LastATResponse = '' GPSTimeout = 30 def __init__(self, power_pin, serial_device, logger): self.powerpin = power_pin self.serial_device = serial_device self.logger = logger GPIO.setmode(GPIO.BCM)
and that doesn’t work: File “cellHandler.py”, line 24, in init GPIO.setmode(GPIO.BCM) ^
Or this:
class CellHandler: NoStartupOnFail = False LastATRequest = '' LastATResponse = '' GPSTimeout = 30 def __init__(self, power_pin, serial_device, logger): self.powerpin = power_pin self.serial_device = serial_device self.logger = logger PRi.GPIO.setmode(GPIO.BCM) File "cellHandler.py", line 25, in __init__ RPi.GPIO.setmode(GPIO.BCM) ^^^
and while this works, later in the class it doesn’t:
class CellHandler: NoStartupOnFail = False LastATRequest = '' LastATResponse = '' GPSTimeout = 30 def __init__(self, power_pin, serial_device, logger): import RPi.GPIO as GPIO self.powerpin = power_pin self.serial_device = serial_device self.logger = logger GPIO.setmode(GPIO.BCM) def startup(self): self.logger.debug("Initiating the SIM7600X startup process") print("Initiating the SIM7600X startup process") # Configure the GPIO pin self.logger.info('Configuing the RPi pins') self.logger.debug('Setting GPIO Mode') self.logger.debug('Setting warnings to False') GPIO.setwarnings(False) Traceback (most recent call last): File "startup.py", line 37, in <module> sys.exit(main()) ^^^^^^ File "startup.py", line 25, in main CH.startup() File "cellHandler.py", line 78, in startup GPIO.setwarnings(False) ^^^^ NameError: name 'GPIO' is not defined
So, could someone lend me some wisdom on how best to manage this? Because I actually have to import several modules that need to be used in this classfile.
submitted by /u/IamTheGorf
[link] [comments]