def __len__(self): ''' Returns the length of the current list of activities. :return: integer - length of the current list of activities ''' self.__list[:] = sort(self.__list, key=lambda x: str(x)) return len(self.__list)
def __iter__(self): ''' Returns an iterator object for the repository. :return: iter(list) - iterator object ''' self.__list[:] = sort(self.__list, key=lambda x: str(x)) return iter(self.__list)
def __len__(self): ''' Returns the length of the current list of people. :return: integer - length of the current list of people ''' self.__list[:] = sort(self.__list, key=lambda x: x.id) return len(self.__list)
def activities_for_person_by_date(self, id): ''' Returns a list of activities performed by the person with the indicated ID by date. :param id: positive integer - person's ID :return: list of Activity as specified :exception NABException: if one of the parameters is not valid ''' funct = lambda x: x.person.id == id L = filter(funct, self.__activityRepo) return sort(L, key=lambda x: x.date)
def add(self, activity): ''' Adds an activity to the current list of activities. :param activity: Activity - activity to be added :return: activity is added to the current list of activities :exception NABException: if one of the parameters is not valid ''' if type(activity) is not Activity: raise NABException("The given activity is not valid.") self.__list.append(activity) self.__list[:] = sort(self.__list, key=lambda x: str(x))
def add(self, person): ''' Adds a person to the current list of people, if his ID does not already exist. :param person: Person - person to be added :return: person is added to the current list of people :exception NABException: if one of the parameters is not valid or a person with that ID already exists ''' if type(person) is not Person: raise NABException("The given person is not valid.") if self.find(person.id) != -1: raise NABException("There's already a person with that ID.") self.__list.append(person) self.__list[:] = sort(self.__list, key=lambda x: x.id)
def activities_in_interval_alphabetically(self, date1, date2): ''' Returns a list of activities performed in the indicated interval alphabetically by description. :param date1: string - start date in a valid format :param date1: string - end date in a valid format :return: list of Activity as specified :exception NABException: if one of the parameters is not valid ''' if date1 > date2: raise NABException("End date must be after the start date.") if not ActivityValidator.valid_date(date1) or not ActivityValidator.valid_date(date2): raise NABException("At least one of the dates is invalid.") funct = lambda x: date1 <= x.date and x.date <= date2 L = filter(funct, self.__activityRepo) return sort(L, key=lambda x: x.description)
def people_with_activities_in_interval(self, date1, date2): ''' Returns a list of people who perform activities in the indicated interval. :param date1: string - start date in a valid format :param date1: string - end date in a valid format :return: list of Person as specified :exception NABException: if one of the parameters is not valid ''' if date1 > date2: raise NABException("End date must be after the start date.") if not ActivityValidator.valid_date(date1) or not ActivityValidator.valid_date(date2): raise NABException("At least one of the dates is invalid.") funct = lambda x: len([a for a in self.__activityRepo if a.person == x and date1 <= a.date and a.date <= date2]) != 0 L = filter(funct, self.__personRepo) return sort(L, key=lambda x: x.id)