"Eggs to Python are like Jars to Java..."
More information
Tuesday, May 29, 2007
Thursday, May 24, 2007
C++ note: static variables
C++ is different from Java and csharp in the way classes are defined. The header files in C++ are not actually compiled into the object - they are only an indicator of how the data structures are organized, i.e. the reference to a member is the base address plus how much offside. All variables and functions to appear inside objects must be defined in the cpp files. The methods in header files are treated as inline methods.
Now static variables are those that occur in the heap. They exist from the beginning until the finishing of the running of program. All variables defined in a cpp file are static variables, they are by default external linked. If a variable defined in a cpp file has explicit "static" modifier, it is internal linked and can only be accessed by the module. Static variables can also be defined inside functions and methods, in which case they are still present all through the running of a program; however, they can only be accessed from the name scope in which they are defined. The accessibility is the only difference between static variables. Lastly, constants in header files are also taken as static variables. This is why values can be assigned to them in the header files.
Now static variables are those that occur in the heap. They exist from the beginning until the finishing of the running of program. All variables defined in a cpp file are static variables, they are by default external linked. If a variable defined in a cpp file has explicit "static" modifier, it is internal linked and can only be accessed by the module. Static variables can also be defined inside functions and methods, in which case they are still present all through the running of a program; however, they can only be accessed from the name scope in which they are defined. The accessibility is the only difference between static variables. Lastly, constants in header files are also taken as static variables. This is why values can be assigned to them in the header files.
Tuesday, May 22, 2007
g++ note: don't forget -c option
I made a mistake when writing Makefile today, by missing to put -c for the compile-only step. It led to a lot of unresolved external references.
Monday, May 14, 2007
Latex note: generating letter size papers
latex paper
dvips -t letterSize -Ppdf -G0 paper
ps2pdf -sPAPERSIZE=letter paper.ps
If the last step doesn't work, try the following command:
ps2pdf paper.ps
It should work because the ps file already contains size information.
dvips -t letterSize -Ppdf -G0 paper
ps2pdf -sPAPERSIZE=letter paper.ps
If the last step doesn't work, try the following command:
ps2pdf paper.ps
It should work because the ps file already contains size information.
Thursday, April 12, 2007
Latex note: more on making EPS graphs from MS Office
Please see my previous article about how to generate an eps chart from from MS Excel.
You can also insert Word pictures into eps. The mechanism is the same as Excel. First, make an empty word document or page to draw the vector graphs. Then print the page, using general eps printer. Lastly, modify the page bounding box of the eps file so that it fits the graph.
There is one thing to notice though: the coordinate counts from bottom left to top right! If the four numbers represent top left and bottom right, the picture will still show in latex document, but it will be sqeezed together with text because the size is below zero!
You can also insert Word pictures into eps. The mechanism is the same as Excel. First, make an empty word document or page to draw the vector graphs. Then print the page, using general eps printer. Lastly, modify the page bounding box of the eps file so that it fits the graph.
There is one thing to notice though: the coordinate counts from bottom left to top right! If the four numbers represent top left and bottom right, the picture will still show in latex document, but it will be sqeezed together with text because the size is below zero!
Latex note: use minipage to align a figure of text to center, while keeping text aligned to left
Sometimes we need to align text with multiple lines to the center of page.
\begin{figure}[t]
\centering
this is first line \\
\hspace*{0.5cm} this is second line \\
this is third line \\
this is fourth line
\caption{caption}
\label{l}
\end{figure}
The above may not work because all lines are aligned to center, and they look messy on the left border. The solution is using a minipage to align text inside figure.
\begin{figure}[t]
\centering
\begin{minipage}[t]{0.5\textwidth}
this is first line \\
\hspace*{0.5cm} this is second line \\
this is third line \\
this is fourth line
\end{minipage}
\caption{caption}
\label{l}
\end{figure}
\begin{figure}[t]
\centering
this is first line \\
\hspace*{0.5cm} this is second line \\
this is third line \\
this is fourth line
\caption{caption}
\label{l}
\end{figure}
The above may not work because all lines are aligned to center, and they look messy on the left border. The solution is using a minipage to align text inside figure.
\begin{figure}[t]
\centering
\begin{minipage}[t]{0.5\textwidth}
this is first line \\
\hspace*{0.5cm} this is second line \\
this is third line \\
this is fourth line
\end{minipage}
\caption{caption}
\label{l}
\end{figure}
Wednesday, April 11, 2007
Latex note: protect citations in caption
\caption{xxx \cite{yyy}} won't work, it won't compile with latex sometimes.
use \caption{xxx {\protect \cite{yyy}}} instead.
use \caption{xxx {\protect \cite{yyy}}} instead.
Saturday, March 31, 2007
Python note: os.path.join unexpected result for paths beginning with "/" or "\"
Need to pay attention when joining paths beginning with "/" (or "\" for windows).
os.path.join("c:\abc", "def", "ghi") -> no problem, c:\abc\def\ghi
os.path.join("c:\abc", "\def") -> outputs "\def".
os.path.join("c:\abc", "def", "ghi") -> no problem, c:\abc\def\ghi
os.path.join("c:\abc", "\def") -> outputs "\def".
Friday, March 23, 2007
C++ note: be careful with unsigned
I just made a mistake with an unsigned integer. It took me nearly an hour to debug, just to find that the subtle bug is caused by this:
if (a<b-2) ...
a = 0 b = 1
b is an unsigned integer.
The reason is that b-2 is interpreted as unsigned integer, and therefore the predicate wrong. The conclusion is that we should avoid using many minuses for unsigned.
if (a+2<b) is the solution for this problem
if (a<b-2) ...
a = 0 b = 1
b is an unsigned integer.
The reason is that b-2 is interpreted as unsigned integer, and therefore the predicate wrong. The conclusion is that we should avoid using many minuses for unsigned.
if (a+2<b) is the solution for this problem
Friday, January 12, 2007
Latex note: convert Excel charts to eps files
This tip applies to any chart from MS Office.
In short, in order to insert charts from Excel to your Latex document, you need to (1) print your chart from MS Office into an eps file; (2) modify the bounding box in the output.
You need to install Generic Postscript Printer from Adobe first. Link the printer to the port FILE: . After installation, set its output option from Property -> Printing preferences... -> Advanced -> Postscript options -> Postscript output opion to encapsulated postscript format (eps).
This printer can be used to print the chart to an eps file.
The EPS file contains borders which defines where the chart is. Use postscript to open this EPS file, then use a text editor to open it simultaneously. Tick options -> eps clip from postscript. Then edit the line %%BoundingBox: ... from the text editor. Adjust the size of the chart in eps so that the clip fits well to the chart. Then save EPS.
Now you are done. Insert the chart into latex!
In short, in order to insert charts from Excel to your Latex document, you need to (1) print your chart from MS Office into an eps file; (2) modify the bounding box in the output.
You need to install Generic Postscript Printer from Adobe first. Link the printer to the port FILE: . After installation, set its output option from Property -> Printing preferences... -> Advanced -> Postscript options -> Postscript output opion to encapsulated postscript format (eps).
This printer can be used to print the chart to an eps file.
The EPS file contains borders which defines where the chart is. Use postscript to open this EPS file, then use a text editor to open it simultaneously. Tick options -> eps clip from postscript. Then edit the line %%BoundingBox: ... from the text editor. Adjust the size of the chart in eps so that the clip fits well to the chart. Then save EPS.
Now you are done. Insert the chart into latex!
Tuesday, November 14, 2006
C++ note: be careful with ifstream
The following is the correct and actually standard way of reading something
while(file.good())
{
file >> s;
cout << s;
}
However, the following is wrong:
while(file.good())
{
file.get(t);
cout << t ;
}
it will normally repeat the last char in the file. The reason is that file.good only changes after get() is called. This is the way
while(file.get(t))
{
cout << t ;
}
file.get(t) will return success if the process works.
while(file.good())
{
file >> s;
cout << s;
}
However, the following is wrong:
while(file.good())
{
file.get(t);
cout << t ;
}
it will normally repeat the last char in the file. The reason is that file.good only changes after get() is called. This is the way
while(file.get(t))
{
cout << t ;
}
file.get(t) will return success if the process works.
Wednesday, November 08, 2006
C++ note: TRACE macros
#ifdef DEBUG
#define TRACE(x) { cout << x << endl; cout.flush(); }
#else
#define TRACE(x)
#endif
#ifdef DEBUG
#define RUN_ON_DEBUG(x) x
#else
#define RUN_ON_DEBUG(x)
#endif
#define TRACE(x) { cout << x << endl; cout.flush(); }
#else
#define TRACE(x)
#endif
#ifdef DEBUG
#define RUN_ON_DEBUG(x) x
#else
#define RUN_ON_DEBUG(x)
#endif
Tuesday, October 31, 2006
Python note: the value and the display of a unicode string
Sometimes it is necessary to view the unicode encoding of a special character or to see the display of a certain unicode code. Here is some example code.
print '\xe6\x8d\xae'.decode("utf-8")
<displays the unicode character>
print repr(<the unicode character here>)
'\xe6\x8d\xae'
print '\xe6\x8d\xae'.decode("utf-8")
<displays the unicode character>
print repr(<the unicode character here>)
'\xe6\x8d\xae'
Friday, September 29, 2006
wxPython note: about callbacks
Be careful about callbacks, for they sometimes bring unexpected logics. The process is not written explicitly in the code, and therefore may cause traps, just like aspect oriented programming. For example, I just wrote a callback from notebook page change. Then I used some manual calls to change the notebook pages. I did not notice that those manual calls also brought up callbacks, and therefore messing the logic. This problem is fixed by introducing a boolean variable bManual to indicate whether a page switch is done by pressing manually or by a function call. Whenever the notebook page is switched by function calls, it is marked with the variable.
Thinking of aspect oriented programming, sometimes a program is added with new functionalities and thus become larger all the time. Apart from using SVN to monitor the changes, it is also possible to use some special end of line comments to clarify. For example, some functionalities can be marked with # [-functionality-]. Then in further editing, we simply check the tags to find which code does the job. However, this approach should be used carefully so that all code and comments are consistent.
Thinking of aspect oriented programming, sometimes a program is added with new functionalities and thus become larger all the time. Apart from using SVN to monitor the changes, it is also possible to use some special end of line comments to clarify. For example, some functionalities can be marked with # [-functionality-]. Then in further editing, we simply check the tags to find which code does the job. However, this approach should be used carefully so that all code and comments are consistent.
Thursday, September 28, 2006
Python note: module subprocess provides functionalities to open arbitrary file
To open an email attachment in some email client, the client needs to make new processes which opens the corresponding file. This can be done by using the subprocess module, which spawns new process and opens the file.
import subprocess
subprocess.Popen(filename, shell=True)
import subprocess
subprocess.Popen(filename, shell=True)
Sunday, September 03, 2006
C++ note: about strings
length - s.length() / s.size()
index - s[1] = 'x' / s[2]
slice - s.substr(1,4) means starting from 1, length 4.
append - s.append(t)
replace - s.replace(1,3, 'x') replaces slice (1,3)
erase - s.erase(1,2) same meaning
find - s.find("abc",2) find from index 2
reverse - reverse(s.begin(), s.end()) modifies strs.
It's alright concatenating strings with +
The constructor can also be s = string(t, start, length).
index - s[1] = 'x' / s[2]
slice - s.substr(1,4) means starting from 1, length 4.
append - s.append(t)
replace - s.replace(1,3, 'x') replaces slice (1,3)
erase - s.erase(1,2) same meaning
find - s.find("abc",2) find from index 2
reverse - reverse(s.begin(), s.end()) modifies strs.
It's alright concatenating strings with +
The constructor can also be s = string(t, start, length).
Monday, August 28, 2006
C++ note: int to string
import <string>
import <sstream>
using namespace std;
int main() {
...string s;
...int a=1;
...stringstream ss;
...ss << a;
...ss >> s;
}
also, use atoi or strtol for string to int (cstdlib).
use s = c to assign directly from C string to string, and use s.c_str() to get c string from string
import <sstream>
using namespace std;
int main() {
...string s;
...int a=1;
...stringstream ss;
...ss << a;
...ss >> s;
}
also, use atoi or strtol for string to int (cstdlib).
use s = c to assign directly from C string to string, and use s.c_str() to get c string from string
Saturday, July 08, 2006
Python note: __hash__
__hash__ provides the hash code for an object. It is used under two circumstances: when the object is put in a dictionary; or when hash() built-in function is called.
There is a requirement for this method - two objects should have the same hash when their __cmp__ returns zero (they are equal). Thus when a class does not provide __cmp__ member function, it should not provide __hash__.
There is a requirement for this method - two objects should have the same hash when their __cmp__ returns zero (they are equal). Thus when a class does not provide __cmp__ member function, it should not provide __hash__.
Friday, July 07, 2006
VIM note: tabs
In VIM 7, tab view is supported.
To open a new tab, use :tabnew
To navigate among tabs, use 'gt'
To close a tab, use ':tabclose'
To open a new tab, use :tabnew
To navigate among tabs, use 'gt'
To close a tab, use ':tabclose'
Tuesday, June 06, 2006
Python note: unicode example (switch utf to gb2312)
This is a script to switch files from unicode to gb2312
# gb2utf - switch encoding between text
# Yue Zhang 2006
import sys
iFile = open(sys.argv[1])
oFile = open(sys.argv[2], "w")
sLine = iFile.readline()
while sLine:
...try:
......uLine = sLine.decode("gb2312")
...except UnicodeDecodeError:
......sLine = iFile.readline()
......continue
...oFile.write(uLine.encode("utf8")) # note this.
...sLine = iFile.readline()
iFile.close()
oFile.close()
# gb2utf - switch encoding between text
# Yue Zhang 2006
import sys
iFile = open(sys.argv[1])
oFile = open(sys.argv[2], "w")
sLine = iFile.readline()
while sLine:
...try:
......uLine = sLine.decode("gb2312")
...except UnicodeDecodeError:
......sLine = iFile.readline()
......continue
...oFile.write(uLine.encode("utf8")) # note this.
...sLine = iFile.readline()
iFile.close()
oFile.close()
Subscribe to:
Posts (Atom)