Friday, October 26, 2012

Visitor design pattern in python

Visitor pattern uses double dispatch which is simple but powerful.
class Node:
    def __init__(self):
        self.children = []
    def add(self, node):
    def printIt(self):
    def accept(self, visitor):
class NodeA(Node):
    def printIt(self):
class NodeB(Node):
    def printIt(self):
class NodeVisitor:
    def visit(self, node):
        if len(node.children) > 0:
            for child in node.children:
if __name__ == "__main__":
    n = Node()
    n1 = NodeA()
    n2 = NodeA()
    n3 = NodeB()
    n4 = NodeB()

    v = NodeVisitor()
Node -+- NodeA -+- NodeB
      |         +- NodeB
      +- NodeA
2012 10 27 01 32 49

Get info in the project with eclipse/java

private static Set< ICompilationUnit> getFiles(String projname)
        throws CoreException {
    IWorkspaceRoot ws = ResourcesPlugin.getWorkspace().getRoot();
    IProject proj = ws.getProject(projname);
    IJavaProject javaProject = JavaCore.create(proj);
    Set< ICompilationUnit> files = new HashSet< ICompilationUnit>(); NullProgressMonitor());
    for (IPackageFragment packFrag : javaProject.getPackageFragments()) {
        for (ICompilationUnit icu : packFrag.getCompilationUnits()) {
    return files;
ICompilationUnit file = …;
IResource resource = null;
try {
	resource = file.getUnderlyingResource();
} catch (JavaModelException e) {
if (resource.getType() == IResource.FILE) {
    IFile ifile = (IFile) resource;
    String path = ifile.getRawLocation().toString();

What it does is get IWorkspaceRoot, and from it, you can get IJavaProject. Then, open with NullProgressMonitor(). For all of the PackageFragment, we can find ICompilationUnit, and add it to the files HashSet.

The issue is how to get the physical path from the compilation unit?
private String getWorkbenchDirectory() throws FileNotFoundException
	IPath path = Platform.getLocation();
	String filePath = path.toOSString();
	return filePath;


  1. Get the absolute path of the currently edited file in Eclipse
  2. How to get file path information from ICompilationUnit?
  3. How do I get the file path of the open workspace for developing plugins in eclipse

useful eclipse hotkey

  1. F3 - for finding the definition of method


package in Java/eclipse

  1. Source is located in src directory and build result is located in bin directory
  2. In source code you don’t have to located the source file following package. For example, you have, you don’t have to create directory abc and put the java source code it it when your package name is abc
  3. After the compilation, the created class is located in bin/abc/Hello.class,
  4. When executing the class in the package you need to specify the package with ‘.’, for example, java abc.Hello for executing it.


Precision and Recall

From wikipedia


The left side is relevant domain and the circle means retrieved domain.

The perfect case can be the circle encloses only the left hand side, which is not always true, so we have precision and recall.


Precision means how precise we retrieved from samples. It focuses on not retrieving wrong samples. It’s the ratio between green circle and whole circle. Whole circle means what I retrieved and green circle means what I retrieved correctly.

So, precision says how accurate your retrieved result is. The more the less false positive.

When precision is low, it just means you include too many wrong samples.


Recall means how many samples you missed. It focuses on not missing correct samples. Think about the case car company recalls defected cars. It cares about the cars that it needs to recall, it doesn’t care about recalling non-defect cars. It’s the ratio between green circle and left side. Left area means the samples that I need to retrieve.

So, recall says did you good at not letting anything behind. The more the less false negative.

MacJournal article