-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbase.py
99 lines (74 loc) · 2.35 KB
/
base.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
# -*- coding:utf-8 -*-
import pandas as pd
import sys
import requests
from requests.adapters import HTTPAdapter
from urllib3.util import Retry
import gugu.config as cf
class Base():
def __init__(self, pandas=True, inter=True):
self.__pandas = False if not pandas else True
self._PY3 = (sys.version_info[0] >= 3)
self._inter = False if not inter else True # 交互模式
self._session = requests.Session()
retry = Retry(connect=5, backoff_factor=1)
adapter = HTTPAdapter(max_retries=retry)
self._session.mount('http://', adapter)
self._session.mount('https://', adapter)
self._session.keep_alive = False
self._data = pd.DataFrame()
def _result(self):
"""
返回结果:使用pandas时返回DataFrame否则返回list
Parameters
-----
dataframe:pandas.DataFrame
index:string
返回dict时选用的键 e.g. code
return
-----
pandas.DataFrame or dict
"""
if self._data.empty:
return None
elif self.__pandas:
return self._data
else:
return self._data.to_dict('records')
def output(self, full=False):
print('')
if not full:
print(self._data)
else:
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('max_colwidth',100)
print(self._data)
def getPandas(self):
return self.__pandas
def setPandas(self, pandas=False):
self.__pandas = pandas
def getInter(self):
"""
获取交互模式状态
return
------
True or False
"""
return self._inter
def setInter(self, inter=False):
"""
设置交互模式
Parameters
------
inter: bool
"""
self._inter = inter
def _writeHead(self):
if self._inter:
sys.stdout.write(cf.GETTING_TIPS)
sys.stdout.flush()
def _writeConsole(self):
if self._inter:
sys.stdout.write(cf.GETTING_FLAG)
sys.stdout.flush()