import os, unittest
# figure all the modules available
dir = os.path.split(__file__)[0]
This prints the current directory, as file points points to the current file name
mods = {}
dont know what this does, but i can guess this creates a list ? or an array ?
l = []
I guess this also creates an array and assigns it l
__all__ = []
Dont know if __all__
has some special significance, but it does look like an array
for file in os.listdir(dir):
if not file.endswith('.py') or file == '__init__.py':
continue
name = file[5:-3]
5:-3 , what does that mean ?
mods[name] = __import__(file[:-3], globals(), locals(), [])
what does mods means ?
__all__.append(name)
l.append(mods[name].suite())
def suite():
return unittest.TestSuite(l)
Almost all of your questions are surrounding basic Python Syntax.
{} sets a dictionary which is like an associative array [] is a list, which is like a mutable standard array
for file in os.listdir(dir): if not file.endswith('.py') or file == 'init.py': continue name = file[5:-3]
Loops through files, I'm pretty sure it is ignoring non python files or init.py and then gets the a substring of the file name
[5:-3] essentially get a substring of the file name, though I am not 100% sure if that is different when using os package to loop through directories.
mods is the dictionary declared above mods = {}
the append stuff is adding things to the lists declared with []
You should read "Dive Into Python". Or at least go onto python's site and read the part about basic data structures.
Broadly, he's listing the files in the directory which end with .py, then adds those names to the content listing of the of the package (__all__
). He goes on the import those scripts by name (using __import__
), then makes that list of modules (objects representing scripts) into a group of unit-tests (unittest.TestSuite).
You will save yourself a world of hurt by reading (or at least skim!) the official tutorial in full:
http://docs.python.org/tutorial/index.html
To answer your direct questions:
__all__
is a way to list the contents of your package, if you don't like the way python does it by default. Documentation here: http://docs.python.org/tutorial/modules.html#importing-from-a-package
The index [5:-3]
means take the items from the list starting at index 5 and up to (but not including) -3. Negative indexes count from the end, ie -1 is the last item. Documentation here: http://docs.python.org/tutorial/introduction.html#lists
mods
was created earlier in the script. It's a dict (perl users read "hash") with no special significance. http://docs.python.org/tutorial/datastructures.html#dictionaries
The program is creating a test suite out of the suite()
functions defined in the files in the same directory with a .py
extension.
The [5:-3]
is ignoring the first five characters of the file names (which I assume to be test_), and the .py
extension.
__all__ is a list with the identifiers that a module exports. It's being set to the list of the modules previously recovered.
This convoluted code is probably the wrong way to achieve what's intended.
精彩评论