Repository navigation
<
8000
/div>
Expand file tree
/
Copy pathutils.py
More file actions
149 lines (122 loc) · 4.16 KB
/
Copy pathutils.py
File metadata and controls
149 lines (122 loc) · 4.16 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import re
import os
import unicodedata
import ConfigParser
from decimal import Decimal
from datetime import date, datetime
from bson import ObjectId
from pandas import tslib, date_range, concat, DataFrame
from settings import PROJECT_PATH
def fix_type(value):
if type(value) is str:
try:
return value.decode('utf-8')
except UnicodeDecodeError:
return value.decode('latin1')
elif type(value) is tslib.Timestamp:
try:
return value.strftime("%Y-%m-%d %H:%M:%S")
except ValueError:
return datetime(1900, 01, 01, 00, 00, 00).strftime()
elif type(value) is date or type(value) is datetime:
try:
return value.strftime("%Y-%m-%d")
except ValueError:
return datetime(1900, 01, 01).strftime()
elif type(value) is Decimal:
return float(value)
return value
def fix_render(_l):
return dict(map(lambda (k, v): (k, fix_type(v)), _l.iteritems()))
def slugfy(text):
slug = unicodedata.normalize("NFKD", text).encode("UTF-8", "ignore")
slug = re.sub(r"[^\w]+", " ", slug)
slug = "-".join(slug.lower().strip().split())
if not slug:
return None
return slug
def df_generate(df, value, str_field):
s = str_field.split('__')
field = s[1]
try:
operator = s[2]
except:
operator = "is"
try:
t = s[3]
if t == "int" and operator not in ["in", "notin", "between"]:
value = int(value)
except:
t = "str"
if t == "date":
try:
mark = s[4].replace(":", "%")
except:
mark = "%Y-%m-%d"
if operator == "gte":
return u"{} >= {}".format(field, value)
elif operator == "lte":
return u"{} <= {}".format(field, value)
elif operator == "is":
if t == 'int':
return u"{} == {}".format(field, value)
return u"{} == '{}'".format(field, value)
elif operator == "in":
if t == 'int':
return u"{} in {}".format(field,
[int(i) for i in value.split(',')])
return u"{} in {}".format(field, [i for i in value.split(',')])
elif operator == "notin":
if t == 'int':
return u"{} not in {}".format([int(i) for i in value.split(',')],
field)
return u"{} not in {}".format([i for i in value.split(',')], field)
elif operator == "between":
_range = []
between = value.split(":")
if t == "date":
_range = [i.strftime(mark)
for i in date_range(between[0], between[1]).tolist()]
elif t == "int":
_range = [i for i in xrange(int(between[0]), int(between[1]) + 1)]
return u"{} in {}".format(field, _range)
def conf(section):
config = ConfigParser.ConfigParser()
config.read(os.path.join(PROJECT_PATH, 'mining.ini'))
_dict = {}
options = config.options(section)
for option in options:
try:
_dict[option] = config.get(section, option)
except:
_dict[option] = None
if 'sql_conn_params' in options:
import ast
_dict['sql_conn_params'] = ast.literal_eval(_dict['sql_conn_params'])
else:
_dict['sql_conn_params'] = {}
return _dict
def log_it(s, name=u"core"):
with open("/tmp/openmining-{}.log".format(name), "a") as log:
msg = u"{} => {}\n".format(datetime.now(), s)
log.write(msg.encode('utf-8'))
def parse_dumps(obj):
if isinstance(obj, datetime):
return str(obj.strftime("%Y-%m-%d %H:%M:%S"))
if isinstance(obj, ObjectId):
return str(obj)
return json.JSONEncoder.default(obj)
def DataFrameSearchColumn(df, colName, value, operator='like'):
ndf = DataFrame()
for idx, record in df[colName].iteritems():
check = True
if operator == 'like' and value in str(record):
check = True
if operator == 'regex' and re.search(value, str(record)):
check = True
if check:
ndf = concat([df[df[colName] == record], ndf], ignore_index=True)
return ndf