Skip to content

Commit ea26761

Browse files
committed
Adds back the utils module to support url data sources
1 parent 87142fa commit ea26761

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

mplaltair/_utils.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from urllib.request import urlopen
2+
from urllib.error import HTTPError
3+
4+
import pandas as pd
5+
6+
_PD_READERS = {
7+
'json': pd.read_json,
8+
'csv': pd.read_csv
9+
}
10+
11+
def _get_format(url):
12+
"""Gives back the format of the file from url
13+
14+
WARNING: It might break. Trying to find a better way.
15+
"""
16+
return url.split('.')[-1]
17+
18+
def _fetch(url):
19+
"""Downloads the file from the given url as a Pandas DataFrame
20+
21+
Parameters
22+
----------
23+
url : string
24+
URL of the file to be downloaded
25+
26+
Returns
27+
-------
28+
pd.DataFrame
29+
Data in the format of a DataFrame
30+
31+
Raises
32+
------
33+
NotImplementedError
34+
Raises when an unsupported file format is given as an URL
35+
"""
36+
try:
37+
ext = _get_format(url)
38+
reader = _PD_READERS[ext]
39+
df = reader(urlopen(url).read())
40+
except KeyError:
41+
raise NotImplementedError('File format not implemented')
42+
return df

0 commit comments

Comments
 (0)