Iteration over dictionary - Python

I'm having trouble with list of dictionaries. There is a function that saves a list(like a shopping list). When the user create and save one of these shopping lists the save function wll also call a classmethod to increment one of the values of my dictionary(like an history). This is what the save function calls:

d = date.today()
y = d.year
m = d.month
Establishment.check_year_existence(y, m)

Now, about the classmethod. The list of dictionaries will save dictionaries based on the year that will be passed throught the classmethod call seen before. If there is a dictionary registered for that year, the increment would be based only by the month.

For example, we are in 2015, the method receive (2015, 2) year and month, if there is already a dictionary registered in 2015, the method won't create another one, it will just increment the value of the month.

{'Year': 2015, 'Jan': 5, 'Feb': 0, ...}
+ 1 (increment)
{'Year': 2015, 'Jan': 5, 'Feb': 1, ...}

This is the classmethod:

global info_estab
info_estab = []

@classmethod
def check_year_existence(cls, y, m):

    #for i, index in enumerate(info_estab):

    if info_estab.key('Year').index(y):
        ?
    else:

        new_info = {'Year': date.today().year, 'Jan': 0, 'Feb': 0, 'Mar': 0, 'Apr': 0, 
'Mai': 0, 'Jun': 0, 'Jul': 0, 'Aug': 0, 'Sep': 0, 'Oct': 0, 'Nov': 0, 'Dec': 0}
        new_info.key(mes).index = 1
        info_estab.append(new_info)

Even though I have tried to find the answer and read about it to try to solve my problem, I still don't know how to do this verification, since I'm not a 'python pro'.

How could I do this verification?

Answers


You can iterate over your entries in your info_estab dict and check for the year value.

for entry in info_estab:
    if entry.get('Year') == y:
        entry[m] += 1
    else:
        new_info = {'Year': y, 'Jan': 0, 'Feb': 0, 'Mar': 0, 'Apr': 0, 'Mai': 0, 'Jun': 0, 'Jul': 0, 'Aug': 0, 'Sep': 0, 'Oct': 0, 'Nov': 0, 'Dec': 0}
        new_info[m] = 1
        info_estab.append(new_info)

Just a thought to make this more performant (depending on how many years you are going to be inserting). You could make info_estab a dictionary itself, with the year as the key. You also wouldn't need one method for new entries, and one for incrementing. It can all be done in one compact method.

global info_estab
info_estab = {}

@classmethod
def increment_month(cls, y, m):

    if y not in info_estab:
        new_info = {'Jan': 0, 'Feb': 0, 'Mar': 0, 
                    'Apr': 0, 'Mai': 0, 'Jun': 0, 'Jul': 0, 'Aug': 0, 
                    'Sep': 0, 'Oct': 0, 'Nov': 0, 'Dec': 0}
        info_estab[y] = new_info              
    info_estab[y][m] += 1

The only other thing I would suggest is try to avoid using global variables. It can make code much harder to understand. But without further context, I am not sure the best way to encapsulate the variable in your class (possibly a class variable since this is a class method?).


Need Your Help

SQL Select when data changes

c# sql entity-framework

I'd like to retrieve an integer value in a SQL Select but only when the data changes, e.g.:

Equalizing horizontal column paddings at unknown number of columns

css css3

I am struggling solving a problem with a multi-column setup.