0

Yet another noob query here. Quotes like "never write the same code twice" have me trying to ferret out my mistakes; here's one I want to improve: code in main file:

if os.name == 'nt':
    dosomething

another way:

if os.name == 'nt':
    os_is_nt = True

if os_is_nt:
    dosomething

another way, putting this function in an import

detectNT()
    if os.name == 'nt':
        return True
    else:
        return False

My belief is, without figuring out how to check any speed differences, is that I should cut it back to the original:

if os.name == 'nt':
    dosomething

but my faith is weak, which is why I'm here.

Also, I'm thinking there's a way to find the local file separator..? I've written this:

def os_file_sep():
    file_separator = "/"
    if os.name == 'nt':
        file_separator = "\\"
    return file_separator
6
  • What do you actually need to do with the file separators? The os module probably does what you need already. Commented Aug 20, 2015 at 10:27
  • 1
    re file separators using the os.path module should take care of that for you with its methods like join Commented Aug 20, 2015 at 10:27
  • 2
    There's two separate issues: don't repeat yourself/one single source of truth and performance. If you write code in a manner that is DRY, where each part of the logic is only expressed once, that may be at odds with performance. Sometimes it's more performant to write it in some unmaintainable way; but then it's unmaintainable, potentially... The question is: is it too slow?! If not: write DRY code and don't worry about performance. Commented Aug 20, 2015 at 10:31
  • 1
    os.path.sep and os.sep are path separators Commented Aug 20, 2015 at 10:33
  • 1
    @SuperBiasedMan in order to highlight the filename from the path I'm printing one, in say green, then the file separator, then the filename in cyan or such. So join was not workable. I see how I can do it now using the os.path.sep Commented Aug 20, 2015 at 11:17

2 Answers 2

1

What should you use?

Use os.path.join (or os.path.sep). Don't reinvent wheel. It's simple and great.

>>>os.path.join("foo", "bar")
'foo\\bar'

What if there wasn't this (better) way?

  • Value never changes - first way ("direct" if check) is IMO best. It's simple and readable (unless you do this check in many different places, then I guess variable could be more error prone).
  • Value changes - I would choose variable and if check if the check is simple and function if the check is complicated.
Sign up to request clarification or add additional context in comments.

Comments

1

Your general question is about performance, so I will answer to that.

The main rule is: do not worry about low level performance unless you really need it. That means that you should be careful about algorithms but not about low level optimization at first sight, and always prefer simple and maintainable code.

When you have a performance problem (or suspect you will soon have), profile your code. Python provides useful tools out of the box in the Standard Library. One of them is profile or its optimized version cProfile. Extract from the manual:

To profile an application with a main entry point of foo(), you would add the following to your module:

import cProfile
cProfile.run('foo()')

The above action would cause foo() to be run, and a series of informative lines (the profile) to be printed.

Now you know where your program spend most of its time and you know the parts that deserve low level optimisation.

And here again Python provides out of the box the timeit module. This module provides a simple way to time small bits of Python code. It has both command line as well as callable interfaces. It avoids a number of common traps for measuring execution times.

For the precise question you asked, the correct answer is David Mašek's one. This one is for the general question of performance. Anyway, if you have doubts about the performances of the different solutions (yours and David's ones), just use timeit to be sure, and optionally report here in a self answer.

Comments

Your Answer

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.