Skip to content

Commit 19370fd

Browse files
author
wegamekinglc
committed
small internal api change
1 parent bcd6ec7 commit 19370fd

File tree

7 files changed

+25
-34
lines changed

7 files changed

+25
-34
lines changed

PyFin/DateUtilities/Calendar.pxd

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Created on 2015-2-20
66
"""
77

88
from PyFin.DateUtilities.Date cimport Date
9+
from PyFin.DateUtilities.Period cimport Period
910

1011

1112
cdef class CalendarImpl(object):
@@ -26,6 +27,6 @@ cdef class Calendar(object):
2627
cpdef endOfMonth(self, Date d)
2728
cpdef bizDaysBetween(self, Date fromDate, Date toDate, bint includeFirst=*, bint includeLast=*)
2829
cpdef adjustDate(self, Date d, int c=*)
29-
cpdef advanceDate(self, Date d, period, int c=*, bint endOfMonth=*)
30+
cpdef advanceDate(self, Date d, Period period, int c=*, bint endOfMonth=*)
3031
cpdef holDatesList(self, Date fromDate, Date toDate, bint includeWeekEnds=*)
3132
cpdef bizDatesList(self, Date fromDate, Date toDate)

PyFin/DateUtilities/Calendar.pyx

+5-18
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ Created on 2017-2-1
55
@author: cheng.li
66
"""
77

8-
import six
98
from PyFin.Enums._Weekdays cimport Weekdays
109
from PyFin.Enums._TimeUnits cimport TimeUnits
1110
from PyFin.Enums._Months cimport Months
1211
from PyFin.Enums._BizDayConventions cimport BizDayConventions
1312
from PyFin.DateUtilities.Date cimport Date
1413
from PyFin.DateUtilities.Period cimport Period
15-
from PyFin.Utilities.Asserts cimport pyFinAssert
1614

1715

1816
cdef class Calendar(object):
@@ -105,20 +103,14 @@ cdef class Calendar(object):
105103
raise ValueError("unknown business-day convention")
106104
return d1
107105

108-
cpdef advanceDate(self, Date d, period, int c=BizDayConventions.Following, bint endOfMonth=False):
106+
cpdef advanceDate(self, Date d, Period period, int c=BizDayConventions.Following, bint endOfMonth=False):
109107

110108
cdef int n
111109
cdef int units
112110
cdef Date d1
113-
cdef Period pobj
114111

115-
if isinstance(period, six.string_types):
116-
pobj = Period(period)
117-
else:
118-
pobj = period
119-
120-
n = pobj.length()
121-
units = pobj.units()
112+
n = period.length()
113+
units = period.units()
122114

123115
if n == 0:
124116
return self.adjustDate(d, c)
@@ -138,10 +130,10 @@ cdef class Calendar(object):
138130
n += 1
139131
return d1
140132
elif units == TimeUnits.Days or units == TimeUnits.Weeks:
141-
d1 = d + pobj
133+
d1 = d + period
142134
return self.adjustDate(d1, c)
143135
else:
144-
d1 = d + pobj
136+
d1 = d + period
145137
if endOfMonth and self.isEndOfMonth(d):
146138
return self.endOfMonth(d1)
147139
return self.adjustDate(d1, c)
@@ -150,8 +142,6 @@ cdef class Calendar(object):
150142
cdef list result = []
151143
cdef Date d = fromDate
152144

153-
pyFinAssert(fromDate <= toDate, ValueError, "from date ({0} must be earlier than to date {1}"
154-
.format(fromDate, toDate))
155145
while d <= toDate:
156146
if self.isHoliday(d) and (includeWeekEnds or not self.isWeekEnd(d.weekday())):
157147
result.append(d)
@@ -162,9 +152,6 @@ cdef class Calendar(object):
162152
cdef list result = []
163153
cdef Date d = fromDate
164154

165-
pyFinAssert(fromDate <= toDate, ValueError, "from date ({0} must be earlier than to date {1}"
166-
.format(fromDate, toDate))
167-
168155
while d <= toDate:
169156
if self.isBizDay(d):
170157
result.append(d)

PyFin/DateUtilities/Date.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ cdef Date _advance(Date date, int n, int units):
7878
cdef class Date(object):
7979

8080
@cython.cdivision(True)
81-
def __init__(self, year=None, month=None, day=None, serialNumber=None):
81+
def __init__(self, int year=0, int month=0, int day=0, int serialNumber=0):
8282
cdef int leap
8383
cdef int y
8484
cdef int m
@@ -102,9 +102,9 @@ cdef class Date(object):
102102
self._month = m
103103
self._day = d - _monthOffset(m, leap)
104104
return
105-
elif serialNumber is not None and (year is not None or month is not None or day is not None):
105+
elif serialNumber and (year or month or day):
106106
raise ValueError("When serial number is offered, no year or month or day number should be entered")
107-
elif year is None or month is None or day is None:
107+
elif not (year and month and day):
108108
raise ValueError("year: {0}, month: {1}, day: {2} can't be null value included".format(year, month, day))
109109

110110
self._calculate_date(year, month, day)

PyFin/api/DateUtilities.py

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def adjustDateByCalendar(holidayCenter, referenceDate, convention=BizDayConventi
5454
def advanceDateByCalendar(holidayCenter, referenceDate, period, convention=BizDayConventions.Following):
5555
cal = Calendar(holidayCenter)
5656
refer = check_date(referenceDate)
57+
period = Period(period)
5758
return cal.advanceDate(refer, period, convention).toDateTime()
5859

5960

PyFin/tests/DateUtilities/testCalendar.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import pickle
1313
from PyFin.DateUtilities import Date
1414
from PyFin.DateUtilities import Calendar
15+
from PyFin.DateUtilities import Period
1516
from PyFin.Enums import BizDayConventions
1617
from PyFin.Enums import Months
1718
from PyFin.Enums import Weekdays
@@ -165,20 +166,20 @@ def testAdvanceDate(self):
165166
bizDayConv = BizDayConventions.Following
166167

167168
# test null period
168-
self.assertEqual(sseCal.advanceDate(referenceDate, '0b', bizDayConv), Date(2014, 2, 7))
169+
self.assertEqual(sseCal.advanceDate(referenceDate, Period('0b'), bizDayConv), Date(2014, 2, 7))
169170

170171
# test negative period
171-
self.assertEqual(sseCal.advanceDate(referenceDate, '-5b', bizDayConv), Date(2014, 1, 24))
172+
self.assertEqual(sseCal.advanceDate(referenceDate, Period('-5b'), bizDayConv), Date(2014, 1, 24))
172173

173174
# The difference is caused by Feb 8 is SSE holiday but a working day for IB market
174-
self.assertEqual(sseCal.advanceDate(referenceDate, '2b', bizDayConv), Date(2014, 2, 10))
175-
self.assertEqual(sseCal.advanceDate(referenceDate, '2d', bizDayConv), Date(2014, 2, 7))
176-
self.assertEqual(ibCal.advanceDate(referenceDate, '2b', bizDayConv), Date(2014, 2, 8))
177-
self.assertEqual(ibCal.advanceDate(referenceDate, '2d', bizDayConv), Date(2014, 2, 7))
175+
self.assertEqual(sseCal.advanceDate(referenceDate, Period('2b'), bizDayConv), Date(2014, 2, 10))
176+
self.assertEqual(sseCal.advanceDate(referenceDate, Period('2d'), bizDayConv), Date(2014, 2, 7))
177+
self.assertEqual(ibCal.advanceDate(referenceDate, Period('2b'), bizDayConv), Date(2014, 2, 8))
178+
self.assertEqual(ibCal.advanceDate(referenceDate, Period('2d'), bizDayConv), Date(2014, 2, 7))
178179

179180
bizDayConv = BizDayConventions.ModifiedFollowing
180181
# May 31, 2014 is a holiday
181-
self.assertEqual(sseCal.advanceDate(referenceDate, '4m', bizDayConv, True), Date(2014, 5, 30))
182+
self.assertEqual(sseCal.advanceDate(referenceDate, Period('4m'), bizDayConv, True), Date(2014, 5, 30))
182183

183184
def testDatesList(self):
184185

PyFin/tests/DateUtilities/testDate.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def testDateInputWithoutCompleteInformationOnYearMonthDay(self):
3030
year = 2015
3131
month = None
3232
day = 18
33-
with self.assertRaises(ValueError):
33+
with self.assertRaises(TypeError):
3434
_ = Date(year=year, month=month, day=day)
3535

3636
def testBasicFunctions(self):

PyFin/tests/api/testDateUtilities.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import datetime as dt
1010
from PyFin.DateUtilities.Date import Date
1111
from PyFin.DateUtilities.Calendar import Calendar
12+
from PyFin.DateUtilities.Period import Period
1213
from PyFin.Enums.BizDayConventions import BizDayConventions
1314
from PyFin.api.DateUtilities import datesList
1415
from PyFin.api.DateUtilities import isBizDay
@@ -100,17 +101,17 @@ def testAdvanceDateByCalendar(self):
100101
cal = Calendar('China.SSE')
101102

102103
expected = advanceDateByCalendar('China.SSE', referenceDate.toDateTime(), '2D')
103-
calculated = cal.advanceDate(referenceDate, '2D')
104+
calculated = cal.advanceDate(referenceDate, Period('2D'))
104105
self.assertEqual(expected, calculated.toDateTime())
105106

106107
expected = advanceDateByCalendar('China.SSE', referenceDate.toDateTime(), '2D')
107-
calculated = cal.advanceDate(referenceDate, '2D')
108+
calculated = cal.advanceDate(referenceDate, Period('2D'))
108109
self.assertEqual(expected, calculated.toDateTime())
109110

110111
expected = advanceDateByCalendar('China.SSE', referenceDate.toDateTime(), '2B')
111-
calculated = cal.advanceDate(referenceDate, '2B')
112+
calculated = cal.advanceDate(referenceDate, Period('2B'))
112113
self.assertEqual(expected, calculated.toDateTime())
113114

114115
expected = advanceDateByCalendar('China.SSE', referenceDate.toDateTime(), '1Y')
115-
calculated = cal.advanceDate(referenceDate, '1Y')
116+
calculated = cal.advanceDate(referenceDate, Period('1Y'))
116117
self.assertEqual(expected, calculated.toDateTime())

0 commit comments

Comments
 (0)