Add a class method to RPNExpr for random expr generation
This commit is contained in:
parent
80a107f16a
commit
45c1e4e3a8
2 changed files with 38 additions and 0 deletions
|
|
@ -20,6 +20,8 @@
|
|||
|
||||
PyMethodDef RPNExpr_methods[] = {
|
||||
{"eval", (PyCFunction)rpnexpr_eval, METH_FASTCALL, "Evaluate an expression"},
|
||||
{"random", (PyCFunction)rpnexpr_random, METH_CLASS | METH_VARARGS | METH_KEYWORDS,
|
||||
"Return a new random RPN expression string"},
|
||||
{"reset_stack", (PyCFunction)rpnexpr_reset_stack, METH_NOARGS,
|
||||
"Reset stack memory storage (set all items to 0)"},
|
||||
{"__getstate__", (PyCFunction)rpnexpr_getstate, METH_NOARGS,
|
||||
|
|
@ -553,3 +555,31 @@ PyObject* rpnexpr_repr(PyObject *self)
|
|||
return res;
|
||||
}
|
||||
|
||||
PyObject* rpnexpr_random(PyObject *cls, PyObject *args, PyObject *kwds)
|
||||
{
|
||||
long long int args_count, expr_sz;
|
||||
char *expr, err_str[128];
|
||||
PyObject *res;
|
||||
char *names[] = {"args_count", "token_count", NULL};
|
||||
|
||||
expr_sz = 10;
|
||||
if(!PyArg_ParseTupleAndKeywords(args, kwds, "L|L:pyrpn.random_expr", names,
|
||||
&args_count, &expr_sz))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
expr = rpn_random(expr_sz, args_count);
|
||||
if(!expr)
|
||||
{
|
||||
snprintf(err_str, 128,
|
||||
"Error generating random expression : %s",
|
||||
strerror(errno));
|
||||
PyErr_SetString(PyExc_RuntimeError, err_str);
|
||||
return NULL;
|
||||
}
|
||||
res = Py_BuildValue("s", expr);
|
||||
//free(expr);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -154,4 +154,12 @@ PyObject* rpnexpr_repr(PyObject *self);
|
|||
*/
|
||||
PyObject* rpnexpr_str(PyObject *self);
|
||||
|
||||
/**@brief Return a new Python str with a random RPN expression
|
||||
* @param mod pyrpn module object
|
||||
* @param args Position arguments in Python list
|
||||
* @param kwds Keywords arguments in Python dict
|
||||
* @ingroup python_module
|
||||
*/
|
||||
PyObject* rpnexpr_random(PyObject *cls, PyObject *args, PyObject *kwds);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue