Bonjour,

Tout d'abord, je tiens à préciser que je travail sur du python 2.7 (Pas par libre choix, rassurez vous).

Mon problème est le suivant, j'ai besoin de générer une instance d'objet avec un mix d'attributs défini dans d'autres classes et des attributs à passer directement à la création de mon objet.
La partie ou je bloque est la récupération des attributs depuis les autres classes de manière élégante sans avoir à faire une forêt de if qui va très vite devenir illisible.
Ce n'est peut-être pas très clair donc voici un exemple :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 
class ComponentCommand1(object):
    mk_cmd = 'mkcomponent'
    rm_cmd = 'rmcomponent'
 
class ComponentCommand2(object):
    mk_cmd = 'mkothercomponent'
    rm_cmd = 'rmothercomponent'
 
class ComponentArgs1():
    args = {
        'name': '',
        'attr1': True,
        'attr2' : '0',
        'attr3' : '2%',
        'attr4' : '',
        'attr5': ''
    }
 
class ComponentArgs2():
    args = {
        'name': '',
        'attr1': True,
        'attr2' : '0',
        'attr3' : '0%',
        'attr4' : '',
        'attr5': ''
    }
 
class ComponentArgs3():
    args = {
        'name': '',
        'attr1': False,
        'attr2' : '0',
        'attr4' : '',
        'attr5': ''
    }
 
class ComponentArgs4():
    args = {
        'name': '',
        'attr1': True,
        'attr2' : '0',
        'attr6' : False,
        'attr7': ''
    }
 
class Component(object):
 
    def __init__(self, external_attr1, external_attr2):
        self.external_attr1 = external_attr1
        self.external_attr2 = external_attr2
 
def component_builder(ext_attr1, ext_attr2, command_choice, attr_choice):
    component = Component(ext_attr1, ext_attr2)
 
    if command_choice == 1:
        component.mk_cmd = ComponentCommand1.mk_cmd
        component.rm_cmd = ComponentCommand1.rm_cmd
    elif command_choice == 2:
        component.mk_cmd = ComponentCommand2.mk_cmd
        component.rm_cmd = ComponentCommand2.rm_cmd
    else:
        raise ValueError('Command_choice unknown.')
 
    if attr_choice == 1:
        component.args = ComponentArgs1.args
    elif attr_choice == 2:
        component.args = ComponentArgs2.args
    elif attr_choice == 3:
        component.args = ComponentArgs3.args
    elif attr_choice == 4:
        component.args = ComponentArgs4.args
    else:
        raise ValueError('Attr_choice unknown.')
 
    return component
J'ai regarder du coté des designs patterns, et le builder semble correspondre à ce que je souhaiterais faire, mais j'ai du mal à voir une bonne façon de l'implémenter, et je trouve ma fonction component_builder très peu flexible et difficilement maintenable une fois que j'aurais beaucoup d'objets différents à y intégrer.

Merci d'avance, et bonne journée