Pages

Jul 19, 2015

Use Sphinx to automatically create Python documents

Creating program documents is a tedious task. However, with the assistance of automatic tools for creating program documents, we can produce the documents in a very easy and quick way. (We still have to write the comments of program functions.)

Here are the steps of that I used Sphinx to create documents for my python project.

1. Install Sphinx
$ pip install sphinx 


2. Create a directory (folder) for documents
Create a directory named "docs", and the structure of the "docs" and "src" directories is shown below:

Python Project/
  |--------docs/
  |--------src/(source code)


3. Create a Sphinx project
In the docs/ directory, perform the following command:
$ sphinx-quickstart docs


During the process, you will be asked several questions. The following are my answers to some of the questions (empty answer means using the default setting):

Separate source and build directories (y/n) [n]: y
autodoc: automatically insert docstrings from modules (y/n) [n]: y
intersphinx: link between Sphinx documentation of different projects (y/n) [n]: y
ifconfig: conditional inclusion of content based on config values (y/n) [n]: y
viewcode: include links to the source code of documented Python objects (y/n) [n]: y

After step 3 is finished, the structure of the Sphinx project will be that as follows:
Python Project/
  |--------docs/
  |             |----- build
  |             |----- source
  |--------src/(source code)


4. Modify docs/source/conf.py
In the conf.py file created in step 3, find the following comments.
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.

Add a setting under the above comments so that Sphinx can find the source code.
sys.path.insert(0, os.path.abspath('../../src/'))


5. Use sphinx-apidoc to create module_name.rst files
$ sphinx-apidoc -o docs/source src


6. Create html files
In the docs directory, perform the following command to create the documents' html files.
$ make html

If the documents are successfully created, you can open the docs/build/html/index.html to read it.


Reference:
http://chimerhapsody.blogspot.com/2014/07/python.html
http://sphinx-doc.org/invocation.html#invocation-of-sphinx-apidoc




Jul 13, 2015

Be careful of implementing "binary search"

When using a binary search, we usually will calculate the middle of the search space by
mid = (start + end) / 2

However, if implement this calculation on a computer program, it might cause an overflow problem if the start and end are already too large. Therefore, instead of using (start + end) / 2, we could use
mid = start + (end - start) / 2


See more detail: http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html

Jul 7, 2015

Static and Dynamic Binding in Java

Static Binding or Early Binding
The binding which can be resolved at compile time by compiler is known as static or early binding. All the static, private and final methods have always been bonded at compile-time . Why binding of Static, final and private methods is always a static binding? You would understand it better after reading dynamic binding. Still let me explain this – Compiler knows that all such methods cannot be overridden and will always be accessed by object of local class. Hence compiler doesn’t have any difficulty to determine object of class (local class for sure). That’s the reason binding for such methods is static.
Dynamic Binding or Late Binding
When compiler is not able to resolve the call/binding at compile time, such binding is known as Dynamic or late Binding.

Reference:
http://beginnersbook.com/2013/04/java-static-dynamic-binding/

The "finally" block in Java

The "finally" block will get executed if we insert a return statement inside the try block of a try-catch-finally. Even when we attempt to exit within the try block (via a return statement, a continue statement, a break statement or any exception), the finally block will still be executed.

Note that there are some cases in which the finally block will not get executed, such as the following:

  • If the virtual machine exits during try/catch block execution.
  • If the thread which is executing the try/catch block gets killed.

Jul 5, 2015

"final" in Java

"final" doesn't guarantee immutable. It only protect the reference, not the object it references to.

ex:
private final int[] x; //x cannot change, but x[0] can change.

Access control in Java inheritance and package













  • the variable or method in a interface is actually public even if we left no modifier before it.

Jul 1, 2015

Bayes' theorem


  • Bayes' theorem was used to convert a prior probability into a posterior probability by incorporating the evidence provided by the observed data.
  • P(W | D) = P(D | W)P(W)/P(D)     ==> posterior = likelihood x prior
    • P(W | D): posterior probability
    • P(D | W): likelihood function
    • P(W): prior probability
    • P(D): normalization constant, ensures that the posterior distribution on the left-hand side is a valid probability density and integrates to one.
  • maximum likelihood: w is set to the value that maximizes the likelihood function p(D | W). This corresponds to choosing the value of W for which the probability of the observed data set is maximized.