David M. Cotter, 03.08.2013 02:55: > I'd like to be able to use PyArg_ParseTuple() in a generic way. > > for example, i'd like to have all commands start with 1 integer parameter, > and this "commandID" will inform me of what parameters come next (via LUT). > > knowing that i can then call ParseTuple again with the proper parameters. > > like this: > > if (PyArg_ParseTuple(args, "i|", &commandID)) { > > switch (commandID) { > > case cmd_with_str: { > const char *strZ = NULL; > > if (PyArg_ParseTuple(args, "is", &commandID, &strZ)) { > // do something with string > } > break; > } > > case cmd_with_float: { > float valF = -1; > > if (PyArg_ParseTuple(args, "if", &commandID, &valF)) { > // do something with float > } > break; > } > } > } > > is there a way to achieve this? the "i|" at the start is not working
If you're willing to switch to Cython, here's an (untested) example: cdef enum: cmd_with_str = 1 cmd_with_float = 2 cdef int command_id = args[0] if command_id == cmd_with_str: str_z = args[1] # it's an object, so use it as such print(str_z) elif command_id == cmd_with_float: val_f = <float>args[1] # converting to C float here ... else: raise ValueError("unknown command") Two comments: 1) you can obviously do the same in C, by writing a bit more code. It would likely be a lot slower, though, and you'd have to take care of error handling etc. 2) you might want to rethink your design as this is a rather unpythonic API. Although it depends on who (or what) you are expecting to use it. Stefan -- http://mail.python.org/mailman/listinfo/python-list