<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import calendar
from datetime import date
from datetime import timedelta

from DateRanger.utils import get_quarter
from DateRanger.utils import get_monthrange
from DateRanger.objects import DateRange
from DateRanger.exceptions import InvalidDateRange

class DateRanger(object):

    """
    A class for getting common bussiness date ranges.
    """

    def __init__(self, base_date=None):
        """
        Argus:
            base_date - the base day. Example: date(2009, 11, 1)
        """
        self.base_date = base_date or date.today()

    def set_base_date(self, base_date):
        """
        Set base date.

        Argus:
            base_date - Example: date(2009, 11, 1)
        """
        self.base_date = base_date

    def base_day(self):
        """
        Get the DateRange of self.base_date.
        """
        return DateRange(self.base_date, self.base_date + timedelta(days=1))

    def prev_day(self, days=1):
        """
        Get the DateRange that n days before self.base_date.

        Argus:
            days - n days ago
        """
        prev_date = self.base_date - timedelta(days=days)
        return DateRange(prev_date, prev_date + timedelta(days=1))

    def next_day(self, days=1):
        """
        Get the DateRange that n days after self.base_date.

        Argus:
            days - next n days
        """
        next_date = self.base_date + timedelta(days=days)
        return DateRange(next_date, next_date + timedelta(days=1))

    def get_week_range(self, base_date):
        """
        Find the first/last day of the week for the given day.
        Weeks start on Sunday and end on Saturday.

        Argus:
            base_date - any date
        """
        start =  base_date - timedelta(days=base_date.weekday()+1)
        end = start + timedelta(days=7)
        return (start, end)

    def base_week(self):
        """
        Get DateRange of the week that contains self.base_date.
        """
        start, end = self.get_week_range(self.base_date)
        return DateRange(start, end)

    def prev_week(self, weeks=1):
        """
        Get the DateRange that n weeks before self.base_date.

        Argus:
            weeks - n week ago
        """
        _, end_date = self.base_week().get_range()
        start, end = self.get_week_range(end_date - timedelta(days=7*weeks))
        return DateRange(start, end)

    def next_week(self, weeks=1):
        """
        Get the DateRange that n weeks after self.base_date.

        Argus:
            weeks - next n weeks
        """
        _, end_date = self.base_week().get_range()
        start, end = self.get_week_range(end_date + timedelta(days=7*weeks))
        return DateRange(start, end)

    def get_month_range(self, year, month):
        """
        Get the first and last day of the given month in given year.

        Args:
            year
            month
        """
        days = calendar.monthrange(year, month)[1]
        start = date(year, month, 1)
        end = date(year, month, days) + timedelta(days=1)
        return (start, end)

    def base_month(self):
        """
        Get the DateRange of the month that contains self.base_date
        """
        year, month = self.base_date.year, self.base_date.month
        start, end = self.get_month_range(year, month)
        return DateRange(start, end)

    def prev_month(self, months=1):
        """
        Get the DateRange that n months before self.base_date.

        Argus:
            months - n months ago
        """
        start, end = self.base_month().get_range()
        for n in range(months):
            prev = start - timedelta(days=1)
            start, end = self.get_month_range(prev.year, prev.month)
        return DateRange(start, end)

    def next_month(self, months=1):
        """
        Get the DateRange that n months after self.base_date.

        Argus:
            months - next n months
        """
        start, end = self.base_month().get_range()
        for n in range(months):
            start = end
            _, end = self.get_month_range(start.year, start.month)
        return DateRange(start, end)

    def get_quarter_range(self, year, quarter):
        """
        Get time range with specific year and quarter.
        """
        if quarter not in (1, 2, 3, 4):
            return (None, None)

        start_month, end_month = get_monthrange(quarter)
        days = calendar.monthrange(year, end_month)[1]
        start = date(year, start_month, 1)
        end = date(year, end_month, days) + timedelta(days=1)
        return start, end

    def base_quarter(self):
        """
        Get the DateRange of the quarter that contains self.base_date.
        """
        quarter = get_quarter(self.base_date.month)
        start, end = self.get_quarter_range(self.base_date.year, quarter)<commit_msg>improve implementation of date and month related methods<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import calendar
from datetime import date
from datetime import timedelta

from DateRanger.utils import get_quarter
from DateRanger.utils import get_monthrange
from DateRanger.objects import DateRange
from DateRanger.exceptions import InvalidDateRange


class DateRanger(object):

    """
    A class for getting common bussiness date ranges.
    """

    def __init__(self, base_date=None):
        """
        Argus:
            base_date - the base day. Example: date(2009, 11, 1)
        """
        self.set_base_date(base_date)

    def set_base_date(self, base_date=None):
        """
        Set base date.

        Argus:
            base_date - Example: date(2009, 11, 1)
        """
        self.base_date = base_date or date.today()

    def base_day(self):
        """
        Get the DateRange of self.base_date.
        """
        return DateRange(self.base_date, self.base_date + timedelta(days=1))

    def relative_day(self, days=0):
        """
        Calcuate a relative date from self.base_date.
        """
        rday = self.base_date + timedelta(days=days)
        return (rday, rday + timedelta(days=1))

    def prev_day(self, days=1):
        """
        Get the DateRange that n days before self.base_date.

        Argus:
            days - n days ago
        """
        ndays = days * -1
        start, end = self.relative_day(days=ndays)
        return DateRange(start, end)

    def next_day(self, days=1):
        """
        Get the DateRange that n days after self.base_date.

        Argus:
            days - next n days
        """
        start, end = self.relative_day(days=days)
        return DateRange(start, end)

    def get_week_range(self, base_date):
        """
        Find the first/last day of the week for the given day.
        Weeks start on Sunday and end on Sunday.
        Because we don't include the last day.

        Argus:
            base_date - any date
        """
        start =  base_date - timedelta(days=base_date.weekday()+1)
        end = start + timedelta(days=7)
        return (start, end)

    def base_week(self):
        """
        Get DateRange of the week that contains self.base_date.
        """
        start, end = self.get_week_range(self.base_date)
        return DateRange(start, end)

    def prev_week(self, weeks=1):
        """
        Get the DateRange that n weeks before self.base_date.

        Argus:
            weeks - n week ago
        """
        _, end_date = self.base_week().get_range()
        start, end = self.get_week_range(end_date - timedelta(days=7*weeks))
        return DateRange(start, end)

    def next_week(self, weeks=1):
        """
        Get the DateRange that n weeks after self.base_date.

        Argus:
            weeks - next n weeks
        """
        _, end_date = self.base_week().get_range()
        start, end = self.get_week_range(end_date + timedelta(days=7*weeks))
        return DateRange(start, end)

    def get_month_range(self, year, month):
        """
        Get the first and last day of the given month in given year.

        Args:
            year
            month
        """
        days = calendar.monthrange(year, month)[1]
        start = date(year, month, 1)
        end = date(year, month, days) + timedelta(days=1)
        return (start, end)

    def base_month(self):
        """
        Get the DateRange of the month that contains self.base_date
        """
        year, month = self.base_date.year, self.base_date.month
        start, end = self.get_month_range(year, month)
        return DateRange(start, end)

    def prev_month(self, months=1):
        """
        Get the DateRange that n months before self.base_date.

        Argus:
            months - n months ago
        """
        base_year = self.base_date.year
        if self.base_date.month - months < 0:
            months = abs(self.base_date.month - months)
            yeardelta = (months // 12) + 1
            monthdelta = months % 12
            month = 12 - monthdelta
        elif self.base_date.month - months == 0:
            yeardelta = 1
            month = 12
        else:
            yeardelta = 0
            month = self.base_date.month - months

        year = base_year - yeardelta
        start, end = self.get_month_range(year, month)
        return DateRange(start, end)

    def next_month(self, months=1):
        """
        Get the DateRange that n months after self.base_date.

        Argus:
            months - next n months
        """
        base_year = self.base_date.year
        if (self.base_date.month + months > 12):
            yeardelta = (self.base_date.month + months) // 12
            year = base_year + yeardelta
            month = (self.base_date.month + months) % 12
        else:
            year = base_year
            month = self.base_date.month + months
        start, end = self.get_month_range(year, month)
        return DateRange(start, end)

    def get_quarter_range(self, year, quarter):
        """
        Get time range with specific year and quarter.
        """
        if quarter not in (1, 2, 3, 4):
            return (None, None)

        start_month, end_month = get_monthrange(quarter)
        days = calendar.monthrange(year, end_month)[1]
        start = date(year, start_month, 1)
        end = date(year, end_month, days) + timedelta(days=1)
        return start, end

    def base_quarter(self):
        """
        Get the DateRange of the quarter that contains self.base_date.
        """
        quarter = get_quarter(self.base_date.month)
        start, end = self.get_quarter_range(self.base_date.year, quarter)
{'fraction_non_alphanumeric': 0.09787194150182807, 'fraction_numerical': 0.009093465829192838, 'mean_word_length': 1.9608659450457953, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2093055', 'n_tokens_mistral': 3272, 'n_tokens_neox': 3113, 'n_words': 884}