Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Tim Chase Newsgroups: comp.lang.python Subject: Re: modifying parts of a urlparse.SplitResult Date: Tue, 12 Jan 2016 07:15:46 -0600 Lines: 27 Message-ID: References: <20160112055236.7af20f50@bigbox.christie.dr> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de Fpx390EPoULTci2JV3n70AwT6fxF6R0NjRCetKCmsPKg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'lst': 0.09; 'tuple': 0.09; 'index': 0.13; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'magic': 0.16; 'namedtuples': 0.16; 'pythonic': 0.16; 'received:10.255': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'underscore.': 0.16; 'url:foo': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'code.': 0.23; 'tim': 0.24; 'header:In-Reply-To:1': 0.24; 'skip:u 20': 0.28; 'chase': 0.29; "can't": 0.32; 'safely': 0.33; 'thanks!': 0.34; 'this?': 0.34; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'method': 0.37; 'charset:us-ascii': 0.37; 'starting': 0.37; 'names': 0.38; 'takes': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'skip:n 10': 0.62; 'more': 0.63; 'received:50': 0.66; 'otten': 0.84; 'private.': 0.84 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1452604683403:42113766 X-MC-Ingress-Time: 1452604683403 In-Reply-To: X-Mailer: Claws Mail 3.11.1 (GTK+ 2.24.25; x86_64-pc-linux-gnu) X-AuthUser: tim@thechases.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101541 On 2016-01-12 13:46, Peter Otten wrote: > Tim Chase wrote: > > >>> u = urlparse.urlsplit(URL) > > >>> lst = list(u) # can't manipulate the tuple directly > > >>> lst[3] = "bar=baz" # 3 = query-string index > > >>> urlparse.urlunsplit(lst) > > 'http://foo/path1/path2/?bar=baz' > > > > It takes knowing that "3" is the magic index (documented, but not > > given some named-constant in urlparse) for the query-string. Is > > there some better, clearer, or more Pythonic way to do this? > > To allow more fieldnames namedtuples use method names starting with > an underscore. You can safely use them, they are not private. So: > > >>> pr = urlparse.urlparse("http://foo/path1/path2/?fragment=foo") > >>> urlparse.urlunparse(pr._replace(query="bar=baz")) > 'http://foo/path1/path2/?bar=baz' Much nicer. Thanks! Off to improve my code. -tkc