I have to make a function whose purpose is taking in parameter a list.
Such as this one :
['music', ' extension=mp3', 'reports/INFOB131', ' extension=doc,docx,pdf', ' name_contains=INFOB131', ' max_size=100000', 'reports/INFOB132', ' extension=doc,docx,pdf', ' name_contains=INFOB132', ' max_size=100000', 'games', ' name_contains=SC2,Wesnoth', 'pictures/Namur', ' extension=jpeg', ' min_size=5000000', ' name_contains=cercle', 'pictures/autres', ' extension=jpeg', ' min_size=5000000']
And return a list similar to this :
data_config = [{'music' : {'extension':'mp3'}}, {'reports/INFOB131': {'extension': ['doc', 'docx','pdf'], 'name_contains':'INFOB131', 'max_size':100000}}, {'reports/INFOB132': {'extension': ['doc', 'docx','pdf'], 'name_contains':'INFOB132', 'max_size':100000}}]
So I made that function :
def my_function(list_in_question, my_config_list =[], prev_list = []):
""" """
enumerated_list = list(enumerate(list_in_question))
if not '=' in enumerated_list[0][1]:
main_key = enumerated_list[0][1]# référencé avant assignement
pre_dict = {main_key : {}}
for i in enumerated_list[1:]:
if '=' in i[1] :
splitted = i[1].split('=')
prev_list.append({splitted[0] : splitted[1]})
elif not '=' in i[1] and i[1] != main_key:
for j in prev_list:
pre_dict[main_key].update(j)
my_config_list.append(pre_dict)
return my_function(list_in_question[i[0]:])
elif not '=' in i[1] and i[1] == main_key and main_key!= enumerated_list[0][1]:
return my_config_list
else:
print("The format of the file containig the data in not adequate !")
But I don't understand why when I execute it this way :
new_lines = ['music', ' extension=mp3', '', 'reports/INFOB131', ' extension=doc,docx,pdf', ' name_contains=INFOB131', ' max_size=100000', '', 'reports/INFOB132', ' extension=doc,docx,pdf', ' name_contains=INFOB132', ' max_size=100000', '', 'games', ' name_contains=SC2,Wesnoth', '', 'pictures/Namur', ' extension=jpeg', ' min_size=5000000', ' name_contains=cercle', '', 'pictures/autres', ' extension=jpeg', ' min_size=5000000']
my_function(new_lines)
I end up with this output...
None
I would be very grateful if someone could help me,
Thank you !
PS : If anyone have an idea of how I could do without loop and do it in a recursive way, it would be awesome !
Everyone... Thank you !!! You really hepled me, all your answers are awesome, I have some issues to understand some parts so I'll be annoying just a little longer with some questions of you code. Anyway, thank you for the time you took to help me, you were all more than great help !!!