0

Apologies for this newbie question. I'm not sure if I even phrased it correctly.

I have a class inside a function that lists a bunch of variables, and I want to be able to choose which variables are printed and returned at the final function call. However, I clearly don't understand enough about objects to accomplish this, because it raises errors when I try to attempt something.

def gpscall(call):
    #Write GPSinput
    out = ''
    ser.write(com.GPSstatus.encode())
    time.sleep(1)

    #Read output
    while ser.inWaiting() > 0:
        decoded = (ser.read(1).decode)
        out += decoded()

    strlen = len(str(out))
    substr = out[0:strlen-9]

    #GPS? information list
    variables = substr.splitlines()

    #Storing each output in a variable
    class GPS:
        PULSE_SAWTOOTH = [int(s) for s in variables[1] if s.isdigit()]
        TRACKED_SATELLITES = [int(s) for s in variables[2] if s.isdigit()]
        VISIBLE_SATELLITES = [int(s) for s in variables[3] if s.isdigit()]

        LONGITUDE = variables[5]
        longlen = len(LONGITUDE)
        LONGDEG = LONGITUDE[0:longlen-7]
        LONGMIN = LONGITUDE[longlen-7:]

        LATITUDE = variables[6]
        latlen = len(LATITUDE)
        LATDEG = LATITUDE[0:latlen-7]
        LATMIN = LATITUDE[latlen-7:]

        HEIGHT = variables[7]
        KNOTS = variables[8]
        DEGREES = [9]

        GPS_STATUS = variables[10]
        TIMING_MODE = variables[17] 
        FIRMWARE_VERSION = variables[20]

    print (call)
    return (call)

if __name__ == "__main__":

    #Call the functions
    gpscall(gpscall.GPS.LATITUDE)

This raises the error,

Function 'gpscall' has no 'GPS' member.

I don't understand why it cannot see the class, I think I'm using the function parameters incorrectly.

Any help with my poorly written code would be greatly appreciated.

2 Answers2

1

Perhaps something like so is your intention? __init__ will initialize the object, and the self. will "save variables to the object."

class GPS:
    def __init__(self):
        #Write GPSinput
        ser.write(com.GPSstatus.encode())

        #Read output
        out = ''
        while ser.inWaiting() > 0:
            decoded = (ser.read(1).decode)
            out += decoded()

        #GPS information list
        substr = out[0:len(str(out))-9]
        variables = substr.splitlines()

        self.PULSE_SAWTOOTH = [int(s) for s in variables[1] if s.isdigit()]
        self.TRACKED_SATELLITES = [int(s) for s in variables[2] if s.isdigit()]
        self.VISIBLE_SATELLITES = [int(s) for s in variables[3] if s.isdigit()]

        self.LONGITUDE = variables[5]
        self.LONGDEG = LONGITUDE[0:len(LONGITUDE)-7]
        self.LONGMIN = LONGITUDE[len(LONGITUDE)-7:]

        self.LATITUDE = variables[6]
        self.LATDEG = LATITUDE[0:len(LATITUDE)-7]
        self.LATMIN = LATITUDE[len(LATITUDE)-7:]

        self.HEIGHT = variables[7]
        self.KNOTS = variables[8]
        self.DEGREES = variables[9]

        self.GPS_STATUS = variables[10]
        self.TIMING_MODE = variables[17]
        self.FIRMWARE_VERSION = variables[20]

gps = GPS()
print(gps.GPS_STATUS) 
felipe
  • 7,324
  • 2
  • 28
  • 37
  • Thanks for your prompt response. I'm having problems with this solution, I get this exception, ['Exception has occurred: AttributeError 'GPS' object has no attribute 'GPS_STATUS']. It appears the code does not recognize anything inside of the GPS class. This occurs for any variable I try and enter. Any idea what could cause this? – Denver Quin Feb 09 '20 at 08:33
  • Did you write `self.GPS_STATUS` inside `__init__` as opposed to simply just `GPS_STATUS`? – felipe Feb 09 '20 at 16:39
  • I think that was the problem. I did not include the self class. Thank you for the help! – Denver Quin Feb 09 '20 at 23:21
-2

Yor cls inside the function is perfect and there is nothing wrong there. You are just trying to call the function and the cls objects in a wrong way.

if __name__ == "__main__":

    #Call the functions
    gpscall(gpscall.GPS.LATITUDE)  <---- ERROR HERE

gpscall is a function, so when you are trying to access GPS.LATITUDE, it won't find any objects. You would have to do either this

gpscall(gpscall("").GPS.LATITUDE)

But I think the best way to do this is to write the func inside the cls. You will still be able to access all the variables of the cls, and it won't create much hassle.

PS: That's a good question, not a noob one. Good luck (y)

O_o
  • 1,103
  • 11
  • 36
  • I'm afraid `gpscall(gpscall("").GPS.LATITUDE)` would most likely not work. Since `gpscall(call)` does at the end `return call`, you would have `gpscall("".GPS.LATITUDE)`, which would return an `AttributeError`. – felipe Feb 09 '20 at 05:50
  • That was a suggestion. I don't know what `call` is. – O_o Feb 09 '20 at 05:52
  • Input param for `gpscall`: `def gpscall(call):`. I see where the confusion arose. – felipe Feb 09 '20 at 05:52
  • I clearly stated that he would have to call the cls directly, instead of functions. – O_o Feb 09 '20 at 05:53
  • Perhaps so, but the suggested solution is not proper and frankly a bit confusing. Just constructive criticism on the answer -- no hate here. – felipe Feb 09 '20 at 05:54
  • Whatever. It does not matter. @Felipe – O_o Feb 09 '20 at 05:57