'Converting static code to dynamic - Python

I have the following class:

class Battery:
    capacity = 4000 #Battery capacity in Wh
    cSOC = 0        #current SOC
    discharge_rate = 0.00003   # 5% in 24hrs -> 0.003% self-discharge rate per min (standard)
    counter = 0

    def __init__(self, battery_num, SOC, cap,timestamp):
      self.battery_num = battery_num
      self.cSOC = SOC
      self.capacity=cap         #Allows to change capacity on real-time
      self.timestamp = timestamp
      batt_time_index = df_batt.index[df_batt['index'] == timestamp].tolist()[0]
      df_batt.at[batt_time_index, battery_num] = SOC
      Battery.counter+=1

    def charge(self, t, E):                                             # t is the time (sample) in which the charge occurs, E is the energy in Wh that is being charged
      E_stored=self.capacity*self.cSOC/100                              # The energy stored (E_stored) in the battery is capacity*SOC/100
      index=df_temp[df_temp['timestamp'] == t].index.values
      E_new_stored=E_stored + E.loc[index,'Output_Energy']              #adding solar energy to initial energy
      E_new_SOC=float(100*E_new_stored/self.capacity)                   #The new capacity will be:  #converting to percentage                                        
      self.cSOC=E_new_SOC                                               #storing the new SOC in cSOC          
      return E_new_SOC
    
    def discharge(self, t):
      E_stored = self.capacity*self.cSOC/100
    # The new energy stored after discharging
      E_new_stored = E_stored-(E_stored*self.discharge_rate) 
    # The new capacity will be
      E_new_SOC = 100*E_new_stored/self.capacity
      self.cSOC = E_new_SOC
      return E_new_SOC

I am creating the instances manually like shown below:

t1= "2016-06-20 09:00:00+05:30"
t2= "2016-06-20 09:00:00+05:30"
t3= "2016-06-20 09:00:00+05:30"

Battery1 = Battery('one', 25, 3200, t1)
Battery2 = Battery('two', 30, 3200, t2)
Battery3 = Battery('three', 40, 3200, t3)

df_temp=df_batt.rename(columns={'index':'timestamp'})
df_temp.fillna(0, inplace=True)

When I create a new instance, a new column is added to the data frame with SOC values at the respective timestamp. The data frame (it's just a few rows) looks like this:

index   timestamp                 Output_Energy         Elevation       one two three
.
.
538 2016-06-20 08:58:00+05:30   40.34829924338887   44.04129964199598   0.0 0.0 0.0
539 2016-06-20 08:59:00+05:30   40.5703298574816    44.25962644894399   0.0 0.0 0.0
540 2016-06-20 09:00:00+05:30   40.79141282764114   44.47799109475774  25.0 30.0 40.0
541 2016-06-20 09:01:00+05:30   41.01157539726741   44.69639316356193   0.0 0.0 0.0
542 2016-06-20 09:02:00+05:30   41.230790026853384  44.91483208973582   0.0 0.0 0.0
543 2016-06-20 09:03:00+05:30   41.44905311289469   45.13330730580419   0.0 0.0 0.0
544 2016-06-20 09:04:00+05:30   41.666364098311895  45.351818241921414  0.0 0.0 0.0
545 2016-06-20 09:05:00+05:30   41.8827166483967    45.57036432534446   0.0 0.0 0.0
546 2016-06-20 09:06:00+05:30   42.0981013074145    45.78894498194398   0.0 0.0 0.0
547 2016-06-20 09:07:00+05:30   42.31250713341641   46.007559633636575  0.0 0.0 0.0
548 2016-06-20 09:08:00+05:30   42.525960667204465  46.22620769987313   0.0 0.0 0.0
549 2016-06-20 09:09:00+05:30   42.738418433471544  46.44488859711554   0.0 0.0 0.0
550 2016-06-20 09:10:00+05:30   42.94990329039521   46.66360188496949   0.0 0.0 0.0
551 2016-06-20 09:11:00+05:30   43.160390522060574  46.88234668098421   0.0 0.0 0.0
552 2016-06-20 09:12:00+05:30   43.36988302062059   47.101122538059016  0.0 0.0 0.0
553 2016-06-20 09:13:00+05:30   43.57837124543777   47.319928859306344  0.0 0.0 0.0
554 2016-06-20 09:14:00+05:30   43.785848829859404  47.53876504413286   0.0 0.0 0.0
555 2016-06-20 09:15:00+05:30   43.992319514156094  47.75763048766155   0.0 0.0 0.0
556 2016-06-20 09:16:00+05:30   44.19779793479642   47.976524582260204  0.0 0.0 0.0
557 2016-06-20 09:17:00+05:30   44.402250365403276  48.19544671486692   0.0 0.0 0.0
558 2016-06-20 09:18:00+05:30   44.6056725858456    48.4143962684937    0.0 0.0 0.0
559 2016-06-20 09:19:00+05:30   44.80807132168398   48.63337262163986   0.0 0.0 0.0
560 2016-06-20 09:20:00+05:30   45.00943825682395   48.852375147711754  0.0 0.0 0.0
.
.

I want to achieve the following:

  1. I want to select the maximum value among the three columns one, two and three and apply charge method on that value (in this case column three) till it reaches at least 50 while at the same time I want to apply discharge method on the other two columns i.e., one and two. I want to keep repeating the process till all columns have a value of at least 50.
  2. When the above-mentioned task is accomplished I want to continue doing the same thing but now changing the value from 50 to 90.

Is working with the data frame a wise choice or working internally with a list/ dictionary better and keep updating the data frame a better thing to do??

I have achieved the above objectives with static programming (if-else conditions) but I want to make my code dynamic, independent of the number of instances I create, and prepare the method(s) calling to match any number of target columns.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source