Permalink
Browse files

support for py3

  • Loading branch information...
1 parent 582cb9a commit 14e8800c9cd752ef85b54de6bbe99f133019f56d arbatel@qq.com committed May 28, 2016
Showing with 33 additions and 25 deletions.
  1. +30 −23 test/nav_test.py
  2. +3 −2 tushare/fund/nav.py
View
@@ -18,80 +18,87 @@ def set_data(self):
def test_get_nav_open(self):
self.set_data()
lst = ['all', 'equity', 'mix', 'bond', 'monetary', 'qdii']
- print '\nget nav open................\n'
+ print('get nav open................\n')
for item in lst:
- print '\n=============\nget %s nav\n=============' % item
+ print('=============\nget %s nav\n=============' % item)
fund_df = nav.get_nav_open(item)
- print '\n', 'nums=', len(fund_df), '\n', fund_df[:self.disp]
+ print('\nnums=%d' % len(fund_df))
+ print(fund_df[:self.disp])
def test_get_nav_close(self):
self.set_data()
type2 = ['all', 'fbqy', 'fbzq']
qy_t3 = ['all', 'ct', 'cx']
zq_t3 = ['all', 'wj', 'jj', 'cz']
- print '\nget nav closed................\n'
+ print('\nget nav closed................\n')
fund_df = None
for item in type2:
if item == 'fbqy':
for t3i in qy_t3:
- print '\n=============\nget %s-%s nav\n=============' % (item, t3i)
+ print('\n=============\nget %s-%s nav\n=============' %
+ (item, t3i))
fund_df = nav.get_nav_close(item, t3i)
- print '\n', 'nums=', len(fund_df)
- print '\n', fund_df[:self.disp]
+ print('\nnums=%d' % len(fund_df))
+ print(fund_df[:self.disp])
elif item == 'fbzq':
for t3i in zq_t3:
- print '\n=============\nget %s-%s nav\n=============' % (item, t3i)
+ print('\n=============\nget %s-%s nav\n=============' %
+ (item, t3i))
fund_df = nav.get_nav_close(item, t3i)
- print '\n', 'nums=', len(fund_df),
- print '\n', fund_df[:self.disp]
+ print('\nnums=%d' % len(fund_df))
+ print(fund_df[:self.disp])
else:
- print '\n=============\nget %s nav\n=============' % item
+ print('\n=============\nget %s nav\n=============' % item)
fund_df = nav.get_nav_close(item)
- print '\n', 'nums=', len(fund_df), '\n', fund_df[:self.disp]
+ print('\nnums=%d' % len(fund_df))
+ print(fund_df[:self.disp])
def test_get_nav_grading(self):
self.set_data()
t2 = ['all', 'fjgs', 'fjgg']
t3 = {'all': '0', 'wjzq': '13', 'gp': '14',
'zs': '15', 'czzq': '16', 'jjzq': '17'}
- print '\nget nav grading................\n'
+ print('\nget nav grading................\n')
fund_df = None
for item in t2:
if item == 'all':
- print '\n=============\nget %s nav\n=============' % item
+ print('\n=============\nget %s nav\n=============' % item)
fund_df = nav.get_nav_grading(item)
- print '\n', 'nums=', len(fund_df), '\n', fund_df[:self.disp]
+ print('\nnums=%d' % len(fund_df))
+ print(fund_df[:self.disp])
else:
for t3i in t3.keys():
- print '\n=============\nget %s-%s nav\n=============' % (item, t3i)
+ print('\n=============\nget %s-%s nav\n=============' %
+ (item, t3i))
fund_df = nav.get_nav_grading(item, t3i)
- print '\n', 'nums=', len(fund_df)
- print '\n', fund_df[:self.disp]
+ print('\nnums=%d' % len(fund_df))
+ print(fund_df[:self.disp])
def test_nav_history(self):
self.set_data()
lst = ['164905', '161005', '380007', '000733', '159920', '164902',
'184721', '165519', '164302', '519749', '150275', '150305',
'150248']
for _, item in enumerate(lst):
- print '\n=============\nget %s nav\n=============' % item
+ print('\n=============\nget %s nav\n=============' % item)
fund_df = nav.get_nav_history(item, self.start, self.end)
if fund_df is not None:
- print '\n', 'nums=', len(fund_df), '\n', fund_df[:self.disp]
+ print('\nnums=%d' % len(fund_df))
+ print(fund_df[:self.disp])
def test_get_fund_info(self):
self.set_data()
lst = ['164905', '161005', '380007', '000733', '159920', '164902',
'184721', '165519', '164302', '519749', '150275', '150305',
'150248']
for item in lst:
- print '\n=============\nget %s nav\n=============' % item
+ print('\n=============\nget %s nav\n=============' % item)
fund_df = nav.get_fund_info(item)
if fund_df is not None:
- print '%s fund info' % item,
- print fund_df
+ print('%s fund info' % item)
+ print(fund_df)
if __name__ == '__main__':
unittest.main()
View
@@ -278,15 +278,16 @@ def _parse_fund_data(url, fund_type='open'):
text = urlopen(request, timeout=10).read()
if text == 'null':
return None
+ text = text.decode('gbk') if ct.PY3 else text
text = text.split('data:')[1].split(',exec_time')[0]
reg = re.compile(r'\,(.*?)\:')
- text = reg.sub(r',"\1":', text.decode('gbk') if ct.PY3 else text)
+ text = reg.sub(r',"\1":', text)
text = text.replace('"{symbol', '{"symbol')
text = text.replace('{symbol', '{"symbol"')
if ct.PY3:
jstr = json.dumps(text)
else:
- jstr = json.dumps(text, encoding='GBK')
+ jstr = json.dumps(text, encoding='gbk')
org_js = json.loads(jstr)
fund_df = pd.DataFrame(pd.read_json(org_js, dtype={'symbol': object}),
columns=ct.NAV_COLUMNS[fund_type])

0 comments on commit 14e8800

Please sign in to comment.