如何使用 PyArg_ParseTupleAndKeywords 解析带有可选参数和关键字的元组? [英] How can one use PyArg_ParseTupleAndKeywords to parse a tuple with optional arguments as well as keywords?

查看:66
本文介绍了如何使用 PyArg_ParseTupleAndKeywords 解析带有可选参数和关键字的元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处找,但找不到 PyArg_ParseTupleAndKeywords() 与元组一起使用的示例 —包含可选参数 —关键字.我找到的最接近的是这个问题,但答案是不是特别有帮助.大多数示例似乎都将关键字作为可选参数,但元组似乎也应该能够包含可选参数.

I've looked everywhere, but I can't find an example of PyArg_ParseTupleAndKeywords() being used with a tuple — containing optional arguments — and keywords. The closest I've found is this question, but the answer isn't particularly helpful. Most examples seem to have the keywords as the optional arguments, but it seems like the tuple should be able to contain optional arguments as well.

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

Suppose I'm trying to parse the following parameters:

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

    看来我应该做类似的事情

    It seems like I should be doing something like

    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;
    }
    

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

    Other places I've looked, but without finding much help:

    使用 PyArg_ParseTupleAndKeywords() 的合适方法是什么?

    What is the appropriate way to use PyArg_ParseTupleAndKeywords()?

    推荐答案

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

    I think something similar to the below simplified solution should work for your scenario but it can get nasty if you have many optional args or more interesting arg types. I'm not sure if there is a better solution but I haven't been able to find one. Hopefully one day someone will post a cleaner solution.

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

    You'll have to be clever to produce useful arg parsing error messages in more complex parsing scenarios.

    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;
    }
    

    这篇关于如何使用 PyArg_ParseTupleAndKeywords 解析带有可选参数和关键字的元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆