diff --git a/README.rst b/README.rst index bc7f9a0..cc0ed54 100644 --- a/README.rst +++ b/README.rst @@ -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: diff --git a/purl/url.py b/purl/url.py index 46d3e6a..6bde17c 100644 --- a/purl/url.py +++ b/purl/url.py @@ -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) + # ============ # Query params # ============ @@ -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 @@ -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 # =======