docstrings for utils module
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parent 7f35ec7 · Browse files · 1 file·+29 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+29 added−8 removed
- other:+29 added−8 removed
Provenance
- Commit
873d128- Tree
fba6a2e- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
7f35ec7
1 file changed+29 added−8 removed
modified:requests/utils.py+29 added-8 removed
13 unmodified lines14 import re15 import zlib16 17 18-def dict_from_cookiejar(cookiejar):19- """Returns a key/value dictionary from a CookieJar."""18+def dict_from_cookiejar(cj):19+ """Returns a key/value dictionary from a CookieJar.20 21+ :param cj: CookieJar object to extract cookies from.22+ """23+24 cookie_dict = {}25 23- for _, cookies in cookiejar._cookies.items():26+ for _, cookies in cj._cookies.items():27 for _, cookies in cookies.items():28 for cookie in cookies.values():29 # print cookie30 cookie_dict[cookie.name] = cookie.value1 unmodified line32 return cookie_dict33 34 35 def cookiejar_from_dict(cookie_dict):33- """Returns a CookieJar from a key/value dictionary."""36+ """Returns a CookieJar from a key/value dictionary.37 38+ :param cookie_dict: Dict of key/values to insert into CookieJar.39+ """40+41 # return cookiejar if one was passed in42 if isinstance(cookie_dict, cookielib.CookieJar):43 return cookie_dict44 5 unmodified lines50 return cj51 52 53 def add_dict_to_cookiejar(cj, cookie_dict):48- """Returns a CookieJar from a key/value dictionary."""54+ """Returns a CookieJar from a key/value dictionary.55 56+ :param cj: CookieJar to insert cookies into.57+ :param cookie_dict: Dict of key/values to insert into CookieJar.58+ """59+60 for k, v in cookie_dict.items():61 62 cookie = cookielib.Cookie(63 version=0,21 unmodified lines85 return cj86 87 88 def get_encodings_from_content(content):79- """Returns encodings from given content string."""89+ """Returns encodings from given content string.90 91+ :param content: bytestring to extract encodings from.92+ """93+94 charset_re = re.compile(r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I)95 96 return charset_re.findall(content)97 98 99 100 def get_encoding_from_headers(headers):88- """Returns encodings from given HTTP Header Dict."""101+ """Returns encodings from given HTTP Header Dict.102 103+ :param headers: dictionary to extract encoding from.104+ """105+106 content_type = headers.get('content-type')107 content_type, params = cgi.parse_header(content_type)108 109 if 'charset' in params:2 unmodified lines112 113 def get_unicode_from_response(r):114 """Returns the requested content back in unicode.115 116+ :param r: Reponse object to get unicode content from.117+118 Tried:119 1. charset from content-type120 2. every encodings from <meta ... charset=XXX>121 3. fall back and replace all unicode characters30 unmodified lines152 return r.content153 154 155 def decode_gzip(content):138- """Return gzip-decoded string."""156+ """Return gzip-decoded string.157 158+ :param content: bytestring to gzip-decode.159+ """160+161 return zlib.decompress(content, 16+zlib.MAX_WBITS)