Construction and Class Abstraction

Asked

Viewed 45 times

0

I am developing a small project that takes care of organizing and controlling the daily sales of a retail seller. Basically the user will impute their sales and kpi’s and the application will try to reset the targets according to these values, among other features, taking into account of course, a monthly quota. My difficulty in this phase of testing is to simulate the passage of time, because much of the applicability of the project is tied to this (reports, feedbacks, etc). I decided to create a class Timeflow, since in the procedural paradigm it was finding many limitations, necessity of the use of files, as well as lack of organization in the code.

Follow code still at the beginning:

class TimeFlow:

    ano = ('Janeiro', 'Fevereiro', 'Março',
           'Abril', 'Maio', 'Junho',
           'Julho', 'Agosto', 'Setembro',
           'Outubro', 'Novembro', 'Dezembro')

    semana_completa = ('Segunda-Feira',
                       'Terça-Feira',
                       'Quarta-Feira',
                       'Quinta-Feira',
                       'Sexta-Feira',
                       'Sábado',
                       'Domingo')


    def __init__(self):

        self.dia = date.today().day
        self.mes = ano[date.today().month - 1]
        self.ano = date.today().year
        self.dia_semana = semana_completa[date.today().weekday() - 1]


def finaliza_dia(self):

        duracao_mes = úteis.dias_mes_atual()  # função que retorna número de dias do mês atual

        if dia == duracao_mes:
            pass
            # aplicar aqui def de passagem de mês
        else:
            self.dia += 1
            # preciso passar dia da semana também


def finaliza_mes(self):
        pass


def finaliza_ano(self):
        pass


def mostra_data(self):
        pass

My question is how to use the variables that will determine the passage of time and where to create them. I know the method __init__ it runs every time an instance is generated, so it makes sense to put those variables there, right? The problem is that I’m not being able to use year and week_complete structures within the constructor method, as you can see. Or I could just create these variables as class variables?

1 answer

2


The attributes ano and semana_completa sane variáveis de classe and need to be accessed as such:

class TimeFlow:
    def __init__(self):
        ...
        self.mes = TimeFlow.ano[date.today().month - 1]
        self.dia_semana = TimeFlow.semana_completa[date.today().weekday() - 1]
        ...

A solution to your problem would be the implementation of a iterador able to advance the days, the months and the years, each one independently, see only:

from datetime import date
from dateutil.relativedelta import relativedelta

class TimeFlow():

    dias_semana = ['Segunda-Feira','Terca-Feira','Quarta-Feira',
                   'Quinta-Feira','Sexta-Feira','Sabado','Domingo']

    meses_ano = ['Janeiro','Fevereiro','Marco','Abril','Maio','Junho',
                 'Julho','Agosto','Setembro','Outubro','Novembro',
                 'Dezembro']

    def __init__(self, data=None):
        self.data = data if data else date.today()

    def proximo_dia(self):
        self.data = self.data + relativedelta(days=1)
        return self

    def proximo_mes(self):
        self.data = self.data.replace(day=1) + relativedelta(months=1)
        return self

    def proximo_ano(self):
        self.data = self.data.replace(day=1, month=1) + relativedelta(years=1)
        return self

    def __str__(self):
        dia_semana = TimeFlow.dias_semana[self.data.weekday()]
        nome_mes = TimeFlow.meses_ano[self.data.month-1]
        return self.data.strftime(f"{dia_semana}, %d de {nome_mes} de %Y")


t = TimeFlow(date(day=1,month=1,year=1970))

print(t)                 # 01/Jan/1970

print(t.proximo_dia())   # 02/Jan/1970
print(t.proximo_dia())   # 03/Jan/1970
print(t.proximo_dia())   # 04/Jan/1970

print(t.proximo_mes())   # 01/Fev/1970
print(t.proximo_mes())   # 01/Mar/1970
print(t.proximo_mes())   # 01/Abr/1970

print(t.proximo_ano())   # 01/Jan/1971
print(t.proximo_ano())   # 01/Jan/1972
print(t.proximo_ano())   # 01/Jan/1973

Exit:

Quinta-Feira, 01 de Janeiro de 1970
Sexta-Feira, 02 de Janeiro de 1970
Sabado, 03 de Janeiro de 1970
Domingo, 04 de Janeiro de 1970
Domingo, 01 de Fevereiro de 1970
Domingo, 01 de Marco de 1970
Quarta-Feira, 01 de Abril de 1970
Sexta-Feira, 01 de Janeiro de 1971
Sabado, 01 de Janeiro de 1972
Segunda-Feira, 01 de Janeiro de 1973
  • can you explain to me the use of the replace method? Does relativedelta no longer make this date change automatically? Using the method proximo_mes as reference, if we had left from January 4 for example it would return 01/02/1970, no? In the case of the project I will not need to spend years or months, unless it really is the last day of the month or year, that is, time will naturally pass.

  • Another question is that I am not able to use the module, is raising exception Modulenotfounderror. I have made all possible attempts and even the python-dateutil package appears in the list of external modules. The curious thing is that the IDE also already recognizes the methods self-competing the code. I don’t know what might be going on...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.