Skip to content

Commit 68c9ece

Browse files
committed
simplify encoding/decoding of strings
1 parent 89ceda2 commit 68c9ece

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

quantlib/time/date.pyx

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# cython: c_string_type=unicode, c_string_encoding=utf8
12
# Cython imports
23
from cython.operator cimport dereference as deref
34
from cpython.datetime cimport date, date_new, datetime_new, datetime, import_datetime, date_year, date_month, date_day, datetime_year, datetime_month, datetime_day, datetime_hour, datetime_minute, datetime_second, datetime_microsecond, PyDate_Check, PyDateTime_Check
@@ -19,7 +20,6 @@ from enum import IntEnum
1920

2021
# Python imports
2122
import_datetime()
22-
import six
2323

2424
cpdef enum Month:
2525
January = _date.January
@@ -91,17 +91,16 @@ cdef class Period:
9191
9292
'''
9393
def __init__(self, *args):
94-
cdef int tu
94+
cdef string tenor
9595
if len(args) == 1:
96-
tenor = args[0]
97-
if(isinstance(tenor, six.string_types)):
98-
self._thisptr.reset(new QlPeriod(parse(tenor.encode('utf-8'))))
96+
if isinstance(args[0], str):
97+
tenor = <string>args[0]
98+
self._thisptr.reset(new QlPeriod(parse(tenor)))
9999
else:
100100
self._thisptr.reset(new QlPeriod(<_period.Frequency>args[0]))
101101
elif len(args) == 2:
102-
tu = <int>args[1]
103-
self._thisptr.reset(new QlPeriod(<Integer> args[0],
104-
<_period.TimeUnit>tu))
102+
self._thisptr.reset(new QlPeriod(<Integer>args[0],
103+
<_period.TimeUnit><int>args[1]))
105104
elif len(args) == 0:
106105
self._thisptr.reset(new QlPeriod())
107106
else:
@@ -125,17 +124,17 @@ cdef class Period:
125124

126125
def __sub__(self, value):
127126
cdef QlPeriod outp
128-
outp = deref((<Period?>self)._thisptr) - deref((<Period?>value)._thisptr)
127+
outp = deref(self._thisptr) - deref((<Period?>value)._thisptr)
129128
return period_from_qlperiod(outp)
130129

131130
def __neg__(self):
132131
cdef QlPeriod outp
133-
outp = unary_minus(deref((<Period>self)._thisptr))
132+
outp = unary_minus(deref(self._thisptr))
134133
return period_from_qlperiod(outp)
135134

136135
def __add__(self, value):
137136
cdef QlPeriod outp
138-
outp = deref( (<Period?>self)._thisptr) + \
137+
outp = deref(self._thisptr) + \
139138
deref( (<Period?>value)._thisptr)
140139
return period_from_qlperiod(outp)
141140

@@ -186,7 +185,7 @@ cdef class Period:
186185
return NotImplemented
187186

188187
def __richcmp__(self, value, int t):
189-
cdef QlPeriod p1 = deref((<Period?>self)._thisptr)
188+
cdef QlPeriod p1 = deref(self._thisptr)
190189
cdef QlPeriod p2 = deref((<Period?>value)._thisptr)
191190

192191
if t==0:
@@ -205,12 +204,12 @@ cdef class Period:
205204
def __str__(self):
206205
cdef _period.stringstream ss
207206
ss << _period.long_period(deref(self._thisptr))
208-
return ss.str().decode()
207+
return ss.str()
209208

210209
def __repr__(self):
211210
cdef _period.stringstream ss
212-
ss << string(b"Period('") << _period.short_period(deref(self._thisptr)) << string(b"')")
213-
return ss.str().decode()
211+
ss << <string>b"Period('" << _period.short_period(deref(self._thisptr)) << <string>b"')"
212+
return ss.str()
214213

215214
def __float__(self):
216215
""" Converts the period to a year fraction.
@@ -331,17 +330,17 @@ cdef class Date:
331330
def __str__(self):
332331
cdef _date.stringstream ss
333332
ss << _date.short_date(self._thisptr)
334-
return ss.str().decode()
333+
return ss.str()
335334

336335
def __repr__(self):
337336
cdef _date.stringstream ss
338-
ss << string(b"Date('") << _date.iso_datetime(self._thisptr) << string(b"')")
339-
return ss.str().decode()
337+
ss << <string>b"Date(" << _date.iso_datetime(self._thisptr) << <string>b")"
338+
return ss.str()
340339

341-
def __format__(self, str fmt):
340+
def __format__(self, str fmt=""):
342341
cdef _date.stringstream ss
343-
ss << _date.formatted_date(self._thisptr, fmt.encode())
344-
return ss.str().decode()
342+
ss << _date.formatted_date(self._thisptr, fmt)
343+
return ss.str()
345344

346345
def __hash__(self):
347346
# Returns a hash based on the serial
@@ -438,9 +437,9 @@ cdef class Date:
438437
def from_string(cls, str s, str fmt=None):
439438
cdef Date instance = Date.__new__(Date)
440439
if fmt is None:
441-
instance._thisptr = _date.parseISO(s.encode())
440+
instance._thisptr = _date.parseISO(s)
442441
else:
443-
instance._thisptr = _date.parseFormatted(s.encode(), fmt.encode())
442+
instance._thisptr = _date.parseFormatted(s, fmt)
444443
return instance
445444

446445
def today():

0 commit comments

Comments
 (0)