| 19 | |
| 20 | |
| 21 | def try_get(data, *path): |
| 22 | def get_one(src, what): |
| 23 | if isinstance(src, dict) and isinstance(what, basestring): |
| 24 | return src.get(what, None) |
| 25 | elif isinstance(src, list) and type(what) is int: |
| 26 | try: |
| 27 | return src[what] |
| 28 | except IndexError: |
| 29 | return None |
| 30 | elif callable(what): |
| 31 | try: |
| 32 | return what(src) |
| 33 | except Exception: |
| 34 | return None |
| 35 | else: |
| 36 | return None |
| 37 | |
| 38 | res = get_one(data, path[0]) |
| 39 | for item in path[1:]: |
| 40 | if res is None: |
| 41 | break |
| 42 | res = get_one(res, item) |
| 43 | |
| 44 | return res |
| 45 | |
| 46 | |
| 47 | class BIGHTTPRequest(HTTPRequest): |