This repository has been archived by the owner on May 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
18 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,34 @@ | ||
import re | ||
|
||
try: | ||
from urllib.parse import urlencode | ||
except ImportError: | ||
from urllib import urlencode | ||
|
||
|
||
# Join given url | ||
# == Example | ||
# api.join_url("example.com", "index.html") # Return "example.com/index.html" | ||
def join_url(url, *paths): | ||
""" | ||
Joins individual URL strings together, and returns a single string. | ||
>>> util.join_url("example.com", "index.html") | ||
'example.com/index.html' | ||
""" | ||
for path in paths: | ||
url = re.sub(r'/?$', re.sub(r'^/?', '/', path), url) | ||
return url | ||
|
||
|
||
def join_url_params(url, params): | ||
return url + "?" + urlencode(params) | ||
|
||
|
||
# Merge dict | ||
def merge_dict(data, *override): | ||
dict_list = list(data.items()) | ||
for value in override: | ||
dict_list = dict_list + list(value.items()) | ||
return dict(dict_list) | ||
""" | ||
Merges any number of dictionaries together, and returns a single dictionary | ||
>>> util.merge_dict ({"foo": "bar"}, {1: 2}, {"Pay": "Pal"}) | ||
{1: 2, 'foo': 'bar', 'Pay': 'Pal'} | ||
>>> | ||
""" | ||
result = {} | ||
for current_dict in (data,) + override: | ||
result.update(current_dict) | ||
return result |