浏览代码

Add a class method to RPNExpr for random expr generation

Yann Weber 1年前
父节点
当前提交
45c1e4e3a8
共有 2 个文件被更改,包括 38 次插入0 次删除
  1. 30
    0
      python_rpnexpr.c
  2. 8
    0
      python_rpnexpr.h

+ 30
- 0
python_rpnexpr.c 查看文件

@@ -20,6 +20,8 @@
20 20
 
21 21
 PyMethodDef RPNExpr_methods[] = {
22 22
 	{"eval", (PyCFunction)rpnexpr_eval, METH_FASTCALL, "Evaluate an expression"},
23
+	{"random", (PyCFunction)rpnexpr_random, METH_CLASS | METH_VARARGS | METH_KEYWORDS,
24
+		"Return a new random RPN expression string"},
23 25
 	{"reset_stack", (PyCFunction)rpnexpr_reset_stack, METH_NOARGS,
24 26
 		"Reset stack memory storage (set all items to 0)"},
25 27
 	{"__getstate__", (PyCFunction)rpnexpr_getstate, METH_NOARGS,
@@ -553,3 +555,31 @@ PyObject* rpnexpr_repr(PyObject *self)
553 555
 	return res;
554 556
 }
555 557
 
558
+PyObject* rpnexpr_random(PyObject *cls, PyObject *args, PyObject *kwds)
559
+{
560
+	long long int args_count, expr_sz;
561
+	char *expr, err_str[128];
562
+	PyObject *res;
563
+	char *names[] = {"args_count", "token_count", NULL};
564
+
565
+	expr_sz = 10;
566
+	if(!PyArg_ParseTupleAndKeywords(args, kwds, "L|L:pyrpn.random_expr", names,
567
+		&args_count, &expr_sz))
568
+	{
569
+		return NULL;
570
+	}
571
+	
572
+	expr = rpn_random(expr_sz, args_count);
573
+	if(!expr)
574
+	{
575
+		snprintf(err_str, 128,
576
+			"Error generating random expression : %s",
577
+			strerror(errno));
578
+		PyErr_SetString(PyExc_RuntimeError, err_str);
579
+		return NULL;
580
+	}
581
+	res = Py_BuildValue("s", expr);
582
+	//free(expr);
583
+	return res;
584
+}
585
+

+ 8
- 0
python_rpnexpr.h 查看文件

@@ -154,4 +154,12 @@ PyObject* rpnexpr_repr(PyObject *self);
154 154
  */
155 155
 PyObject* rpnexpr_str(PyObject *self);
156 156
 
157
+/**@brief Return a new Python str with a random RPN expression
158
+ * @param mod pyrpn module object
159
+ * @param args Position arguments in Python list
160
+ * @param kwds Keywords arguments in Python dict
161
+ * @ingroup python_module
162
+ */
163
+PyObject* rpnexpr_random(PyObject *cls, PyObject *args, PyObject *kwds);
164
+
157 165
 #endif

正在加载...
取消
保存