Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.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; 'value,': 0.04; 'parameter': 0.05; 'python)': 0.05; 'skip:[ 40': 0.07; "'''": 0.09; 'chose': 0.09; 'default.': 0.09; 'false,': 0.09; 'solution,': 0.09; 'splitting': 0.09; 'subject:parameters': 0.09; 'subject:parsing': 0.09; 'tuple': 0.09; "'',": 0.16; '3),': 0.16; ':o)': 0.16; 'arbitrarily': 0.16; 'comma': 0.16; 'comma,': 0.16; 'expression,': 0.16; 'parameters,': 0.16; 'situations,': 0.16; 'subject:function': 0.16; 'def': 0.16; 'figure': 0.21; 'trying': 0.23; 'dictionary': 0.23; 'libraries': 0.25; 'string': 0.26; 'function': 0.26; '(and': 0.27; 'pass': 0.28; 'knowing': 0.28; 'import': 0.29; 'far.': 0.29; 'fine.': 0.29; 'object': 0.30; 'match': 0.30; 'none,': 0.30; 'values,': 0.30; 'print': 0.32; "skip:' 10": 0.32; 'list': 0.32; 'it.': 0.33; 'external': 0.33; 'to:addr:python-list': 0.34; 'calling': 0.34; '(as': 0.34; '\xa0\xa0\xa0': 0.34; "can't": 0.34; 'actual': 0.35; 'regular': 0.36; 'pull': 0.37; 'anything': 0.37; 'open': 0.37; 'some': 0.37; 'but': 0.37; 'problems': 0.38; 'except': 0.39; 'list,': 0.39; 'to:addr:python.org': 0.39; 'appreciated.': 0.40; 'here': 0.66; 'assistance': 0.66; 'legal': 0.69; 'enhancement.': 0.84; 'namely': 0.84; 'or:': 0.91 X-Originating-IP: [70.101.97.52] From: Lee Harr To: Subject: parsing function parameters Date: Wed, 3 Aug 2011 20:32:04 +0430 Importance: Normal Content-Type: text/plain; charset="windows-1256" Content-Transfer-Encoding: 8bit MIME-Version: 1.0 X-OriginalArrivalTime: 03 Aug 2011 16:02:05.0460 (UTC) FILETIME=[B0A96D40:01CC51F6] X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312387394 news.xs4all.nl 23959 [2001:888:2000:d::a6]:52911 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10811 I am trying to get some information about a function before (and without) calling it. Here is what I have so far. I chose to go with a regular expression, so now I have 2 problems :o) def pdict(f, pstr):     '''given a function object and a string with the function parameters,     return a dictionary of {parameter name: default value or None, ...}     I don't really need the actual default values, just knowing that     they exist or having their string representations would be fine.     def foo(x, y, z=50):         pass     ps = 'x, y, z=50'     pdict(foo, ps)     {'x': None, 'y': None, 'z': 50}     OR: {'x': '', 'y': '', 'z': '50'}     OR: {'x': False, 'y': False, 'z': True}     Note that the parameter list can be arbitrarily complex         (as long as it is legal python)     def bar(m, n=(1, 2, 3), o  =  ('n=5', 'or anything legal')):         pass     pps = 'm, n=(1, 2, 3), o  =  ('n=5', 'or anything legal')'     pdict(bar, pps)     {'m': None, 'n': '(1, 2, 3)', 'o': "('n=5', 'or anything legal')"}     '''     prs = f.func_code.co_varnames     rxl = [r'(\s*%s\s*=(?=(?:\s*\S+))\s*\S+)\s*'%pr for pr in prs]     rx = ','.join(rxl)     print rx     import re     re.match(rx, pstr).groups() This regex works in limited situations, namely when every parameter has a default value, but I can't figure out how to also make it match a parameter that has no default. I am open to any solution, though I am reluctant to pull in any more external libraries just for this one little enhancement. I have it working by just splitting the pstr on comma, except of course that it fails when the default value contains a comma (a tuple or list, for instance). Any assistance or ideas appreciated.