Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: using a custom number class or type

Tags:

python

I am working with a custom number type which is best thought of as YearQuarter, (i.e. 20141, 20142, 20143, 20144, 20151, 20152, ...), or as I label it, quarter_code, q_code for short. Its incrementing function would be something like:

def code_sum(q_code, n):
    q_code_year, q_code_quarter = q_code // 10, q_code % 10
    n_year, n_quarter = (n // 4), (n % 4 - 1)

    quarters = q_code_quarter + n_quarter
    years = q_code_year + n_year + quarters // 4
    return years * 10 + quarters % 4 + 1

#  code_sum(20141, 1) = 20142, code_sum(20144, 1) = 20151
#  code_sum(20144, -1) = 20143, code_sum(20151, -1) = 20144
#  NOTE: code_sum(20147, 0) = 20153

I want to warn or raise exceptions for numbers which don't conform to the form year*10 + number_of_quarters. It easy to write and call a check function, but I'm wondering if constantly calling check is the best approach when using quarter_codes in many different functions. E.g.

def foo(qc1, qc2, qc3):
    qc1, qc2, qc3 = check(qc1, qc2, qc3)
    # do something
    return bar

def foo2(qc, arg1, arg2) ...
    qc = check(qc)
    return 42

def fooN(qc1, qc2, arg1):
    qc1, qc2 = check(qc1, qc2)

And so on. Here is a short check function as an example.

def check(*args):
    checked = tuple()
    for q_code in args:
        if q_code % 10 > 4:
            while q_code % 10 > 4:
                q_code += 6
            print('Number of quarters > 4. Using {}'.format(q_code))
            checked += (q_code, )
        else:
            checked += (q_code, )

    return checked[0] if len(checked) == 1 else checked

It seems a little laborious to create class YearQtr although maybe I am missing something here altogether. My question really boils to: how far should I go to creating a custom number class or type? And how would I do that?

like image 745
Cole Avatar asked Nov 01 '22 04:11

Cole


1 Answers

I hope this can help you

class QuarterCode(object):
    """docstring for QuarterCode"""

    @property
    def value(self):
        return self.__value;

    @value.setter
    def value(self, value):
        assert 1 <= value%10 <=4, "Number of quarters differs from {1,2,3,4}"
        self.__value = value

    def __init__(self, value):
        self.value = value

    def code_sum(self, n):
        q_code_year, q_code_quarter = self.value // 10, self.value % 10
        n_year, n_quarter = (n // 4), (n % 4 - 1)
        quarters = q_code_quarter + n_quarter
        years = q_code_year + n_year + quarters // 4
        self.value = years * 10 + quarters % 4 + 1

You can use this class as follows:

>>>q1 = QuarterCode(20142) 
>>>q1.value
20142 
>>>q1.code_sum(10)
20164
>>>q1.value = 20145
AssertionError: Number of quarters differs from {1,2,3,4}
like image 140
Salvatore Avanzo Avatar answered Nov 15 '22 04:11

Salvatore Avanzo