Skip to content

Commit

Permalink
Fix TPEX date asterisk (fixed #37) (#38)
Browse files Browse the repository at this point in the history
* Ignore * suffix in date from TPEX

* Add unit test for *
  • Loading branch information
ianlini authored and mlouielu committed Mar 26, 2018
1 parent fbc780d commit 082126a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
14 changes: 14 additions & 0 deletions test/test_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ def test_make_datatuple_without_prices(self):
self.assertEqual(dt.change, 0.0)
self.assertEqual(dt.transaction, 15718)

def test_make_datatuple_with_asterisk(self):
data = ['106/05/02*', '45,851', '9,053,856', '198.50',
'199.00', '195.50', '196.50', '2.00', '15,718']
dt = self.fetcher._make_datatuple(data)
self.assertEqual(dt.date, datetime.datetime(2017, 5, 2))
self.assertEqual(dt.capacity, 45851000)
self.assertEqual(dt.turnover, 9053856000)
self.assertEqual(dt.open, 198.5)
self.assertEqual(dt.high, 199.0)
self.assertEqual(dt.low, 195.5)
self.assertEqual(dt.close, 196.5)
self.assertEqual(dt.change, 2.0)
self.assertEqual(dt.transaction, 15718)


class StockTest(object):
def test_fetch_31(self):
Expand Down
3 changes: 2 additions & 1 deletion twstock/stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ def _convert_date(self, date):
return '/'.join([str(int(date.split('/')[0]) + 1911)] + date.split('/')[1:])

def _make_datatuple(self, data):
data[0] = datetime.datetime.strptime(self._convert_date(data[0]), '%Y/%m/%d')
data[0] = datetime.datetime.strptime(self._convert_date(data[0].replace('*', '')),
'%Y/%m/%d')
data[1] = int(data[1].replace(',', '')) * 1000
data[2] = int(data[2].replace(',', '')) * 1000
data[3] = None if data[3] == '--' else float(data[3].replace(',', ''))
Expand Down

0 comments on commit 082126a

Please sign in to comment.