2017-05-11 5 views
0

私は、pythonでデコレータを使用して複数の集約関数を実行しています。文がすべての条件に対して1つの出力しか生成しない場合

これらのifの条件は、異なる値の1つの条件に対してのみ出力を生成します。

私は位置を変更しようとしましたが、常に同じ出力を生成しています。

私は間違っていますか?

はどうすれば条件での私の要件に従って値が異なることができます

サンプルコード:

# This class allows function addition, multiplication, division 
      etc. 
class operable: 
    def __init__(self, f): 
     self.f = f 
    def __call__(self, x): 
     return self.f(x) 

def op_to_function_op(op): 
    def function_op(self, operand): 
     def f(x): 
      return op(self(x), operand(x)) 
     return operable(f) 
    return function_op 

for name, op in [(name, getattr(operator, name)) for name in 
    dir(operator) if "__" in name]: 
    try: 
     op(1,2) 
    except TypeError: 
     pass 
    else: 
     setattr(operable, name, op_to_function_op(op)) 

def allocate_service(ModelAdmin, request, queryset): 
    platinum_customers = [] 
    silver_customers = [] 
    gold_customers = [] 
    non_customers = [] 
    message = '' 


    for customer in queryset: 



     @operable 
     def getAge(): 
      age = 0 

      if customer.Age == Customer.objects.get(Age = "60 +"): 
       age = 0 

      elif customer.Age == Customer.objects.get(Age = "36 - 59"): 
       age = 1 
      else: 
       age = 2 


     def getEducation(): 
      education = 0 
      if customer.Education == Customer.objects.get(Education = 
       "Highschool and below"): 
       education = 0 
      else: 
       education = 1 


     def getEmployment(): 
      employment = 0 
      if customer.Employment == Customer.objects.get(Employment = 
       "Student"): 
       employment = 0 
      elif customer.Employment == Customer.objects.get(Employment = 
       "Contract"): 
       employment = 1 
      else: 
       employment = 2 


     def getStability(): 
      stability = 0 
      if customer.Employer_Stability == 
       Customer.objects.get(Employer_Stability = "Unstable"): 
       stability = 0 
      else: 
       stability = 1 


     def getResidential(): 
      residential = 0 
      if customer.Residential_Status == 
       Customer.objects.get(Residential_Status = "Rented"): 
       residential = 0 
      else: 
       residential = 1 


     def getSalary(): 
      salary = 0 
      if customer.Salary == Customer.objects.get(Salary <= 1000): 
       salary = 0 
      elif customer.Salary == Customer.objects.get(Salary <= 10001 and 
       Salary > 1000): 
       salary = 1 
      else: 
       salary = 2 


     def getLoyalty(): 
      loyalty = 0 
      loy = Customer.objects.get(Customer_Loyalty <= 2) 

      if customer.Customer_Loyalty == loy.Customer_Loyalty: 
       loyalty = 0 
      else: 
       loyalty = 1 


     def getBalance(): 
      balance = 0 
      if customer.Balance == Customer.objects.get(Balance <= 2500): 
       balance = 0 
      elif customer.Balance == Customer.objects.get(Balance <= 10001 
       and Balance > 2500): 
       balance = 1 
      else: 
       balance = 2 



     def feat_list(): 
      total = getAge + getEducation + getEmployment + getStability + 
       getResidential + getSalary + getLoyalty + getBalance 
      return total 



     if feat_list() <= 11: 
      customer.Service_Level = Service.objects.get(service_name = 
       'Silver Package') 
      silver_customers.append(customer.Name) 

     elif 11 < feat_list() <= 15: 
      customer.Service_Level = Service.objects.get(service_name = 
       'Gold Package') 
      gold_customers.append(customer.Name) 
     elif feat_list() > 15: 
      customer.Service_Level = Service.objects.get(service_name = 
       "Platinum Package") 
      platinum_customers.append(customer.Name) 
     else: 
      customer.Service_Level = Service.objects.get(service_name = "No 
       Service Package") 
      non_customers.append(customer.name) 

     customer.save() 

     if platinum_customers: 
      message = 'The following customers are now Platinum Customers: 
       {}'.format(', '.join(platinum_customers)) 
     if silver_customers: 
      message = 'The following customers are now Silver Customers: 
       {}'.format(', '.join(silver_customers)) 
     if gold_customers: 
      message = 'The following customers are now Gold Customers: 
       {}'.format(', '.join(gold_customers)) 
     if not platinum_customers and not silver_customers and not   
       gold_customers: 
      message = 'No customer changes made!' 
     ModelAdmin.message_user(request, message, level=SUCCESS) 
    allocate_service.short_description = 'Allocate Service' 

私はService Levelの値を決定するために、このコードのセクションを実行したいと思います:

if feat_list() <= 11: 
     customer.Service_Level = Service.objects.get(service_name = 'Silver Package') 
     silver_customers.append(customer.Name) 

    elif 11 < feat_list() <= 15: 
     customer.Service_Level = Service.objects.get(service_name = 'Gold Package') 
     gold_customers.append(customer.Name) 
    elif feat_list() > 15: 
     customer.Service_Level = Service.objects.get(service_name = "Platinum Package") 
     platinum_customers.append(customer.Name) 
    else: 
     customer.Service_Level = Service.objects.get(service_name = "No Service Package") 
     non_customers.append(customer.name) 

    customer.save() 

問題はそれも設定され、すべてのクエリにSilver PackagesService Levelを与えているということです私が習慣を持っていたら、Platinum or Gold Packagesを得るためにいくつか編集しました。私はこのブロックで何が正確に間違っているのか分かりません。それは望ましくない出力を提供していますが、実行中です

+0

あなたのコードは 'if'sの多くが含まれています。私たちはあなたが問題を抱えている特定のものが何であるか分かりません。質問を編集して、コードを__running__する例と、問題を示す出力を含めてみてください。 –

+0

私はそれをやりましょう – maffsojah

+0

また、親関数などに内部関数が定義されているかどうかは不明です。インデントには別の見た目が必要です – JacobIRR

答えて

0

私は、各機能にデコレータを使用する必要があります。

  1. はあなたが
  2. あなたはリターン各サブ操作の結果に必要な評価されている各機能にデコレータを追加する必要があります(あなたがfeat_listでそれを合計することができます())

となっています。そのため、11以下のスコアを受け取ったため、最終的にsilver_customersになりました。結果を追跡することは容易であるので、 合計変数を印刷することが重要である:

# This class allows function addition, multiplication, division 
      etc. 
class operable: 
    def __init__(self, f): 
     self.f = f 
    def __call__(self, x): 
     return self.f(x) 

def op_to_function_op(op): 
    def function_op(self, operand): 
     def f(x): 
      return op(self(x), operand(x)) 
     return operable(f) 
    return function_op 

for name, op in [(name, getattr(operator, name)) for name in 
    dir(operator) if "__" in name]: 
    try: 
     op(1,2) 
    except TypeError: 
     pass 
    else: 
     setattr(operable, name, op_to_function_op(op)) 

def allocate_service(ModelAdmin, request, queryset): 
    platinum_customers = [] 
    silver_customers = [] 
    gold_customers = [] 
    non_customers = [] 
    message = '' 


    for customer in queryset: 

     @operable 
     def getAge(): 
      age = 0 

      if customer.Age == Customer.objects.get(Age = "60 +"): 
       age = 0 

      elif customer.Age == Customer.objects.get(Age = "36 - 59"): 
       age = 1 
      else: 
       age = 2 
      return age 

     @operable 
     def getEducation(): 
      education = 0 
      if customer.Education == Customer.objects.get(Education = 
       "Highschool and below"): 
       education = 0 
      else: 
       education = 1 
      return education 
     @operable 
     def getEmployment(): 
      employment = 0 
      if customer.Employment == Customer.objects.get(Employment = 
       "Student"): 
       employment = 0 
      elif customer.Employment == Customer.objects.get(Employment = 
       "Contract"): 
       employment = 1 
      else: 
       employment = 2 
      return employment 

     @operable 
     def getStability(): 
      stability = 0 
      if customer.Employer_Stability == 
       Customer.objects.get(Employer_Stability = "Unstable"): 
       stability = 0 
      else: 
       stability = 1 
      return stability 

     @operable 
     def getResidential(): 
      residential = 0 
      if customer.Residential_Status == 
       Customer.objects.get(Residential_Status = "Rented"): 
       residential = 0 
      else: 
       residential = 1 
      return residential 

     @operable 
     def getSalary(): 
      salary = 0 
      if customer.Salary == Customer.objects.get(Salary <= 1000): 
       salary = 0 
      elif customer.Salary == Customer.objects.get(Salary <= 10001 and 
       Salary > 1000): 
       salary = 1 
      else: 
       salary = 2 
      return salary 
     @operable 
     def getLoyalty(): 
      loyalty = 0 
      loy = Customer.objects.get(Customer_Loyalty <= 2) 

      if customer.Customer_Loyalty == loy.Customer_Loyalty: 
       loyalty = 0 
      else: 
       loyalty = 1 
      return loyalty 
     @operable 
     def getBalance(): 
      balance = 0 
      if customer.Balance == Customer.objects.get(Balance <= 2500): 
       balance = 0 
      elif customer.Balance == Customer.objects.get(Balance <= 10001 
       and Balance > 2500): 
       balance = 1 
      else: 
       balance = 2 
      return balance 


     def feat_list(): 
      total = getAge + getEducation + getEmployment + getStability + 
       getResidential + getSalary + getLoyalty + getBalance 
      print('total:' + total) 
      return total 



     if feat_list() <= 11: 
      customer.Service_Level = Service.objects.get(service_name = 
       'Silver Package') 
      silver_customers.append(customer.Name) 

     elif 11 < feat_list() <= 15: 
      customer.Service_Level = Service.objects.get(service_name = 
       'Gold Package') 
      gold_customers.append(customer.Name) 
     elif feat_list() > 15: 
      customer.Service_Level = Service.objects.get(service_name = 
       "Platinum Package") 
      platinum_customers.append(customer.Name) 
     else: 
      customer.Service_Level = Service.objects.get(service_name = "No 
       Service Package") 
      non_customers.append(customer.name) 

     customer.save() 

     if platinum_customers: 
      message = 'The following customers are now Platinum Customers: 
       {}'.format(', '.join(platinum_customers)) 
     if silver_customers: 
      message = 'The following customers are now Silver Customers: 
       {}'.format(', '.join(silver_customers)) 
     if gold_customers: 
      message = 'The following customers are now Gold Customers: 
       {}'.format(', '.join(gold_customers)) 
     if not platinum_customers and not silver_customers and not   
       gold_customers: 
      message = 'No customer changes made!' 
     ModelAdmin.message_user(request, message, level=SUCCESS) 
    allocate_service.short_description = 'Allocate Service' 
+0

を変換する方法はありますか?それは同じ出力を生成するまでです – maffsojah

関連する問題