forked from ferventdesert/etlpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extends.py
128 lines (92 loc) · 2.43 KB
/
extends.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
# encoding: UTF-8
import re;
spacere = re.compile("[ ]{2,}");
spacern = re.compile("(^\r\n?)|(\r\n?$)")
def getkeys(generator):
count=0;
s=set();
for r in generator:
s=s|r.keys();
count+=1;
if count>=20:
return list(s);
return list(s)
def ReplaceLongSpace(txt):
r = spacere.subn(' ', txt)[0]
r = spacern.subn('', r)[0]
return r;
def Merge(d1, d2):
for r in d2:
d1[r] = d2[r];
return d1;
def MergeQuery(d1, d2, columns):
if isinstance(columns, str) and columns.strip() != "":
columns = columns.split(' ');
for r in columns:
if r in d2:
d1[r] = d2[r];
return d1;
def Query(data, key):
if data is None:
return key;
if isinstance(key, str) and key.startswith('[') and key.endswith(']'):
key = key[1:-1];
return data[key];
return key;
def findany(iteral, func):
for r in iteral:
if func(r):
return True;
return False;
def getindex(iteral, func):
for r in range(len(iteral)):
if func(iteral[r]):
return r;
return -1;
def Cross(a, genefunc):
for r1 in a:
for r2 in genefunc(r1):
for key in r2:
r1[key] = r2[key]
yield r1;
def MergeAll(a, b):
while True:
t1 = a.__next__()
if t1 is None:
return;
t2 = b.__next__()
if t2 is not None:
for t in t2:
t1[t] = t2[t];
yield t1;
def Append(a, b):
for r in a:
yield r;
for r in b:
yield r;
def get_type_name(obj):
s=str(obj.__class__);
p=s.find('.');
r= s[p+1:].split('\'')[0]
return r;
class EObject(object):
pass;
def convert_to_builtin_type(obj):
d= { key:value for key,value in obj.__dict__.items() if isinstance(value,(str,int,float,list,dict,tuple,EObject) or value is None)};
return d
def dict_to_poco_type(obj):
if isinstance(obj,dict):
result= EObject();
for key in obj:
v= obj[key]
setattr(result,key,dict_to_poco_type(v))
return result
elif isinstance(obj,list):
for i in range(len(obj)):
obj[i]=dict_to_poco_type(obj[i]);
return obj;
def dict_copy_poco(obj,dic):
for key,value in obj.__dict__.items():
if key in dic:
if isinstance(dic[key], (str,int,float)):
setattr(obj,key,dic[key])