Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'else:': 0.03; 'parameters': 0.04; 'api.': 0.05; 'elif': 0.05; 'float': 0.07; 'string': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; '(via': 0.16; '-1;': 0.16; 'args[0]': 0.16; 'comments:': 0.16; 'expecting': 0.16; 'from:addr:behnel.de': 0.16; 'from:addr:stefan_ml': 0.16; 'from:name:stefan behnel': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:type': 0.16; 'subject:when': 0.16; 'code.': 0.18; 'obviously': 0.18; 'bit': 0.19; 'stefan': 0.19; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'error': 0.23; 'char': 0.24; 'integer': 0.24; '(or': 0.24; 'handling': 0.26; 'switch': 0.26; 'this:': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply- To:1': 0.27; 'raise': 0.29; 'converting': 0.30; 'skip:( 20': 0.30; 'break;': 0.31; 'null;': 0.31; 'parameters.': 0.31; 'subject:the': 0.34; "i'd": 0.34; 'something': 0.35; 'received:84': 0.35; 'there': 0.35; 'object,': 0.36; 'next': 0.36; 'subject:?': 0.36; 'example,': 0.37; 'two': 0.37; 'depends': 0.38; 'generic': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'though,': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'commands': 0.60; "you're": 0.61; 'such': 0.63; 'more': 0.64; 'here': 0.66; 'inform': 0.78; 'const': 0.84; 'received:arcor-ip.net': 0.84; 'received:pools.arcor-ip.net': 0.84; 'subject:anything': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Stefan Behnel Subject: Re: PyArg_ParseTuple() when the type could be anything? Date: Sat, 03 Aug 2013 16:31:37 +0200 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: dslb-084-056-004-084.pools.arcor-ip.net User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130623 Thunderbird/17.0.7 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375540313 news.xs4all.nl 15993 [2001:888:2000:d::a6]:34009 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51860 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 = 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