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

1

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 do not 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 using the 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
    
asked by anonymous 01.03.2017 / 04:26

3 answers

4

I think you're doing things unnecessarily:

You can reduce this:

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

To:

data_hoje[1:] # 1 Mar 2017

It seems to me that you are just checking to see if the date (day) starts with a zero and to remove that 0 if that happens, then why not make everything simpler and use ltrim :

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

Finally, you can simply use %e in your function 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 strip() only to take the space that will be instead of 0 .

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

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

This last is just a sample of what you could do instead of these conditions all and how much you could reduce the code, the two solutions I gave above are better.

    
01.03.2017 / 09:12
4

Use 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
    
01.03.2017 / 04:56
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)
    
01.03.2017 / 16:01