Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101537

Re: modifying parts of a urlparse.SplitResult

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: modifying parts of a urlparse.SplitResult
Date Tue, 12 Jan 2016 13:46:43 +0100
Organization None
Lines 39
Message-ID <mailman.58.1452602821.13488.python-list@python.org> (permalink)
References <20160112055236.7af20f50@bigbox.christie.dr>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de 3fQhu+JbIt9426yZ4wdB8QKUuTyA7DiXXs6gYZThssrQ==
Return-Path <python-python-list@m.gmane.org>
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; 'modify': 0.04; 'lst': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tuple': 0.09; 'index': 0.13; 'magic': 0.16; 'namedtuples': 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'underscore.': 0.16; 'url:foo': 0.16; 'wrote:': 0.16; 'attribute': 0.18; '>>>': 0.20; '"",': 0.22; 'parse': 0.22; 'import': 0.24; '(most': 0.24; 'tim': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'appear': 0.26; 'header:X -Complaints-To:1': 0.26; "skip:' 10": 0.28; 'skip:u 20': 0.28; 'chase': 0.29; 'skip:q 20': 0.29; "i'd": 0.31; "can't": 0.32; 'safely': 0.33; 'traceback': 0.33; 'file': 0.34; 'this?': 0.34; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:org': 0.37; 'starting': 0.37; 'names': 0.38; 'skip:p 20': 0.38; 'takes': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'some': 0.40; 'skip:n 10': 0.62; 'more': 0.63; 'private.': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd8ad4.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:101537

Show key headers only | View raw


Tim Chase wrote:

> I can successfully parse my URLs.  However, I'd like to modify a part
> then reassemble them.  However, like tuples, the parts appear to be
> unmodifiable
> 
>   >>> URL = 'http://foo/path1/path2/?fragment=foo'
>   >>> import urlparse
>   >>> u = urlparse.urlparse(URL)
>   >>> u
>   ParseResult(scheme='http', netloc='foo', path='/path1/path2/',
>   params='', query='fragment=foo', fragment='')
>   >>> u.query
>   'fragment=foo'
>   >>> u.query = 'blah=baz'
>   Traceback (most recent call last):
>     File "<stdin>", line 1, in <module>
>   AttributeError: can't set attribute
> 
> The best I've been able to come up with is
> 
>   >>> 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'

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: modifying parts of a urlparse.SplitResult Peter Otten <__peter__@web.de> - 2016-01-12 13:46 +0100

csiph-web