txerpa /
besepa-python
| 1 | import re |
||
|
0 ignored issues
–
show
|
|||
| 2 | |||
| 3 | try: # pragma: no cover |
||
| 4 | from urllib.parse import urlencode |
||
| 5 | except ImportError: # pragma: no cover |
||
| 6 | from urllib import urlencode |
||
| 7 | |||
| 8 | |||
| 9 | def join_url(url, *paths): |
||
| 10 | """ |
||
| 11 | Joins individual URL strings together, and returns a single string. |
||
| 12 | |||
| 13 | Usage:: |
||
| 14 | |||
| 15 | >>> util.join_url("example.com", "index.html") |
||
| 16 | 'example.com/index.html' |
||
| 17 | """ |
||
| 18 | for path in paths: |
||
| 19 | url = re.sub(r'/?$', re.sub(r'^/?', '/', path), url) |
||
| 20 | return url |
||
| 21 | |||
| 22 | |||
| 23 | def join_url_params(url, params): |
||
| 24 | """Constructs percent-encoded query string from given parms dictionary |
||
| 25 | and appends to given url |
||
| 26 | Usage:: |
||
| 27 | >>> util.join_url_params("example.com/index.html", {"page-id": 2, "Company": "Tx Erpa"}) |
||
| 28 | example.com/index.html?page-id=2&Company=Tx+Erpa |
||
| 29 | """ |
||
| 30 | return url + "?" + urlencode(params) |
||
| 31 | |||
| 32 | |||
| 33 | def merge_dict(data, *override): |
||
| 34 | """ |
||
| 35 | Merges any number of dictionaries together, and returns a single dictionary |
||
| 36 | |||
| 37 | Usage:: |
||
| 38 | |||
| 39 | >>> util.merge_dict({"foo": "bar"}, {1: 2}, {"Tx": "erpa"}) |
||
| 40 | {1: 2, 'foo': 'bar', 'Tx': 'erpa'} |
||
| 41 | """ |
||
| 42 | result = {} |
||
| 43 | for current_dict in (data,) + override: |
||
| 44 | result.update(current_dict) |
||
| 45 | return result |
||
| 46 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.