Skip to content
Myr

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

1 file+29 added−8 removed

modified:requests/utils.py+29 added-8 removed
13 unmodified lines
14import re15import zlib161718def dict_from_cookiejar(cookiejar):19 """Returns a key/value dictionary from a CookieJar."""18def dict_from_cookiejar(cj):19 """Returns a key/value dictionary from a CookieJar.2021 :param cj: CookieJar object to extract cookies from.22 """2324 cookie_dict = {}2523 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.value
1 unmodified line
32 return cookie_dict333435def cookiejar_from_dict(cookie_dict):33 """Returns a CookieJar from a key/value dictionary."""36 """Returns a CookieJar from a key/value dictionary.3738 :param cookie_dict: Dict of key/values to insert into CookieJar.39 """4041 # return cookiejar if one was passed in42 if isinstance(cookie_dict, cookielib.CookieJar):43 return cookie_dict44
5 unmodified lines
50 return cj515253def 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.5556 :param cj: CookieJar to insert cookies into.57 :param cookie_dict: Dict of key/values to insert into CookieJar.58 """5960 for k, v in cookie_dict.items():6162 cookie = cookielib.Cookie(63 version=0,
21 unmodified lines
85 return cj868788def get_encodings_from_content(content):79 """Returns encodings from given content string."""89 """Returns encodings from given content string.9091 :param content: bytestring to extract encodings from.92 """9394 charset_re = re.compile(r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I)9596 return charset_re.findall(content)979899100def get_encoding_from_headers(headers):88 """Returns encodings from given HTTP Header Dict."""101 """Returns encodings from given HTTP Header Dict.102103 :param headers: dictionary to extract encoding from.104 """105106 content_type = headers.get('content-type')107 content_type, params = cgi.parse_header(content_type)108109 if 'charset' in params:
2 unmodified lines
112113def get_unicode_from_response(r):114 """Returns the requested content back in unicode.115116 :param r: Reponse object to get unicode content from.117118 Tried:119 1. charset from content-type120 2. every encodings from <meta ... charset=XXX>121 3. fall back and replace all unicode characters
30 unmodified lines
152 return r.content153154155def decode_gzip(content):138 """Return gzip-decoded string."""156 """Return gzip-decoded string.157158 :param content: bytestring to gzip-decode.159 """160161 return zlib.decompress(content, 16+zlib.MAX_WBITS)