McMo
McMo

Reputation: 151

Check functions within class for True

(Python 3.6.3 on Windows 7 64 Bit using eclipse Oxygen)

i am trying to return the values of the function which returns True. You enter the relevant parameters a,v,s,j and depending on the results a specific case takes place. I have tried using callable but since all of them are callable it doesn't work. Using dict.items() has not worked either.

Maybe it is somehow possible to use a for-loop trying iterate through the class looking for True and break the loop. Unfortunately I was not able to set up such a for-loop. I am grateful for any help.

class cases(object):

    def __init__(self, a, v, s, j):
        self.a = a
        self.v = v
        self.s = s
        self.j = j

    def case_one(self, a, v, s, j):
        tj = pow((s/(2*j)),1/3)
        a_max = tj * j
        v_max = j*(tj**2)
        if a >= a_max and v >= v_max:
            x = True
        else:
            x = False        
        return x, tj, a_max, v_max
    pass     

    def case_two(self, a, v, s, j):    
        tj = pow(v/j, 1/2)
        tv = (s/(j*tj**2)-2*tj)
        a_max = tj * j
        if a >= a_max and tv > 0:
            x = True
        else:
            x = False
        return x, tv, tj, a_max  
    pass   

    def case_three(self, a, v, s, j):
        ta = (a/j)*(-3/2+pow((1/4)+(s*j**2/pow(a,3)),1/2))
        tj = a/j
        v_max = j*tj*(ta+tj)
        if v >= v_max and ta > 0:
            x = True
        else:
            x = False
        return x, ta, tj, v_max  
    pass        

    def case_four(self, a, v, s, j):
        tj = a/j
        ta = (v/a)-(a/j)
        tv = (s/v)-(v/a)-(a/j)
        if tv > 0 and ta > 0:
            x = True
        else:
            x = False
        return x, tj, ta, tv
    pass

    def __call__(self):
        self.case_one()
        self.case_two()
        self.case_three()
        self.case_four()


case = cases.case_four(1,14.5,7,0.25,120)
print(case)

P.S.: Apparently I did not fully understand how "self" works within a class since I have to enter a "1" as the very first parameter in order get any results. otherwise i get an error message saying "TypeError: case_four() missing 1 required positional argument: 'j'" but j equals 120.

Upvotes: 0

Views: 44

Answers (1)

Jeronimo
Jeronimo

Reputation: 2387

  1. Make an instance of you class (and rename your class to upper camel case).

=>

cases = Cases(14.5,7,0.25,120)
  1. Remove the arguments from your methods - self already has them since you assgined them to it inside __init__:

=>

 def case_one(self):
        a,v,s,j = self.a, self.v, self.s, self.j   # aliases
        tj = pow((s/(2*j)),1/3)
        a_max = tj * j
        v_max = j*(tj**2)
        x = (a >= a_max and v >= v_max)
        return x, tj, a_max, v_max
  1. To loop over the methods to get the reurn values, do something like this. Note that the instance cases is used, not the class Cases:

=>

for method in [cases.case_one, cases.case_two, cases.case_three, cases.case_four]:
    ret = method()
    if ret[0]:
        print(method.__name__)
        break
else:
    print("no result was True")
  1. And remove the pass at the end of every method...

Upvotes: 1

Related Questions