-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.py
131 lines (108 loc) · 3.9 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2015 jaidev <jaidev@newton>
#
# Distributed under terms of the BSD 3-clause license.
"""
Misecellaneous bells and whistles.
"""
import sys
import json
import pandas as pd
import numpy as np
import datetime
DATA_TYPES = {'String': str, 'Date/Time': datetime.date, 'Float': float,
'Integer': int}
class TypeEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, type):
return str(obj)
elif isinstance(obj, set):
return list(obj)
elif callable(obj):
return ".".join((obj.__module__, obj.__name__))
elif isinstance(obj, np.ndarray):
return np.array_str(obj)
else:
if "Engine" in str(obj):
return str(obj)
return json.JSONEncoder.default(self, obj)
def generate_questionnaire(filepath):
"""Generate a questionnaire for data at `filepath`.
This questionnaire will be presented to the client, which helps us
automatically generate the schema.
:param filepath: Path to the file that needs to be ingested.
:type filepath: str
:return: A dictionary of questions and their possible answers. The format
of the dictionary is such that every key is a question to be put to the
client, and its value is a list of possible answers. The first item in the
list is the default value.
:rtype: dict
"""
qdict = {}
if filepath.endswith(".tsv"):
dataframe = pd.read_table(filepath)
else:
dataframe = pd.read_csv(filepath)
for col in dataframe.columns:
qstring = "What is the data type of {}?".format(col)
if "float" in str(dataframe[col].dtype).lower():
defaultType = "Float"
elif "object" in str(dataframe[col].dtype).lower():
defaultType = "String"
elif "int" in str(dataframe[col].dtype).lower():
defaultType = "Integer"
typeslist = DATA_TYPES.keys()
typeslist.remove(defaultType)
typeslist = [defaultType] + typeslist
qdict[qstring] = typeslist
return qdict
def colnames(filename, parser=None, **kwargs):
"""
Read the column names of a delimited file, without actually reading the
whole file. This is simply a wrapper around `pandas.read_csv`, which reads
only one row and returns the column names.
:param filename: Path to the file to be read
:param kwargs: Arguments to be passed to the `pandas.read_csv`
:type filename: str
:rtype: list
:Example:
Suppose we want to see the column names of the Fisher iris dataset.
>>> colnames("/path/to/iris.csv")
['Sepal Length', 'Petal Length', 'Sepal Width', 'Petal Width', 'Species']
"""
if 'nrows' in kwargs:
UserWarning("The nrows parameter is pointless here. This function only"
"reads one row.")
kwargs.pop('nrows')
if parser is None:
if "sep" in kwargs:
sep = kwargs.get('sep')
if sep == r"\t":
parser = pd.read_table
kwargs.pop('sep')
else:
parser = pd.read_csv
elif filename.endswith('.tsv'):
parser = pd.read_table
else:
parser = pd.read_csv
return parser(filename, nrows=1, **kwargs).columns.tolist()
def get_md5_checksum(filepath):
"""Get the md5 checksum of a file.
:param filepath: Path to the file of which to calculate the md5 checksum.
:type filepath: Str
:return: MD5 checksum of the file.
:rtype: Str
:Example:
>>> get_md5_checksum('pysemantic/tests/testdata/iris.csv')
'9b3ecf3031979169c0ecc5e03cfe20a6'
"""
import subprocess
if sys.platform == "darwin":
cmd = "md5 -q {}".format(filepath).split()
else:
cmd = "md5sum {}".format(filepath).split()
return subprocess.check_output(cmd).rstrip().split()[0]