<commit_before>'A month, as in 200703; immutable'

import pdb
import unittest


class Month(object):
    def __init__(self, value1, value2=None):
        '''Constructors
           Month('200703')
           Month(200703)
           Month(2007, 3)
        '''

        if value2 is None:
            if isinstance(value1, str):
                self.year = int(value1[:4])
                self.month = int(value1[4:])
            elif isinstance(value1, int):
                self.month = value1 % 100
                self.year = (value1 - self.month) / 100
        else:
            self.year = int(value1)
            self.month = int(value2)

        # enforce invariant (other methods depend on this)
        assert self.year > 0, self
        assert 1 <= self.month <= 12, self

    def __repr__(self):
        return 'Month(year=%d, month=%d)' % (self.year, self.month)

    def increment(self):
        'return new Month one month after self'
        if self.month == 12:
            return Month(self.year + 1, 1)
        else:
            return Month(self.year, self.month + 1)

    def decrement(self):
        'return new Month one month before self'
        if self.month == 1:
            return Month(self.year - 1, 12)
        else:
            return Month(self.year, self.month - 1)

    def as_str(self):
        return '%04d%02d' % (self.year, self.month)

    def as_int(self):
        return self.year * 100 + self.month

    def equal(self, other):
        return self.year == other.year and self.month == other.month


class TestMonth(unittest.TestCase):
    def test_constructor(self):
        self.assertTrue(Month('200703').equal(Month(2007, 03)))
        self.assertTrue(Month(200703).equal(Month(2007, 03)))
        self.assertTrue(Month(200712).equal(Month(2007, 12)))

    def test_as_str(self):
        self.assertTrue(Month(200703).as_str() == '200703')

    def test_as_int(self):
        self.assertTrue(Month(200703).as_int() == 200703)

    def test_equal(self):
        self.assertTrue(Month('200703').equal(Month(2007, 03)))

    def test_increment(self):
        self.assertTrue(Month(200701).increment().equal(Month(200702)))
        self.assertTrue(Month(200712).increment().equal(Month(200801)))

    def test_decrement(self):
        self.assertTrue(Month(200701).decrement().equal(Month(200612)))
        self.assertTrue(Month(200712).decrement().equal(Month(200711)))

if __name__ == '__main__':
    unittest.main()
    if False:
        pdb.set_trace()<commit_msg>all decrement by more than 1 month<commit_after>'A month, as in 200703; immutable'

import pdb
import unittest


class Month(object):
    def __init__(self, value1, value2=None):
        '''Constructors
           Month('200703')
           Month(200703)
           Month(2007, 3)
        '''

        if value2 is None:
            if isinstance(value1, str):
                self.year = int(value1[:4])
                self.month = int(value1[4:])
            elif isinstance(value1, int):
                self.month = value1 % 100
                self.year = (value1 - self.month) / 100
            elif isinstance(value1, Month):
                self.month = value1.month
                self.year = value1.year
            else:
                print 'construction error: value1 is of type %s' % type(value1), value1
        else:
            self.year = int(value1)
            self.month = int(value2)

        # enforce invariant (other methods depend on this)
        assert self.year > 0, self
        assert 1 <= self.month <= 12, self

    def __repr__(self):
        return 'Month(year=%d, month=%d)' % (self.year, self.month)

    def increment(self):
        'return new Month one month after self'
        if self.month == 12:
            return Month(self.year + 1, 1)
        else:
            return Month(self.year, self.month + 1)

    def decrement(self, by=1):
        'return new Month one month before self'
        assert by >= 0
        print self, by
        if by > 12:
            years = by // 12
            print years
            return Month(self.year - years, self.month).decrement(by - 12 * years)
        elif self.month > by:
            return Month(self.year, self.month - by)
        else:
            print 12 - by + 1
            return Month(self.year - 1, 12 - by + 1)

    def as_str(self):
        return '%04d%02d' % (self.year, self.month)

    def as_int(self):
        return self.year * 100 + self.month

    def equal(self, other):
        return self.year == other.year and self.month == other.month


class TestMonth(unittest.TestCase):
    def test_constructor(self):
        self.assertTrue(Month('200703').equal(Month(2007, 03)))
        self.assertTrue(Month(200703).equal(Month(2007, 03)))
        self.assertTrue(Month(200712).equal(Month(2007, 12)))
        m1 = Month(2007, 3)
        m2 = Month(m1)
        self.assertTrue(m1 != m2)

    def test_as_str(self):
        self.assertTrue(Month(200703).as_str() == '200703')

    def test_as_int(self):
        self.assertTrue(Month(200703).as_int() == 200703)

    def test_equal(self):
        self.assertTrue(Month('200703').equal(Month(2007, 03)))

    def test_increment(self):
        self.assertTrue(Month(200701).increment().equal(Month(200702)))
        self.assertTrue(Month(200712).increment().equal(Month(200801)))

    def test_decrement(self):
        self.assertTrue(Month(200701).decrement().equal(Month(200612)))
        self.assertTrue(Month(200712).decrement().equal(Month(200711)))
        self.assertTrue(Month(200701).decrement(1).equal(Month(200612)))
        self.assertTrue(Month(200701).decrement(2).equal(Month(200611)))
        self.assertTrue(Month(200701).decrement(12).equal(Month(200601)))
        self.assertTrue(Month(200701).decrement(13).equal(Month(200512)))
        self.assertTrue(Month(200701).decrement(120).equal(Month(199701)))

if __name__ == '__main__':
    unittest.main()
    if False:
        pdb.set_trace()
{'fraction_non_alphanumeric': 0.13150409904634433, 'fraction_numerical': 0.07729630249288941, 'mean_word_length': 2.2810098792535674, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, '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': '4052643', 'n_tokens_mistral': 2144, 'n_tokens_neox': 1816, 'n_words': 402}