I am new to decorators and believe I understand their purpose/the general gist of them for the most part. Unfortunately, I am having some issues with this decorator and its usage in a class. I am trying to populate a dictionary _profile_dict = {}
with a new key created by profile + str(<counter value>)
. I'd rather not instantiate the class and set it to a variable in my if __name__ == '__main'
statement so I am trying to execute the class with the create_profiles
function. Here is my code,
_profile_dict = {}
class Profile:
def __init__(self):
self.first_name = input('first name: ')
self.last_name = input('last name: ')
self.email = input('email: ')
self.address1 = input('primary address: ')
self.address2 = input('secondary address: ')
self.city = input('city: ')
self.country = input('country: ')
self.province = input('province: ')
self.zip = input('zip: ')
self.phone = input('phone number:')
self.cc_name = input('Name on credit card: ')
self.cc_num = input('Credit card number: ')
self.exp_month = input('card expiration month: ')
self.exp_year = input('card expiration year: ')
self.cvv = input('cvv: ')
self.json_obj = {"utf8": "✓",
"_method": "",
"authenticity_token": "",
"previous_step": "",
"checkout[email]": self.email,
"checkout[buyer_accepts_marketing]": 1,
"checkout[shipping_address][first_name]": self.first_name,
"checkout[shipping_address][last_name]": self.last_name,
"checkout[shipping_address][address1]": self.address1,
"checkout[shipping_address][address2]": self.address2,
"checkout[shipping_address][city]": self.city,
"checkout[shipping_address][country]": self.country,
"checkout[shipping_address][province]": self.province,
"checkout[shipping_address][zip]": self.zip,
"checkout[shipping_address][phone]": self.phone,
"step": ""}
def counter(self, func):
def wrapper():
wrapper.counter += 1
return func(wrapper.counter)
wrapper.counter = 0
return wrapper
@counter
def _populate_profile_dict(self, count):
profile = 'profile'
_profile_dict[profile + str(count)] = self.json_obj
print(_profile_dict)
def create_profiles():
Profile()._populate_profile_dict()
if __name__ == "__main__":
create_profiles()
At the moment, I am getting the following error,
Traceback (most recent call last):
File "C:/Users/Carsten/Profile_Gen/src/config.py", line 6, in <module>
class Profile:
File "C:/Users/Carsten/Profile_gen/src/config.py", line 49, in Profile
@counter
TypeError: counter() missing 1 required positional argument: 'func'
Although from what I just read over here, Python decorators in classes , I'm assuming this is not possible. Is it possible to accomplish what I am trying to acheive with @classmethod at all?
Just by changing the last 3 functions/methods to this, still leads to no output:
@classmethod
def counter(cls, func):
def wrapper():
wrapper.counter += 1
return func(wrapper.counter)
wrapper.counter = 0
return wrapper
def _populate_profile_dict(self, count):
profile = 'profile'
_profile_dict[profile + str(count)] = self.json_obj
print(_profile_dict)
#def create_profiles():
# Profile()._populate_profile_dict()
if __name__ == "__main__":
Profile()._populate_profile_dict =
Profile.counter(Profile._populate_profile_dict)
Any help would be appreciated. Thank you for your time. I feel like decorators as well as a few other things that I have on my agenda will definitely help propel me into more of an intermediate stage of python.