Avoiding code duplication without the use of switch/case in the Python language

Asked

Viewed 331 times

0

Friends,

I have the following code in Python:

data_hoje = time.strftime("%d %b %Y", time.gmtime() ) #data de hoje

data_desejada = time.strftime("%d %b %Y", time.gmtime(time.time() + (3600 * 24 * 2))) # daqui a 2 dias

I know that in Python we don’t have the switch/case structure that is used in other languages. The above code is very repetitive. How to make it more "lean", ie avoid the use of various if/Elif?

if data_hoje[0:2] == "01":
    data_nova = data_hoje[1:2] + data_hoje[2::] 

elif data_hoje[0:2] == "02":
    data_nova = data_hoje[1:2] + data_hoje[2::]

elif data_hoje[0:2] == "03":
    data_nova = data_hoje[1:2] + data_hoje[2::]

elif data_hoje[0:2] == "04":
    data_nova = data_hoje[1:2] + data_hoje[2::]

elif data_hoje[0:2] == "05":
    data_nova = data_hoje[1:2] + data_hoje[2::]

elif data_hoje[0:2] == "06":
    data_nova = data_hoje[1:2] + data_hoje[2::]

elif data_hoje[0:2] == "07":
    data_nova = data_hoje[1:2] + data_hoje[2::]

elif data_hoje[0:2] == "08":
    data_nova = data_hoje[1:2] + data_hoje[2::]

elif data_hoje[0:2] == "08":
    data_nova = data_hoje[1:2] + data_hoje[2::]

else:
    data_nova = data_hoje






if data_desejada[0:2] == "01":
    data_nova2 = data_desejada[1:2] + data_desejada[2::] 

elif data_desejada[0:2] == "02":
    data_nova2 = data_desejada[1:2] + data_desejada[2::]

elif data_desejada[0:2] == "03":
    data_nova2 = data_desejada[1:2] + data_desejada[2::]

elif data_desejada[0:2] == "04":
    data_nova2 = data_desejada[1:2] + data_desejada[2::]

elif data_desejada[0:2] == "05":
    data_nova2 = data_desejada[1:2] + data_desejada[2::]

elif data_desejada[0:2] == "06":
    data_nova2 = data_desejada[1:2] + data_desejada[2::]

elif data_desejada[0:2] == "07":
    data_nova2 = data_desejada[1:2] + data_desejada[2::]

elif data_desejada[0:2] == "08":
    data_nova2 = data_desejada[1:2] + data_desejada[2::]

elif data_desejada[0:2] == "08":
    data_nova2 = data_desejada[1:2] + data_desejada[2::]

else:
    data_nova2 = data_desejada
  • 1

    Wow - this code is absurdly repetitive -now, I don’t see how "switch Casse" would reduce the number of its lines - the answers reflect smarter ways to do this without if/Elif or switch/case. Now - to do operations with dates, you should use the Python dattime module, and not do operations with text, which can break dozens of different ways.

3 answers

4

Usa Loop:

for i in range(1,9,1):
  if data_hoje[0:2] == "0"+str(i):
    data_nova = data_hoje[1:2] + data_hoje[2::]
else:
  data_nova = data_hoje

for i in range(1,9,1):
  if data_hoje[0:2] == "0"+str(i):
    data_nova2 = data_hoje[1:2] + data_hoje[2::]
else:
  data_nova2 = data_hoje
  • ,Thank you! data_hoje = time.strftime("%d %b %Y", time.gmtime() prints, for example, 01 Mar 2017. The idea was to turn this into 1 Mar 2017. Some better way?

  • import datetime / / data = datetime.date.Today() / print(data.day) / the bar is just for sorting, use the datetime class

4


I think you’re doing things unnecessarily:

Can you narrow it down:

data_hoje[1:2] + data_hoje[2::] # 1 Mar 2017

for:

data_hoje[1:] # 1 Mar 2017

It seems to me here that you are just checking if the date (day) starts with a zero and removing that zero if this is true, so why not make it simpler and use ithrim:

data_hoje = time.strftime("%d %b %Y", time.gmtime()).lstrip('0') # 1 Mar 2017
data_desejada = time.strftime("%d %b %Y", time.gmtime(time.time() + (3600 * 24 * 2))).lstrip('0') # 3 Mar 2017

Last, you can just use %e in your job strftime:

data_hoje = time.strftime("%e %b %Y", time.gmtime()).strip() # 1 Mar 2017
data_desejada = time.strftime("%e %b %Y", time.gmtime(time.time() + (3600 * 24 * 2))).strip() # 3 Mar 2017

Here I use the strip() just to take away the space that will be instead of the 0.

And a fix on your condition, they could just be:

if data_hoje.startswith('0'): # caso a data comece com um 0
    data_nova = data_hoje[1:]

This last one is just an example of what you could do instead of all these conditions and how much you could reduce the code, the two solutions I’ve worked on are better.

  • thanks! There is something similar to switch/case in Python?

  • @Eds Nop, with if/Elif you can achieve the same. Anyway not needed for this case, read my explanation well. All your code is reduced to those lines I put in, one of the first two examples I put in

2

If you simply want a smaller code you can also try a simplified loop in a function:

def atribuiData(data):
    data_nova = [(data[1:2] + data[2::]) for i in range(1,9) if data[0:2] == ("0{0}".format(i))]
    if not data_nova:
        data_nova = data
    return data_nova

data_nova = atribuiData(data_hoje)
data_nova2 = atribuiData(data_desejada)

Browser other questions tagged

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