Pages

Sunday, October 28, 2012

doctest for python class

You can class test with doctest. One issue to consider is to create object to test the method. This is an example.
"""
This is the "iniFileGenerator" module.

>>> f = iniFileGenerator()
>>> f.get()
hello
"""
class iniFileGenerator:
    def __ini__(self, hintFilePath):
        self.hintFilePath = hintFilePath
    def get(self):
        """
        >>> iniFileGenerator().get()
        hello
        """
        print "hello"
        
if __name__ == "__main__":
    import doctest
    doctest.testmod()
No news is good news, if you want to get the test result you need to use command line.
refactoringChecker> python iniFileGenerator.py -v
Trying:
    f = iniFileGenerator()
Expecting nothing
ok
Trying:
    f.get()
Expecting:
    hello
ok
Trying:
    iniFileGenerator().get()
Expecting:
    hello
ok
2 items had no tests:
    __main__.iniFileGenerator
    __main__.iniFileGenerator.__ini__
2 items passed all tests:
   2 tests in __main__
   1 tests in __main__.iniFileGenerator.get
3 tests in 4 items.
3 passed and 0 failed.
Test passed.

Reuse testing class objects

When testing class, you want to use the same object multiple times throughout the method testing. In this case, you can use extra globs dictionary.
"""
This is the "iniFileGenerator" module.
>>> print f.hintFilePath
./tests/unit_test_files/hint.txt
"""
class iniFileGenerator:
    def __init__(self, hintFilePath):
        self.hintFilePath = hintFilePath
    def hello(self):
        """
        >>> f.hello()
        hello
        """
        print "hello"
if __name__ == "__main__":
    import doctest
    hintFile = "./tests/unit_test_files/hint.txt"
    doctest.testmod(extraglobs={'f': iniFileGenerator(hintFile)})

Reference

Compilation Unit

Eclipse uses CompilationUnit as the basic unit of programming. A compilation unit is a single file the compiler sees. Your .c file may include other .c files, but after the preprocessor sorting out the includes, the compiler eventually sees just a single "compilation unit". – Eli Bendersky Feb 21 '09 at 7:12

Header files have no separate life, only their content is #included into .c or .cpp files. But since #include is handled by the preprocessor, the compiler has no knowledge about distinct header files; it only sees the resulting code listing as input. This is what is called a compilation unit: a source file with all its #include directives replaced by the content of the relevant header files. - Péter Török

Reference

  1. What does “static” mean in a C program?
  2. How is compilation unit defined in c++?
  3. http://en.wikipedia.org/wiki/Single_Compilation_Unit

substring search in python

Let's say you want to extract "add" from "abc()", you have a couple of choices in Python.

string#find

string object has find() method to get the position that matches the substring.
str = "add()"
pos = str.find("(")
str[0:pos]

You can have one liner.
str[0:str.find("(")]

regular expression

Regular expression in python has two favors, one is compile()/match(), and the other one is search().
re.compile(r'([^\)]*)\(\)').match(str).group(1)
re.search(r'([^\)]*)\(\)', str).group(1)

Reference