Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent circular import #323

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions quantlib/time/calendar.pxd
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
cimport quantlib.time._calendar as _calendar
from . cimport _calendar

from libcpp.vector cimport vector

cdef class Calendar:
cdef _calendar.Calendar _thisptr

cdef class TARGET(Calendar):
pass
3 changes: 1 addition & 2 deletions quantlib/time/calendars/poland.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from .. cimport _calendar as _calendar
from . cimport _poland
from .. calendar cimport Calendar
from ..calendar cimport Calendar

cdef class Poland(Calendar):
"""Poland calendars"""
Expand Down
4 changes: 4 additions & 0 deletions quantlib/time/calendars/target.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from ..calendar cimport Calendar

cdef class TARGET(Calendar):
pass
1 change: 0 additions & 1 deletion quantlib/time/calendars/target.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
cimport quantlib.time.calendars._target as _tg
from quantlib.time.calendar cimport Calendar

cdef class TARGET(Calendar):
'''TARGET calendar
Expand Down
2 changes: 1 addition & 1 deletion quantlib/time/daycounter.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cimport quantlib.time._daycounter as _daycounter
from . cimport _daycounter

cdef class DayCounter:

Expand Down
36 changes: 17 additions & 19 deletions quantlib/time/daycounter.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ from . cimport _daycounter
from . cimport _date

from .date cimport Date
from quantlib.time.daycounters.actual_actual cimport from_name as aa_from_name
from quantlib.time.daycounters.thirty360 cimport from_name as th_from_name
cimport quantlib.time.daycounters._simple as _simple

cdef class DayCounter:
'''This class provides methods for determining the length of a time
Expand Down Expand Up @@ -60,10 +57,8 @@ cdef class DayCounter:

@classmethod
def from_name(cls, name):
cdef DayCounter cnt = cls.__new__(cls)
name, convention = _get_daycounter_type_from_name(name)
cnt._thisptr = daycounter_from_name(name, convention)
return cnt
return daycounter_from_name(name, convention)

def _get_daycounter_type_from_name(name):
""" Returns a tuple (counter type, convention) from the DayCounter name. """
Expand All @@ -77,37 +72,40 @@ def _get_daycounter_type_from_name(name):
return (name, None)


cdef _daycounter.DayCounter* daycounter_from_name(str name, str convention) except NULL:
cdef DayCounter daycounter_from_name(str name, str convention):
""" Returns a new DayCounter pointer.

The QuantLib DayCounter don't have a copy constructor or any other easy
way to get copy of a given DayCounter. """

name_u = name.upper()

cdef _daycounter.DayCounter* cnt = NULL
from .daycounters.simple import Actual365Fixed, Actual360, OneDayCounter, SimpleDayCounter
from .daycounters.actual_actual import ActualActual, Convention as aaConvention
from .daycounters.thirty360 import Thirty360, Convention as thConvention
if name_u in ['ACTUAL360', 'ACTUAL/360', 'ACT/360']:
if convention == 'inc':
cnt = new _simple.Actual360(True)
else:
cnt = new _simple.Actual360()
return Actual360(convention == "inc")
elif name_u in ['ACTUAL365FIXED', 'ACTUAL/365', 'ACT/365']:
cnt = new _simple.Actual365Fixed()
return Actual365Fixed()
elif name_u == 'BUSINESS252':
raise ValueError(
'Business252 from name is not supported. Requires a calendar'
)
elif name_u in ['1/1', 'ONEDAYCOUNTER']:
cnt = new _simple.OneDayCounter()
return OneDayCounter()
elif name_u == 'SIMPLEDAYCOUNTER':
cnt = new _simple.SimpleDayCounter()
return SimpleDayCounter
elif name.startswith('Actual/Actual') or name.startswith('ACT/ACT') :
cnt = aa_from_name(convention)
try:
return ActualActual(aaConvention[convention])
except KeyError as e:
raise ValueError(str(e))
elif name == "30/360" or name == "30E/360":
if convention is None:
convention = 'BondBasis'
convention = convention.replace(" ", "")
cnt = th_from_name(convention)
try:
return Thirty360(thConvention[convention])
except KeyError as e:
raise ValueError(str(e))
else:
raise ValueError("Unkown day counter type: {}".format(name))
return cnt
3 changes: 0 additions & 3 deletions quantlib/time/daycounters/actual_actual.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
cimport quantlib.time._daycounter as _daycounter
from quantlib.time.daycounter cimport DayCounter


Expand All @@ -14,5 +13,3 @@ cdef extern from 'ql/time/daycounters/actualactual.hpp' namespace 'QuantLib::Act
Actual365
AFB
Euro

cdef _daycounter.DayCounter* from_name(str convention)
14 changes: 2 additions & 12 deletions quantlib/time/daycounters/actual_actual.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ For more details, refer to
https://www.isda.org/a/pIJEE/The-Actual-Actual-Day-Count-Fraction-1999.pdf
'''

from cython.operator cimport dereference as deref
cimport quantlib.time._daycounter as _daycounter
cimport quantlib.time.daycounters._actual_actual as _aa
from . cimport _actual_actual as _aa
from quantlib.time.schedule cimport Schedule
from quantlib.time.daycounter cimport DayCounter

cdef class ActualActual(DayCounter):

Expand All @@ -36,11 +33,4 @@ cdef class ActualActual(DayCounter):
For more details, refer to
https://www.isda.org/a/pIJEE/The-Actual-Actual-Day-Count-Fraction-1999.pdf
"""
self._thisptr = <_daycounter.DayCounter*> new \
_aa.ActualActual(convention, schedule._thisptr)

cdef _daycounter.DayCounter* from_name(str convention):

cdef Convention ql_convention = Convention[convention]

return new _aa.ActualActual(ql_convention)
self._thisptr = new _aa.ActualActual(convention, schedule._thisptr)
29 changes: 10 additions & 19 deletions quantlib/time/daycounters/simple.pyx
Original file line number Diff line number Diff line change
@@ -1,46 +1,37 @@
"""This module contains "simple" Daycounter classes, i.e. which do not depend on
a convention"""

from cython.operator cimport dereference as deref

cimport quantlib.time._daycounter as _daycounter
from . cimport _simple
from quantlib.time.daycounter cimport DayCounter
cimport quantlib.time.calendars._target as _tg
cimport quantlib.time._calendar as _calendar
from quantlib.time.calendars.target cimport TARGET
from quantlib.time.calendar cimport Calendar
from libcpp cimport bool

cdef class Actual365Fixed(DayCounter):

def __cinit__(self, *args):
self._thisptr = <_daycounter.DayCounter*> new _simple.Actual365Fixed()
def __cinit__(self):
self._thisptr = new _simple.Actual365Fixed()


cdef class Actual360(DayCounter):

def __cinit__(self, bool include_last_day = False):
self._thisptr = <_daycounter.DayCounter*> new _simple.Actual360(include_last_day)
self._thisptr = new _simple.Actual360(include_last_day)


cdef class Business252(DayCounter):

def __cinit__(self, *args, calendar=None):
cdef _calendar.Calendar cl
if calendar is None:
cl = _tg.TARGET()
else:
cl = (<Calendar>calendar)._thisptr
self._thisptr = <_daycounter.DayCounter*> new _simple.Business252(cl)
def __cinit__(self, Calendar calendar=TARGET()):
self._thisptr = new _simple.Business252(calendar._thisptr)


cdef class OneDayCounter(DayCounter):

def __cinit__(self, *args):
self._thisptr = <_daycounter.DayCounter*> new _simple.OneDayCounter()
def __cinit__(self):
self._thisptr = new _simple.OneDayCounter()


cdef class SimpleDayCounter(DayCounter):

def __cinit__(self, *args):
self._thisptr = <_daycounter.DayCounter*> new _simple.SimpleDayCounter()
def __cinit__(self):
self._thisptr = new _simple.SimpleDayCounter()
3 changes: 0 additions & 3 deletions quantlib/time/daycounters/thirty360.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
cimport quantlib.time._daycounter as _daycounter
from quantlib.time.daycounter cimport DayCounter

cdef class Thirty360(DayCounter):
Expand All @@ -15,5 +14,3 @@ cdef extern from 'ql/time/daycounters/thirty360.hpp' namespace 'QuantLib::Thirty
ISMA
ISDA
NASD

cdef _daycounter.DayCounter* from_name(str convention) except NULL
6 changes: 0 additions & 6 deletions quantlib/time/daycounters/thirty360.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,3 @@ cdef class Thirty360(DayCounter):

def __cinit__(self, Convention convention=Convention.BondBasis, Date termination_date=Date()):
self._thisptr = new _th.Thirty360(convention, deref(termination_date._thisptr))

cdef _daycounter.DayCounter* from_name(str convention) except NULL:
try:
return new _th.Thirty360(Convention[convention])
except KeyError:
raise ValueError("Unknown convention: {}".format(convention))
Loading