Created
November 19, 2015 20:22
-
-
Save diegoguimaraes/61a3e44c40cc45016cda to your computer and use it in GitHub Desktop.
Period Collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
period_collection = { | |
"force": 0, | |
"data": [ | |
{ | |
"id_contract": 34, | |
"initial_date": "2015-10-10", | |
"end_date": "2015-10-20", | |
"active": 1, | |
"days_of_week": [5,6,7] | |
}, | |
{ | |
"id_contract": 34, | |
"initial_date": "2015-10-10", | |
"end_date": "2015-10-20", | |
"active": 1, | |
"days_of_week": [1,2,3] | |
}, | |
{ | |
"id_contract": 34, | |
"initial_date": "2015-10-15", | |
"end_date": "2015-10-16", | |
"active": 1, | |
"days_of_week": [5,6,7] | |
}, | |
] | |
} | |
class Period: | |
def __init__(self, data): | |
for k,v in data.items(): | |
setattr(self, k, v) | |
self.initial_date = datetime.strptime(self.initial_date, '%Y-%m-%d').date() | |
self.end_date = datetime.strptime(self.end_date, '%Y-%m-%d').date() | |
def __eq__(self, other): | |
""" | |
Dois objetos iguais sao dois objetos conflitantes | |
""" | |
if self.initial_date == other.initial_date\ | |
and self.initial_date >= other.initial_date\ | |
and self.initial_date <= other.end_date\ | |
and self._days_of_week_conflict(other): | |
return True | |
if self.end_date < other.initial_date\ | |
and self.end_date > other.end_date\ | |
and self._days_of_week_conflict(other): | |
return True | |
return False | |
def _days_of_week_conflict(self, other): | |
if set.intersection(set(self.days_of_week), set(other.days_of_week)): | |
return True | |
return False | |
def __repr__(self): | |
return '<{} {} -> {} {}>'.format( | |
self.__class__.__name__, | |
self.initial_date, | |
self.end_date, self.days_of_week) | |
class PeriodCollection: | |
def __init__(self, periods): | |
self.collection = [Period(data) for data in periods['data']] | |
def __repr__(self): | |
return '{}-{}'.format(self.__class__.__name__, len(self.collection)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment