4
\$\begingroup\$

The function below converts a time code into seconds, while maintaining the decimal value (fractions of seconds). The time code fed to this function will always contain microseconds. This currently works, but I'd be interested to see how it could be improved/simplified.

def Second_Conversion(x): #Takes a string such as "2:22:04.996304"
 HMS = x.split(':')
 SF = x.split('.')
 Full_HMS = x[0:7]
 Split_HMS = Full_HMS.split(':')
 Full_Units = int(Split_HMS[0]) * 3600 + int(Split_HMS[1]) * 60 + int(Split_HMS[2])
 Second_Fractions = int(SF[1])
 Converted_Seconds_Fractions = Second_Fractions /1000000
 print(Converted_Seconds_Fractions)
 Final_Time = Full_Units + Converted_Seconds_Fractions
 print(Final_Time)
200_success
146k22 gold badges190 silver badges478 bronze badges
asked Mar 12, 2015 at 23:18
\$\endgroup\$
3
  • \$\begingroup\$ Please follow PEP8 \$\endgroup\$ Commented Mar 13, 2015 at 10:04
  • \$\begingroup\$ Thanks for the link Janos! Would you mind explaining how exactly I'm breaking the PEP8? I'm pretty new to Python, and would greatly appreciate any insight you can offer. Thanks again! \$\endgroup\$ Commented Mar 14, 2015 at 1:45
  • 1
    \$\begingroup\$ Variable and function names should be snake_case, all lowercase. \$\endgroup\$ Commented Mar 14, 2015 at 7:21

2 Answers 2

4
\$\begingroup\$

This section can be simplified as you only print the variables, never work with them:

Second_Fractions = int(SF[1])
Converted_Seconds_Fractions = Second_Fractions /1000000
print(Converted_Seconds_Fractions)
Final_Time = Full_Units + Converted_Seconds_Fractions
print(Final_Time)

Why don't you just do this?

print(int(SF[1]) / 1000000)
print(Full_Units + Converted_Seconds_Fractions)

Also, it isn't good to have magic values in your code. I would probably put the 1000000 value in a variable named with UPPER_CASE naming to show it shouldn't be changed. Python does not have a way to signify constant variables, so that is why you use a different naming convention for them.

Again, I would probably not print from the same method you convert them with. What if you want to get the value from this method sometime without it printing them? You should return the values instead of just printing them, and maybe make a separate method for printing them.

answered Mar 13, 2015 at 4:04
\$\endgroup\$
1
  • \$\begingroup\$ Thanks so much for the suggestions Hosch250! Funnily enough, I included the print statements in an attempt make the code easier to understand (for this post, actually). The Final_Time variable will be returned, and used elsewhere in the program (I just didn't think including that portion of the code was relevant). Regarding the magic value, I completely overlooked this, and I'll definitely change it. Anyway, thanks again for your help! \$\endgroup\$ Commented Mar 14, 2015 at 1:33
4
\$\begingroup\$

I don't know whether you wanted to optimize the code for length or speed or "elegance", but here it is as a one-liner:

def Second_Conversion(x):
 return sum(map(lambda i,j: int(i)*j, x.replace('.', ':').split(':'), [3600, 60, 1, 1e-6]))
answered Mar 13, 2015 at 3:46
\$\endgroup\$
1
  • \$\begingroup\$ Very cool! I'm still pretty new to Python, so I really love seeing the different ways one can approach a problem. Thanks @Nivaead! \$\endgroup\$ Commented Mar 14, 2015 at 1:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.