AlexW
AlexW

Reputation: 2591

python adding a class object to a list

Im trying to create an empty instance of my class object and add it to a list, however, as soon as i try to create the object add new data i get an error for it as seen below:

error

Traceback (most recent call last):
  File "pagerduty.py", line 96, in <module>
    UserData = User()
TypeError: 'dict' object is not callable

code

class User(object):
    __attrs = ['Policy','Level', 'StartDate', 'EndDate', 'StartTime',
               'EndTime', 'Name', 'Mobile']

    def __init__(self, **kwargs):
        for attr in self.__attrs:
            setattr(self, attr, kwargs.get(attr, None))

    def __repr__(self):
        return ', '.join(
            ['%s: %r' % (attr, getattr(self, attr)) for attr in self.__attrs])  

OnCallData = [] 
for User in objPolicyData['users']: 
    UserData = User()     
    UserData.Name = User['name']
    UserData.Mobile = UserMobile = getUserMobile(User['id']) 
    for OnCall in User['on_call']:    
        UserPolicy = OnCall['escalation_policy'] 
        PolicyName = UserPolicy['name']
        if PolicyName.lower().find('test') == -1:
            UserData.Policy = PolicyName
            UserData.Level = OnCall['level']
            UserData.StartDate = getDate(OnCall['start'])
            UserData.EndDate = getDate(OnCall['end'])
            UserData.StartTime = getTime(OnCall['start'])
            UserData.EndTime = getTime(OnCall['end'])    
OnCallData.append(UserData)

Upvotes: 0

Views: 67

Answers (2)

syntonym
syntonym

Reputation: 7384

Both your class and the variable you are iterating over is called User. Change one of them to a different name.

By convention in python variable names are lowercase. So I suggest the following change:

for user in objPolicyData['users']: 
    user_data = User()     
    user_data.name = User['name']
    user_data.mobile = UserMobile = getUserMobile(User['id']) 
    for on_call in User['on_call']:    
        user_policy = on_call['escalation_policy'] 
        policy_name = user_policy['name']
        if policy_name.lower().find('test') == -1:
            user_data.policy = policy_name
            user_data.level = on_call['level']
            user_data.start_date = get_date(on_call['start'])

and so on. This follows the naming conventions in pep8. This will also take care of your error.

Of course if there is already an established style guide you should follow it and decide for a different name of User (the iteration variable).

Upvotes: 2

Elisha
Elisha

Reputation: 4951

in your for scope, the User identifier is the iterated value from objPolicyData['users'] (as you used it in UserData.Name = User['name'])

you need to use a diffierent name for the iteration.
something like that:

for userI in objPolicyData['users']: 
    UserData = User()     
    UserData.Name = userI['name']
    UserData.Mobile = UserMobile = getUserMobile(userI['id']) 
    for OnCall in userI['on_call']:    
        UserPolicy = OnCall['escalation_policy'] 
        PolicyName = UserPolicy['name']
        if PolicyName.lower().find('test') == -1:
            UserData.Policy = PolicyName
            UserData.Level = OnCall['level']
            UserData.StartDate = getDate(OnCall['start'])
            UserData.EndDate = getDate(OnCall['end'])
            UserData.StartTime = getTime(OnCall['start'])
            UserData.EndTime = getTime(OnCall['end'])  

Note that using the conventions could prevent this bug for you. meaning, starting the name of a variable with lower case letter (user) and a class with capital letter (User).
this is relevant for most of your variables names

Upvotes: 2

Related Questions