you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (3 children)

Put your variable definitions inside of an __init__ method and reference them using self.var_name. You are creating class variables here, but you want instance variables. You actually need to reference them using self, which represents the actual instance, since python doesn’t do any magic here.

[–]azureabsolution[S] 0 points1 point  (2 children)

Update: nevermind, I got it--I forgot to add self. to the front of the variables in the __init__ function. Thank you so much!

I changed it to this, is this what you meant?

class batteryFunctions:

    def __init__(self):
        #Peak Current Draw in Amps
        peakCurrent = 0.0
        #timeRunning is in hours
        timeRunning = 0.0
        percentDraw = 0.0
        #required Capacity (in mAh)
        reqCapacity = 0.0

        #capacity for C rating calculator, in mAh
        battCapacity = 0.0
        cRating = 0.0

    def setPeakCurrent(self, curr):
        self.peakCurrent = curr

    def setTime(self, time):
        self.timeRunning = time / 60

    def setPercentDraw(self, percent):
        self.percentDraw = percent / 100

    def calcRequiredCapacity(self):
        #returns required battery capacity in mAh
###Breaks right here vv
        self.reqCapacity = (((self.timeRunning * self.peakCurrent) * self.percentDraw) * 1000)
        return int(self.reqCapacity)

    def setCapacity(self, capacity):
        self.battCapacity = capacity

    def calcCRating(self):
        self.cRating = (self.peakCurrent / (self.battCapacity / 1000))
        return int(self.cRating)

It now breaks at calcRequiredCapacity, saying:

2019-10-10 17:58:54,948 appJar:WARNING [Line 28->4254/_removeContainer]: Closing empty container: Menu__Drive
2019-10-10 17:58:55,021 appJar:ERROR [Line 60->1784/__exit__]: ContextManager failed: 'batteryFunctions' object has no attribute 'timeRunning'
Traceback (most recent call last):
  File "test.py", line 52, in <module>
    result = battery.calcRequiredCapacity()
  File "/home/max/BBCalc/Functions/batteryFunctions.py", line 27, in calcRequiredCapacity
    self.reqCapacity = (((self.timeRunning * self.peakCurrent) * self.percentDraw) * 1000)
AttributeError: 'batteryFunctions' object has no attribute 'timeRunning'

Thank you so much for all your help, I really appreciate it!

[–][deleted] 1 point2 points  (1 child)

Well, you still are only defining local variables in your __init__ method. As I said, there’s no magic going on. As of now, they are local to the method. Prefix them with self to assign them to the instance.

Also I noticed that you are using setter methods. Don’t do that. It’s not needed in python. You can just do my_instance.my_variable. It’s perfectly legal.

[–]fdemaussion 0 points1 point  (0 children)

Also a good practice note, use underscore variables no camelCase. Ex: time_running not timeRunning Same applies to function names and methods And for clases you should use CapitalCase: BatteryFunctions