0

Trying to build code-list independent from main file

#### expense_codelist.py #### def thelist(filename_list): fn_type = filename_list.split('-') pay_method_var = fn_type[1] if fn_type[0] == 'Chase': pay_method_val = 'Credit Card' card_name == 'Chase' proceed = 'Y' if fn_type[0] == 'Expenses': pay_method_val = 'Debit Card' card_name = 'Bancontact' proceed = 'Y' return if fn_type[0] == 'ING Credit Card': pay_method_val = 'Credit Card' card_name = 'ING' proceed = 'Y' if fn_type[0] == 'Capital One': pay_method_val = 'Credit Card' card_name = 'Capital One' proceed = 'Y' 

I want to use the variables pay_method_val, card_name, proceed to be returned to the main script. What I tried is below:

#### process.py #### import expense_codelist print expense_codelist.thelist(incsvfn)proceed 

I am getting the below Error:

print expense_codelist.thelist(incsvfn).proceed AttributeError: 'NoneType' object has no attribute 'proceed' 
2
  • 2
    You probably need to return those values in your function then Commented Feb 12, 2018 at 10:54
  • You should also use elif for the other conditions to make the code more efficient. Commented Feb 12, 2018 at 10:58

2 Answers 2

1

You could use the collections module and put

TheList = collections.namedtuple('TheList', 'pay_method_val, card_name, proceed') return TheList(pay_method_val, card_name, proceed) 

a the end of your function, but this feels like a bit of a hack.

What you probably want is a class which stores the data, and would be written like this:

class TheList(object): def __init__(self, filename_list): fn_type = filename_list.split('-') self.pay_method_var = fn_type[1] if fn_type[0] == 'Chase': self.pay_method_val = 'Credit Card' self.card_name == 'Chase' self.proceed = 'Y' elif fn_type[0] == 'Expenses': self.pay_method_val = 'Debit Card' self.card_name = 'Bancontact' self.proceed = 'Y' elif fn_type[0] == 'ING Credit Card': self.pay_method_val = 'Credit Card' self.card_name = 'ING' self.proceed = 'Y' elif fn_type[0] == 'Capital One': self.pay_method_val = 'Credit Card' self.card_name = 'Capital One' self.proceed = 'Y' 

which would be called like so:

import expense_codelist thelist = expense_codelist.TheList(incsvfn) print thelist.proceed 
Sign up to request clarification or add additional context in comments.

Comments

0

If you just want the values of those variables (and not an object containing them), you can simply return them:

#### expense_codelist.py #### def thelist(filename_list): fn_type = filename_list.split('-') pay_method_var = fn_type[1] if fn_type[0] == 'Chase': pay_method_val = 'Credit Card' card_name == 'Chase' proceed = 'Y' elif fn_type[0] == 'Expenses': pay_method_val = 'Debit Card' card_name = 'Bancontact' proceed = 'Y' return elif fn_type[0] == 'ING Credit Card': pay_method_val = 'Credit Card' card_name = 'ING' proceed = 'Y' elif fn_type[0] == 'Capital One': pay_method_val = 'Credit Card' card_name = 'Capital One' proceed = 'Y' else: raise ValueError("Card type not recognized") return pay_method_var, pay_method_val, card_name, proceed 

And in your other file just save them to local variables:

#### process.py #### import expense_codelist pay_method_var, pay_method_val, card_name, proceed = expense_codelist.thelist(incsvfn) print proceed 

I also made your if chain an if..elif..else chain. Your strings are mutually exclusive (and there should be code handling the case where the card type is something unexpected).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.