Search Results

Search found 2 results on 1 pages for 'chaindriver'.

Page 1/1 | 1 

  • Python Error-Checking Standard Practice

    - by chaindriver
    Hi, I have a question regarding error checking in Python. Let's say I have a function that takes a file path as an input: def myFunction(filepath): infile = open(filepath) #etc etc... One possible precondition would be that the file should exist. There are a few possible ways to check for this precondition, and I'm just wondering what's the best way to do it. i) Check with an if-statement: if not os.path.exists(filepath): raise IOException('File does not exist: %s' % filepath) This is the way that I would usually do it, though the same IOException would be raised by Python if the file does not exist, even if I don't raise it. ii) Use assert to check for the precondition: assert os.path.exists(filepath), 'File does not exist: %s' % filepath Using asserts seems to be the "standard" way of checking for pre/postconditions, so I am tempted to use these. However, it is possible that these asserts are turned off when the -o flag is used during execution, which means that this check might potentially be turned off and that seems risky. iii) Don't handle the precondition at all This is because if filepath does not exist, there will be an exception generated anyway and the exception message is detailed enough for user to know that the file does not exist I'm just wondering which of the above is the standard practice that I should use for my codes.

    Read the article

  • Python accessing modules from package that is distributed over different directories

    - by chaindriver
    Hi, I have a question regarding one single module that is distributed over multiple directories. Let's say I have these two file and directories: ~/lib/python xxx __init__.py util __init__.py module1.py module2.py ~/graphics/python xxx __init__.py misc __init__.py module3.py module4.py So then in my Python modules, I did this: import sys pythonlibpath = '~/lib/python' if pythonlibpath not in sys.path: sys.path.append(pythonlibpath) import xxx.util.module1 which works. Now, the problem is that I need xxx.misc.module3, so I did this: import sys graphicslibpath = '~/graphics/python' if graphicslibpath not in sys.path: sys.path.append(graphicslibpath) import xxx.misc.module3 but I get this error: ImportError: No module named misc.module3 It seems like it somehow still remembers that there was a xxx package in ~/lib/python and then tries to find misc.module3 from there. How do I get around this issue?

    Read the article

1