Skip to content

Commit 0300573

Browse files
author
Sky NSS
committed
changed url_params to http_params for read_*
1 parent bfaddc6 commit 0300573

File tree

7 files changed

+58
-58
lines changed

7 files changed

+58
-58
lines changed

doc/source/whatsnew/v0.21.0.txt

+9-9
Original file line numberDiff line numberDiff line change
@@ -86,29 +86,29 @@ Other Enhancements
8686

8787
If `python-requests` library is installed try to use it first. If not, continue using urllib
8888
The :meth:`DataFrame.read_csv`, :meth:`DataFrame.read_html`, :meth:`DataFrame.read_json`,
89-
:meth:`DataFrame.read_excel` now allow optional param of ``url_params`` to pass in
89+
:meth:`DataFrame.read_excel` now allow optional param of ``http_params`` to pass in
9090
parameters for basic auth, disable ssl strict check or even a requests.Session() object
9191

9292

9393
.. ipython:: python
9494
import pandas as pd
9595

96-
# url_params is optional parameter. If it is non-empty, it attempts to use python-requests library
97-
df = pd.read_csv('https://uname:[email protected]/bb.csv', url_params= {'auth': None} ) # now url can contain username and pwd
96+
# http_params is optional parameter. If it is non-empty, it attempts to use python-requests library
97+
df = pd.read_csv('https://uname:[email protected]/bb.csv', http_params= {'auth': None} ) # now url can contain username and pwd
9898
# Note - all basic auth scenarios require python-requests library
9999

100100
# Basic Auth
101-
df = pd.read_csv('https://aa.com/bb.csv', url_params={ 'auth': ('john', 'pwd') } ) # now url can contain username and pwd
101+
df = pd.read_csv('https://aa.com/bb.csv', http_params={ 'auth': ('john', 'pwd') } ) # now url can contain username and pwd
102102

103103
# Basic Auth And disable verification of SSL certificate eg: testing
104104
up = { 'auth': ('john', 'pwd') , 'verify' : False}
105-
df = pd.read_csv('https://aa.com/bb.csv', url_params=up ) # now url can contain username and pwd
105+
df = pd.read_csv('https://aa.com/bb.csv', http_params=up ) # now url can contain username and pwd
106106

107-
# Optionally, A requests.Session() can also be passed into url_params
107+
# Optionally, A requests.Session() can also be passed into http_params
108108
import requests
109109
s = requests.Session()
110110
s.auth = MyAuthProvider('secret-key') # custom auth provider supported by requests
111-
df = pd.read_csv(url, url_params=s)
111+
df = pd.read_csv(url, http_params=s)
112112

113113
# For advanced users, this may provide extensibility. However, testing on pandas side is limited to basic scenarios
114114
# here is an example of advanced scenario
@@ -119,14 +119,14 @@ parameters for basic auth, disable ssl strict check or even a requests.Session()
119119
s.headers.update( {'User-Agent': 'Custom user agent'} ) # extensible to set any custom header needed
120120
s.proxies = { 'http': 'http://a.com:100'} # if user has proxies
121121
s.cert = '/path/client.cert' # if custom cert is needed
122-
df = pd.read_csv( 'https://aa.com/bbb.csv', url_params=s)
122+
df = pd.read_csv( 'https://aa.com/bbb.csv', http_params=s)
123123

124124
def print_http_status(r, *args, **kwargs):
125125
print(r.status_code)
126126
print(r.headers['Content-Length'])
127127
s = Session()
128128
s.hooks = dict(response=print_http_status)
129-
df = pd.read_csv( 'https://aa.com/bbb.csv', url_params=s)
129+
df = pd.read_csv( 'https://aa.com/bbb.csv', http_params=s)
130130

131131

132132
.. _whatsnew_0210.api_breaking:

pandas/io/common.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -191,29 +191,29 @@ def _is_handled_by_requests(o):
191191
return _is_url(o) and parse_url(o).scheme in ['http', 'https']
192192

193193

194-
def gen_session(url_params):
194+
def gen_session(http_params):
195195
"""
196-
Generate python-requests session from url_params dict
196+
Generate python-requests session from http_params dict
197197
"""
198198
s = None
199-
if url_params and type(url_params) is requests.sessions.Session:
200-
s = url_params
199+
if http_params and type(http_params) is requests.sessions.Session:
200+
s = http_params
201201
else:
202202
s = requests.Session()
203203
s.stream = True
204204
# Setting accept-encoding to None for backwards compatibility with
205205
# urlopen. ideally we want to allow gzip download
206206
# urlopen doesnt decompress automatically, requests does.
207207
s.headers.update({'Accept-Encoding': None})
208-
if url_params and type(url_params) is dict:
209-
if url_params.get('auth', None) and not s.auth:
210-
s.auth = url_params.get('auth')
211-
if url_params.get('verify', True) is False and s.verify is not False:
212-
s.verify = url_params.get('verify')
208+
if http_params and type(http_params) is dict:
209+
if http_params.get('auth', None) and not s.auth:
210+
s.auth = http_params.get('auth')
211+
if http_params.get('verify', True) is False and s.verify is not False:
212+
s.verify = http_params.get('verify')
213213
return s
214214

215215

216-
def fetch_url(url, url_params=None, skip_requests=False):
216+
def fetch_url(url, http_params=None, skip_requests=False):
217217
"""
218218
If url is url, first try python-requests else try urllib.
219219
Note if requests library is used, auto gunzip is
@@ -226,7 +226,7 @@ def fetch_url(url, url_params=None, skip_requests=False):
226226
'http://cnn.com'
227227
'file:///home/sky/aaa.csv'
228228
229-
url_params : dict or requests.Session(), default None
229+
http_params : dict or requests.Session(), default None
230230
A python dict containing:
231231
'auth': tuple (str, str) eg (unae, pwd)
232232
'auth': Any other auth object accepted by requests
@@ -244,20 +244,20 @@ def fetch_url(url, url_params=None, skip_requests=False):
244244
.. versionadded:: 0.21.0
245245
Raises
246246
------
247-
ValueError if url_params specified without installed python-requests pkg
247+
ValueError if http_params specified without installed python-requests pkg
248248
"""
249-
if not url_params:
249+
if not http_params:
250250
skip_requests = True
251251
if (not skip_requests) and \
252252
is_requests_pkg_avail() and \
253253
_is_handled_by_requests(url):
254-
s = gen_session(url_params)
254+
s = gen_session(http_params)
255255
resp = s.get(url)
256256
resp.raise_for_status()
257257
content_bytes = resp.content
258258
else:
259-
if url_params and (skip_requests or not is_requests_pkg_avail()):
260-
msg = 'To utilize url_params, python-requests library is ' + \
259+
if http_params and (skip_requests or not is_requests_pkg_avail()):
260+
msg = 'To utilize http_params, python-requests library is ' + \
261261
'required but not detected'
262262
raise ValueError(msg)
263263
resp = _urlopen(url)
@@ -266,7 +266,7 @@ def fetch_url(url, url_params=None, skip_requests=False):
266266

267267

268268
def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
269-
compression=None, url_params=None,
269+
compression=None, http_params=None,
270270
skip_requests=False):
271271
"""
272272
If the filepath_or_buffer is a url, translate and return the buffer.
@@ -281,7 +281,7 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
281281
compression : str, default None
282282
indicate the compression such as 'gzip'.
283283
284-
url_params : dict or requests.Session(), default None
284+
http_params : dict or requests.Session(), default None
285285
A python dict containing:
286286
'auth': tuple (str, str) eg (unae, pwd)
287287
'auth': Any other auth object accepted by requests
@@ -304,13 +304,13 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
304304
305305
Raises
306306
------
307-
ValueError if url_params specified without installed python-requests pkg
307+
ValueError if http_params specified without installed python-requests pkg
308308
"""
309309
filepath_or_buffer = _stringify_path(filepath_or_buffer)
310310

311311
if _is_url(filepath_or_buffer):
312312
req, content_bytes = fetch_url(filepath_or_buffer,
313-
url_params,
313+
http_params,
314314
skip_requests)
315315
reader = BytesIO(content_bytes)
316316
content_encoding = req.headers.get('Content-Encoding', None)

pandas/io/excel.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def read_excel(io, sheet_name=0, header=0, skiprows=None, skip_footer=0,
213213

214214
if not isinstance(io, ExcelFile):
215215
io = ExcelFile(io, engine=engine,
216-
url_params=kwds.get('url_params', None))
216+
http_params=kwds.get('http_params', None))
217217

218218
return io._parse_excel(
219219
sheetname=sheet_name, header=header, skiprows=skiprows, names=names,
@@ -261,8 +261,8 @@ def __init__(self, io, **kwds):
261261
# If io is a url, want to keep the data as bytes so can't pass
262262
# to get_filepath_or_buffer()
263263
if _is_url(self.io):
264-
rs = kwds.get('url_params', None)
265-
req, content = fetch_url(self.io, url_params=rs)
264+
rs = kwds.get('http_params', None)
265+
req, content = fetch_url(self.io, http_params=rs)
266266
io = BytesIO(content)
267267
elif not isinstance(self.io, (ExcelFile, xlrd.Book)):
268268
io, _, _ = get_filepath_or_buffer(self._io)

pandas/io/html.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,14 @@ def _get_skiprows(skiprows):
116116
type(skiprows).__name__)
117117

118118

119-
def _read(obj, url_params=None):
119+
def _read(obj, http_params=None):
120120
"""Try to read from a url, file or string.
121121
122122
Parameters
123123
----------
124124
obj : str, unicode, or file-like
125125
126-
url_params : dict or requests.Session(), default None
126+
http_params : dict or requests.Session(), default None
127127
A python dict containing:
128128
'auth': tuple (str, str) eg (unae, pwd)
129129
'auth': Any other auth object accepted by requests
@@ -140,7 +140,7 @@ def _read(obj, url_params=None):
140140
raw_text : str
141141
"""
142142
if _is_url(obj):
143-
req, text = fetch_url(obj, url_params)
143+
req, text = fetch_url(obj, http_params)
144144
elif hasattr(obj, 'read'):
145145
text = obj.read()
146146
elif isinstance(obj, char_types):
@@ -198,12 +198,12 @@ class _HtmlFrameParser(object):
198198
functionality.
199199
"""
200200

201-
def __init__(self, io, match, attrs, encoding, url_params=None):
201+
def __init__(self, io, match, attrs, encoding, http_params=None):
202202
self.io = io
203203
self.match = match
204204
self.attrs = attrs
205205
self.encoding = encoding
206-
self.url_params = url_params
206+
self.http_params = http_params
207207

208208
def parse_tables(self):
209209
tables = self._parse_tables(self._build_doc(), self.match, self.attrs)
@@ -456,7 +456,7 @@ def _parse_tables(self, doc, match, attrs):
456456
return result
457457

458458
def _setup_build_doc(self):
459-
raw_text = _read(self.io, self.url_params)
459+
raw_text = _read(self.io, self.http_params)
460460
if not raw_text:
461461
raise ValueError('No text parsed from document: %s' % self.io)
462462
return raw_text
@@ -744,7 +744,7 @@ def _parse(flavor, io, match, attrs, encoding, **kwargs):
744744
for flav in flavor:
745745
parser = _parser_dispatch(flav)
746746
p = parser(io, compiled_match, attrs, encoding,
747-
url_params=kwargs.get('url_params', None))
747+
http_params=kwargs.get('http_params', None))
748748

749749
try:
750750
tables = p.parse_tables()
@@ -768,7 +768,7 @@ def read_html(io, match='.+', flavor=None, header=None, index_col=None,
768768
skiprows=None, attrs=None, parse_dates=False,
769769
tupleize_cols=False, thousands=',', encoding=None,
770770
decimal='.', converters=None, na_values=None,
771-
keep_default_na=True, url_params=None):
771+
keep_default_na=True, http_params=None):
772772
r"""Read HTML tables into a ``list`` of ``DataFrame`` objects.
773773
774774
Parameters
@@ -869,7 +869,7 @@ def read_html(io, match='.+', flavor=None, header=None, index_col=None,
869869
870870
.. versionadded:: 0.19.0
871871
872-
url_params : requests.Session(), default None
872+
http_params : requests.Session(), default None
873873
A python requests.Session object if http(s) path to enable basic auth
874874
and many other scenarios that requests allows
875875
@@ -922,4 +922,4 @@ def read_html(io, match='.+', flavor=None, header=None, index_col=None,
922922
parse_dates=parse_dates, tupleize_cols=tupleize_cols,
923923
thousands=thousands, attrs=attrs, encoding=encoding,
924924
decimal=decimal, converters=converters, na_values=na_values,
925-
keep_default_na=keep_default_na, url_params=url_params)
925+
keep_default_na=keep_default_na, http_params=http_params)

pandas/io/json/json.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def write(self):
174174
def read_json(path_or_buf=None, orient=None, typ='frame', dtype=True,
175175
convert_axes=True, convert_dates=True, keep_default_dates=True,
176176
numpy=False, precise_float=False, date_unit=None, encoding=None,
177-
lines=False, url_params=None):
177+
lines=False, http_params=None):
178178
"""
179179
Convert a JSON string to pandas object
180180
@@ -263,7 +263,7 @@ def read_json(path_or_buf=None, orient=None, typ='frame', dtype=True,
263263
264264
.. versionadded:: 0.19.0
265265
266-
url_params : dict or requests.Session(), default None
266+
http_params : dict or requests.Session(), default None
267267
A python dict containing:
268268
'auth': tuple (str, str) eg (unae, pwd)
269269
'auth': Any other auth object accepted by requests
@@ -334,7 +334,7 @@ def read_json(path_or_buf=None, orient=None, typ='frame', dtype=True,
334334

335335
filepath_or_buffer, _, _ = get_filepath_or_buffer(path_or_buf,
336336
encoding=encoding,
337-
url_params=url_params)
337+
http_params=http_params)
338338
if isinstance(filepath_or_buffer, compat.string_types):
339339
try:
340340
exists = os.path.exists(filepath_or_buffer)

pandas/io/parsers.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,11 @@ def _read(filepath_or_buffer, kwds):
391391
encoding = re.sub('_', '-', encoding).lower()
392392
kwds['encoding'] = encoding
393393

394-
url_params = kwds.get('url_params', None)
394+
http_params = kwds.get('http_params', None)
395395
compression = kwds.get('compression')
396396
compression = _infer_compression(filepath_or_buffer, compression)
397397
filepath_or_buffer, _, compression = get_filepath_or_buffer(
398-
filepath_or_buffer, encoding, compression, url_params)
398+
filepath_or_buffer, encoding, compression, http_params)
399399
kwds['compression'] = compression
400400

401401
if kwds.get('date_parser', None) is not None:
@@ -579,7 +579,7 @@ def parser_f(filepath_or_buffer,
579579
float_precision=None,
580580

581581
# python requests session
582-
url_params=None,
582+
http_params=None,
583583
):
584584

585585
# Alias sep -> delimiter.
@@ -662,7 +662,7 @@ def parser_f(filepath_or_buffer,
662662
infer_datetime_format=infer_datetime_format,
663663
skip_blank_lines=skip_blank_lines,
664664

665-
url_params=url_params)
665+
http_params=http_params)
666666

667667
return _read(filepath_or_buffer, kwds)
668668

pandas/tests/io/test_http_auth.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,18 @@ def check_http_auth(url, uname, pwd, verify_ssl):
142142

143143
def get_df(url, uname, pwd, verify_ssl, pd_read_fn, fname):
144144
furl = url + fname
145-
url_params = {}
145+
http_params = {}
146146
if uname or pwd:
147-
url_params['auth'] = (uname, pwd)
147+
http_params['auth'] = (uname, pwd)
148148
if verify_ssl is not None:
149-
url_params['verify'] = verify_ssl
149+
http_params['verify'] = verify_ssl
150150
msg = '{0: <90} -- auth:[{1: <10}/{2: <10}] v:[{3: <5}]'.format(
151151
furl, str(uname), str(pwd), str(verify_ssl))
152152
if verify_ssl or furl.lower().startswith('http://'):
153-
df = pd_read_fn(furl, url_params=url_params)
153+
df = pd_read_fn(furl, http_params=http_params)
154154
else:
155155
with tm.assert_produces_warning(InsecureRequestWarning):
156-
df = pd_read_fn(furl, url_params=url_params)
156+
df = pd_read_fn(furl, http_params=http_params)
157157
if type(df) is list: # html
158158
df = df[0]
159159
smatch = match_csv(df)
@@ -184,9 +184,9 @@ def get_df(url, uname, pwd, verify_ssl, pd_read_fn, fname):
184184
{'auth': ('uname', 'pwd'), 'verify': False},
185185
]
186186
)
187-
def test_url_params(up):
187+
def test_http_params(up):
188188
_skip_if_no_requests()
189-
s = pd.io.common.gen_session(url_params=up)
189+
s = pd.io.common.gen_session(http_params=up)
190190
assert type(s) is requests.sessions.Session
191191
if up and up.get('auth', None):
192192
assert s.auth == up.get('auth', None)
@@ -199,7 +199,7 @@ def test_pass_session_obj():
199199
s = requests.sessions.Session()
200200
s.auth = ('uname', 'pwd')
201201
s.verify = False
202-
t = pd.io.common.gen_session(url_params=s)
202+
t = pd.io.common.gen_session(http_params=s)
203203
assert s == t
204204
assert s.auth == t.auth
205205
assert s.verify == t.verify
@@ -209,5 +209,5 @@ def test_skip_requests():
209209
with pytest.raises(ValueError):
210210
a = (uname, pwd)
211211
resp, content_bytes = pd.io.common.fetch_url('http://cnn.com',
212-
url_params={'auth': a},
212+
http_params={'auth': a},
213213
skip_requests=True)

0 commit comments

Comments
 (0)