Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,43 @@ Hence, you can build a URL up in steps:
>>> u.as_string()
'http://www.example.com/some/path?q=search+term'

Along with the above overloaded methods, there is also a ``add_path_segment``
method for adding a segment at the end of the current path:
Along with the above overloaded methods, there are also the ``add_path_segment``
and ``add_path_segments`` methods for adding segments at the end of the current
path:

.. code:: python

>>> new_url = u.add_path_segment('here')
>>> new_url.as_string()
'http://www.example.com/some/path/here?q=search+term'
>>> new_url = u.add_path_segments('here', 'too')
>>> new_url.as_string()
'http://www.example.com/some/path/here/too?q=search+term'

There are also the ``append_query_param``, ``append_query_params`` and
``remove_query_param`` methods:

.. code:: python

# appending only one parameter
>>> u = u.append_query_param('key1', 'value1')
>>> u.as_string()
'https://www.google.com/search?q=testing&key1=value1'

# removing a parameter
>>> u = u.remove_query_param('key1')
>>> u.as_string()
'https://www.google.com/search?q=testing'

# appending multiple parameters at once
>>> u = u.append_query_params(('key', 'value'), ('another', 'test'))
>>> u.as_string()
'https://www.google.com/search?q=testing&key=value&another=test'

# removing multiple parameters at once
>>> u = u.remove_query_params('q', 'key', 'another')
>>> u.as_string()
'https://www.google.com/search'

Couple of other things:

Expand Down
37 changes: 37 additions & 0 deletions purl/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,18 @@ def add_path_segment(self, value):
segments = self.path_segments() + (to_unicode(value),)
return self.path_segments(segments)

def add_path_segments(self, *values):
"""
Add one or more path segments to the end ot the current string

:param string values: the new segments to use
"""

segments = self.path_segments()
for value in values:
segments += (to_unicode(value),)
return self.path_segments(segments)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could loop over values and call self.add_path_segment on each right?


# ============
# Query params
# ============
Expand Down Expand Up @@ -475,6 +487,19 @@ def append_query_param(self, key, value):
values.append(value)
return self.query_param(key, values)

def append_query_params(self, *keyvaluetuples):
"""
Append one or more query parameters

Provide the query parameters as tuples, where the first value in the
tuple represents the key, and the second one represents the value.
"""

url = self
for keyvalue in keyvaluetuples:
url = url.append_query_param(keyvalue[0], keyvalue[1])
return url

def query_params(self, value=None):
"""
Return or set a dictionary of query params
Expand Down Expand Up @@ -510,6 +535,18 @@ def remove_query_param(self, key, value=None):
del parse_result[key]
return URL._mutate(self, query=unicode_urlencode(parse_result, doseq=True))

def remove_query_params(self, *keys):
"""
Remove one or more query parameters from a URL

:param string key: The keys to delete
"""

url = self
for key in keys:
url = url.remove_query_param(key)
return url

# =======
# Helpers
# =======
Expand Down