Ver código fonte

Moved named tuple init in pyrpn module and declaration in python_const

Yann Weber 10 meses atrás
pai
commit
65875ab93a
5 arquivos alterados com 149 adições e 52 exclusões
  1. 92
    0
      python_const.c
  2. 13
    0
      python_const.h
  3. 0
    28
      python_if.c
  4. 1
    4
      python_if.h
  5. 43
    20
      python_pyrpn.c

+ 92
- 0
python_const.c Ver arquivo

@@ -11,6 +11,96 @@ PyModuleDef rpnconstmodule = {
11 11
 	NULL
12 12
 };
13 13
 
14
+/* Various named tuple definition, instanciated in python_rpnexpr module init */
15
+static PyStructSequence_Field params_fields[] = {
16
+	{	.name = "pos_flag",
17
+		.doc = "Position flag, indicating coordinate system",
18
+	},
19
+	{	.name =  "res_flag",
20
+		.doc = "Result flag, indicate how to handle expr results",
21
+	},
22
+	{	.name = "size_lim",
23
+		.doc = "Size limit, an array indicating the space size (depends \
24
+on pos_flag",
25
+	},
26
+	{
27
+		.name = "const_values",
28
+		.doc = "Constant values to use when setting value in space \
29
+(expr gives the position, and we set this value",
30
+	},
31
+	{ NULL, NULL },
32
+};
33
+PyStructSequence_Desc rpnif_params_desc = {
34
+	.name = "RPNIterParams",
35
+	.doc = "Named tuple for RPNIter parameters",
36
+	.n_in_sequence = (sizeof(params_fields) / sizeof(*params_fields))-1,
37
+	.fields = params_fields,
38
+};
39
+PyTypeObject rpnif_params_SeqDesc;
40
+
41
+static PyStructSequence_Field token_types_fields[] = {
42
+	{	.name = "op",
43
+		.doc = "Operation",
44
+	},
45
+	{	.name = "const",
46
+		.doc = "Constant value",
47
+	},
48
+	{	.name = "var",
49
+		.doc = "Variable (argument)",
50
+	},
51
+};
52
+PyStructSequence_Desc rpn_token_types_desc = {
53
+	.name = "RPNTokenTypes",
54
+	.doc = "Named tuple with token types as keys",
55
+	.n_in_sequence = 3,
56
+	.fields = token_types_fields,
57
+};
58
+PyTypeObject rpn_token_types_SeqDesc;
59
+
60
+static PyStructSequence_Field mutation_params_fields[] = {
61
+	{	.name = "minimum_length",
62
+		.doc = "Expression minimum length",
63
+	},
64
+	{	.name =  "weight_add",
65
+		.doc = "The weight for 'add token' mutation",
66
+	},
67
+	{	.name = "weight_del",
68
+		.doc = "The weight for 'delete token' mutation",
69
+	},
70
+	{	.name = "weight_mut",
71
+		.doc = "The weight for 'mutate token' mutation",
72
+	},
73
+	{	.name = "weight_mut_soft",
74
+		.doc = "The weight for 'mutate soft (value not type) token' mutation",
75
+	},
76
+	{
77
+		.name = "weight_add_elt",
78
+		.doc = "The weights for each token types when adding a token",
79
+	},
80
+	{
81
+		.name = "weight_del_elt",
82
+		.doc= "The weights for each token types when deleting a token",
83
+	}
84
+};
85
+PyStructSequence_Desc rpn_mutation_params_desc = {
86
+	.name = "RPNMutationParams",
87
+	.doc = "Named tuple for mutation parameters",
88
+	.n_in_sequence = (sizeof(mutation_params_fields) / sizeof(*mutation_params_fields)),
89
+	.fields = mutation_params_fields,
90
+};
91
+PyTypeObject rpn_mutation_params_SeqDesc;
92
+
93
+
94
+rpn_mutation_params_t rpn_mutation_params_default = {
95
+	.min_len = 3,
96
+	.w_add = 1.25,
97
+	.w_del = 1.0,
98
+	.w_mut = 2.0,
99
+	.w_mut_soft = 4.0,
100
+	.w_add_elt = {1.0,1.0,1.0},
101
+	.w_mut_elt = {1.0,1.0,1.0},
102
+};
103
+
14 104
 int Py_rpnconst_add(PyObject* mod, const char* name, int value)
15 105
 {
16 106
 	PyObject *val;
@@ -30,6 +120,8 @@ PyObject *Py_rpnconst_init(void)
30 120
 	mod = PyModule_Create(&rpnconstmodule);
31 121
 	if(mod == NULL) { return NULL; }
32 122
 
123
+	rpn_mutation_init_params(&rpn_mutation_params_default);
124
+
33 125
 	if(Py_rpnconst_add(mod, "POS_LINEAR", RPN_IF_POSITION_LINEAR) ||
34 126
 		Py_rpnconst_add(mod, "POS_XY", RPN_IF_POSITION_XY) ||
35 127
 		Py_rpnconst_add(mod, "POS_XDIM", RPN_IF_POSITION_XDIM) ||

+ 13
- 0
python_const.h Ver arquivo

@@ -27,12 +27,25 @@
27 27
 #include <Python.h>
28 28
 #include "structmember.h"
29 29
 
30
+#include "rpn_mutate.h"
30 31
 #include "rpn_if_default.h"
31 32
 
32 33
 /**@brief pyrpn.const module specs
33 34
  * @ingroup python_module */
34 35
 extern PyModuleDef rpnconstmodule;
35 36
 
37
+/**@brief A single instance of this object is used for parameters representation */
38
+extern PyTypeObject rpnif_params_SeqDesc;
39
+extern PyStructSequence_Desc rpnif_params_desc;
40
+
41
+extern PyTypeObject rpn_token_types_SeqDesc;
42
+extern PyStructSequence_Desc rpn_token_types_desc;
43
+
44
+extern PyTypeObject rpn_mutation_params_SeqDesc;
45
+extern PyStructSequence_Desc rpn_mutation_params_desc;
46
+
47
+extern rpn_mutation_params_t rpn_mutation_params_default;
48
+
36 49
 /**@brief pyrpn.const module initialisation function
37 50
  * @ingroup python_module */
38 51
 PyObject *Py_rpnconst_init(void);

+ 0
- 28
python_if.c Ver arquivo

@@ -1,33 +1,5 @@
1 1
 #include "python_if.h"
2 2
 
3
-static PyStructSequence_Field params_fields[] = {
4
-	{	.name = "pos_flag",
5
-		.doc = "Position flag, indicating coordinate system",
6
-	},
7
-	{	.name =  "res_flag",
8
-		.doc = "Result flag, indicate how to handle expr results",
9
-	},
10
-	{	.name = "size_lim",
11
-		.doc = "Size limit, an array indicating the space size (depends \
12
-on pos_flag",
13
-	},
14
-	{
15
-		.name = "const_values",
16
-		.doc = "Constant values to use when setting value in space \
17
-(expr gives the position, and we set this value",
18
-	},
19
-	{ NULL, NULL },
20
-};
21
-PyStructSequence_Desc rpnif_params_desc = {
22
-	.name = "RPNIterParams",
23
-	.doc = "Named tuple for RPNIter parameters",
24
-	.n_in_sequence = (sizeof(params_fields) / sizeof(*params_fields))-1,
25
-	.fields = params_fields,
26
-};
27
-
28
-PyTypeObject rpnif_params_SeqDesc;
29
-
30
-
31 3
 PyMethodDef RPNIterExpr_methods[] = {
32 4
 	{"get_params", (PyCFunction)rpnif_get_params, METH_NOARGS,
33 5
 		"Get a named tuple with parameters"},

+ 1
- 4
python_if.h Ver arquivo

@@ -30,6 +30,7 @@
30 30
 #include "rpn_if.h"
31 31
 #include "rpn_if_default.h"
32 32
 #include "python_rpnexpr.h"
33
+#include "python_const.h"
33 34
 
34 35
 /**@defgroup python_if RPN Iterated Function Python class
35 36
  * @ingroup python_module
@@ -49,10 +50,6 @@
49 50
  * This file is the header of the RPNIterExpr Python class
50 51
  */
51 52
 
52
-/**@brief A single instance of this object is used for parameters representation */
53
-extern PyTypeObject rpnif_params_SeqDesc;
54
-extern PyStructSequence_Desc rpnif_params_desc;
55
-
56 53
 /**@brief RPNIterExpr Python class methods list
57 54
  * @ingroup python_if */
58 55
 extern PyMethodDef RPNIterExpr_methods[];

+ 43
- 20
python_pyrpn.c Ver arquivo

@@ -58,46 +58,37 @@ PyInit_pyrpn(void)
58 58
 	const_mod = Py_rpnconst_init();
59 59
 	if(const_mod == NULL)
60 60
 	{
61
-		Py_DECREF(mod);
62
-		return NULL;
61
+		goto fail_init;
63 62
 	}
64 63
 	Py_INCREF(const_mod);
65 64
 	if(PyModule_AddObject(mod, "const", const_mod) < 0)
66 65
 	{
67
-		Py_DECREF(const_mod);
68
-		Py_DECREF(mod);
69
-		return NULL;
66
+		goto fail_add_const;
70 67
 	}
71 68
 
72 69
 	// Init RPNExpr type
73 70
 	if(PyType_Ready(&RPNExprType) < 0)
74 71
 	{
75
-		return NULL;
72
+		goto fail_expr_type_ready;
76 73
 	}
77 74
 
78 75
 	// Add type to module
79 76
 	Py_INCREF(&RPNExprType);
80 77
 	if(PyModule_AddObject(mod, "RPNExpr", (PyObject*)&RPNExprType) < 0)
81 78
 	{
82
-		Py_DECREF(&RPNExprType);
83
-		Py_DECREF(mod);
84
-		Py_DECREF(const_mod);
85
-		return NULL;
79
+		goto fail_add_rpnexpr;
86 80
 	}
87 81
 
88 82
 	// Init RPNIterExpr type
89 83
 	if(PyType_Ready(&RPNIterExprType) < 0)
90 84
 	{
91
-		return NULL;
85
+		goto fail_iter_type_ready;
92 86
 	}
93 87
 
94 88
 	Py_INCREF(&RPNIterExprType);
95 89
 	if(PyModule_AddObject(mod, "RPNIterExpr", (PyObject*)&RPNIterExprType) < 0)
96 90
 	{
97
-		Py_DECREF(&RPNExprType);
98
-		Py_DECREF(&RPNIterExprType);
99
-		Py_DECREF(mod);
100
-		return NULL;
91
+		goto fail_add_iter;
101 92
 	}
102 93
 
103 94
 	// Named tuple for RPNIterExpr's params
@@ -110,15 +101,47 @@ PyInit_pyrpn(void)
110 101
 	if(PyModule_AddObject(mod, "RPNIterExprParams",
111 102
 		(PyObject*)&rpnif_params_SeqDesc) < 0)
112 103
 	{
113
-		Py_DECREF(&RPNExprType);
114
-		Py_DECREF(&RPNIterExprType);
115
-		Py_DECREF(&rpnif_params_SeqDesc);
116
-		Py_DECREF(mod);
117
-		return NULL;
104
+		goto fail_add_iter_expr_params;
118 105
 	}
119 106
 
107
+	// Named tuple for token types
108
+	PyStructSequence_InitType(&rpn_token_types_SeqDesc,
109
+			&rpn_token_types_desc);
110
+	Py_INCREF(&rpn_token_types_SeqDesc);
111
+	if(PyModule_AddObject(mod, "RPNTokenTypesTuple",
112
+				(PyObject*)&rpn_token_types_SeqDesc) < 0)
113
+	{
114
+		goto fail_add_token_types_tuple;
115
+	}
116
+
117
+	// Named tuple for mutation parameters
118
+	PyStructSequence_InitType(&rpn_mutation_params_SeqDesc,
119
+			&rpn_mutation_params_desc);
120
+	Py_INCREF(&rpn_mutation_params_SeqDesc);
121
+	if(PyModule_AddObject(mod, "RPNMutationParamsTuple",
122
+				(PyObject*)&rpn_mutation_params_SeqDesc) < 0)
123
+	{
124
+		goto fail_add_mutation_params_tuple;
125
+	}
120 126
 
121 127
 	return mod;
128
+
129
+fail_add_mutation_params_tuple:
130
+	Py_DECREF(&rpn_token_types_SeqDesc);
131
+fail_add_token_types_tuple:
132
+	Py_DECREF(&rpnif_params_SeqDesc);
133
+fail_add_iter_expr_params:
134
+fail_add_iter:
135
+	Py_DECREF(&RPNIterExprType);
136
+fail_iter_type_ready:
137
+fail_add_rpnexpr:
138
+	Py_DECREF(&RPNExprType);
139
+fail_expr_type_ready:
140
+fail_add_const:
141
+	Py_DECREF(const_mod);
142
+fail_init:
143
+	Py_DECREF(mod);
144
+	return NULL;
122 145
 }
123 146
 
124 147
 PyObject* pyrpn_ops(PyObject* mod, PyObject* noargs)

Carregando…
Cancelar
Salvar