【问题标题】:How can one use PyArg_ParseTupleAndKeywords to parse a tuple with optional arguments as well as keywords?如何使用 PyArg_ParseTupleAndKeywords 解析带有可选参数和关键字的元组?
【发布时间】:2016-01-28 17:31:08
【问题描述】:

我到处寻找,但找不到PyArg_ParseTupleAndKeywords() 与元组一起使用的示例——包含可选参数——和关键字。我找到的最接近的是this question,但答案并不是特别有用。大多数示例似乎将关键字作为可选参数,但元组似乎也应该能够包含可选参数。

假设我正在尝试解析以下参数:

  • numpy 双精度数组(强制)
  • numpy 双精度数组(可选,无关键字)
  • 可选关键字参数:
    • k1 => numpy 双精度数组
    • k2 => 整数
    • k3 => 双
    • k4 => Python 类实例

看来我应该做类似的事情

static PyObject* pymod_func(PyObject* self, PyObject* args, PyObject* kwargs) {
  static char* keywords[] = {"k1", "k2", "k3", "k4", NULL};

  PyObject *arg1, *arg2, *k1, *k4
  PyObject *arr1, *arr2, *karr1;
  double *k3;
  int *k2;
  PyArg_ParseTupleAndKeywords(args, kwargs, "O!|O!OidO", keywords, &arg1, &PyArray_Type, &arg2, &PyArray_Type, &k1, &PyArray_Type, &k2, &k3, &k4);

  arr1 = PyArray_FROM_OTF(arg1, NPY_FLOAT64, NPY_ARRAY_INOUT_ARRAY);
  if (arr1 == NULL) return NULL;

  arr2 = PyArray_FROM_OTF(arg1, NPY_FLOAT64, NPY_ARRAY_INOUT_ARRAY);
  // no null check, because optional

  karr1 = PyArray_FROM_OTF(k1, NPY_FLOAT64, NPY_ARRAY_INOUT_ARRAY);
  // again, no null check, because this is optional

  // do things with k3, k2, and k4 also

  return NULL;
}

我看过的其他地方,但没有找到太多帮助:

PyArg_ParseTupleAndKeywords()的正确使用方式是什么?

【问题讨论】:

    标签: python python-2.7 numpy


    【解决方案1】:

    我认为类似于下面的简化解决方案应该适用于您的场景,但如果您有许多可选参数或更有趣的 arg 类型,它可能会变得令人讨厌。 我不确定是否有更好的解决方案,但我一直找不到。希望有一天有人会发布一个更清洁的解决方案。

    你必须很聪明才能产生有用的 arg 解析错误消息 在更复杂的解析场景中。

    static PyObject* nasty_func(PyObject* self, PyObject* args, PyObject* kwargs) {
      static char* keywords[] = {"one", "optional", "two", NULL};
      static char* keywords_alt[] = {"one", "two", NULL};
    
      int ok = 0;
      PyObject *result = NULL;
    
      int *one;
      char *two;
      int *optional;
    
      ok = PyArg_ParseTupleAndKeywords(args, kwargs, "iis", keywords, &one, &optional, &two);
    
      if (!ok) {
        PyErr_Clear();
        ok = PyArg_ParseTupleAndKeywords(args, kwargs, "is", keywords_alt, &one, &two);
        if (!ok) {
          PyErr_SetString(PyExc_TypeError, "Invalid args. allowed formats: 'one:i, two:s' or 'one:i, optional:i, two:s'");
          return NULL;
        }
      }
    
      // do stuff with your parsed variables
    
      return result;
    }
    

    【讨论】:

      【解决方案2】:

      从 Python 3.3 开始,您可以在 indicate that the rest of the arguments are keyword-only 的格式字符串中使用 $,从 Python 3.6 开始,you can indicate a positional-only parameter 可以在 keywords 参数中使用空名称。

      因此,在足够高的 Python 版本中,您可以使用如下代码:

      static char* keywords[] = {"", "", "k1", "k2", "k3", "k4", NULL};
      
      // [...]
      
      PyArg_ParseTupleAndKeywords(args, kwargs,
                                  "O!|O!$O!idO", keywords,
                                  &PyArray_Type, &arg1, &PyArray_Type, &arg2,
                                  &PyArray_Type, &k1, &k2, &k3, &k4);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-06
        • 2013-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-24
        • 1970-01-01
        相关资源
        最近更新 更多