tag:blogger.com,1999:blog-51603285311193973072024-03-13T00:44:24.472-04:00Keep MovingA Blog for programming & technical solutionsAchilleshttp://www.blogger.com/profile/06941725483326536858noreply@blogger.comBlogger72125tag:blogger.com,1999:blog-5160328531119397307.post-29228775714977083872012-10-10T17:50:00.000-04:002012-10-10T17:50:09.083-04:00Include jQuery in the Browser JavaScript ConsoleTo play around with jQuery in your browser's JavaScript Console, copy and paste the following lines!
<pre class='prettyprint'>
var jq = document.createElement('script');
jq.src = "http://code.jquery.com/jquery-latest.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
jQuery.noConflict();
</pre>Anonymousnoreply@blogger.com1tag:blogger.com,1999:blog-5160328531119397307.post-37966458614972878372012-07-12T14:33:00.001-04:002012-07-12T14:35:27.525-04:00Using changelist for SVNChangelist is useful for organizing your code changes when you're working on more than one projects. You can mark files with different changelist flags to keep them organized.<br />
<br />
For example, we changed three files for ticket 66, and then commit them all together later:
<br />
<pre class="prettyprint">$ svn changelist ticket66 a.py b.py c.py
Path 'a.py' is now a member of changelist 'ticket66'.
Path 'b.py' is now a member of changelist 'ticket66'.
Path 'c.py' is now a member of changelist 'ticket66'.
</pre>
<pre class="prettyprint">$ svn status
A someotherfile.py
A test/sometest.py
--- Changelist 'ticket 66':
A a.py
A b.py
A c.py
</pre>
<pre class="prettyprint">$ svn commit --changelist ticket66 -m "Fixing ticket 66."
Adding a.py
Adding b.py
Adding c.py
Transmitting file data ...
Committed revision 2.
</pre>Anonymousnoreply@blogger.com1tag:blogger.com,1999:blog-5160328531119397307.post-40014682820900797982012-05-09T15:29:00.003-04:002012-05-09T15:29:53.532-04:00Integer validation in JavaScript<pre class="prettyprint">
function isInteger(val) {
var intRegEx = /\D/g;
return (!intRegEx.test(val));
}
</pre>Anonymousnoreply@blogger.com1tag:blogger.com,1999:blog-5160328531119397307.post-56801674944749238502012-03-20T10:03:00.000-04:002012-03-20T10:03:31.098-04:00Request the parent window to open a URL from an iframe with different domainsSometime we are developing something within an iframe, but we want the parent window to refresh or open a URL. The code to use is:<br />
<pre class='prettyprint'>top.location.href = URL;
</pre>Achilleshttp://www.blogger.com/profile/06941725483326536858noreply@blogger.com1tag:blogger.com,1999:blog-5160328531119397307.post-32775715046832186982012-03-08T10:30:00.000-05:002012-03-08T10:30:17.603-05:00SVN commands<b>Create a patch</b><br />
svn diff > ~/fix_ugly_bug.diff<br />
<br />
<b>Apply a patch</b><br />
patch -p0 -i ~/fix_ugly_bug.diffAchilleshttp://www.blogger.com/profile/06941725483326536858noreply@blogger.com1tag:blogger.com,1999:blog-5160328531119397307.post-89925474026319407632012-03-05T16:01:00.000-05:002012-03-05T16:01:29.571-05:00Text lineup using html span tag<pre class="prettyprint"><style type="text/css">
span {
display: inline-block;
width: 50px;
}
</style>
</pre>Achilleshttp://www.blogger.com/profile/06941725483326536858noreply@blogger.com1tag:blogger.com,1999:blog-5160328531119397307.post-10003726070402489622012-01-31T12:52:00.003-05:002012-01-31T12:54:53.261-05:00Intersection of lists in Python<pre class='prettyprint'>def intersect(a, b):<br /> return list(set(a) & set(b))<br /></pre>Anonymousnoreply@blogger.com1tag:blogger.com,1999:blog-5160328531119397307.post-62756688032217481362011-12-17T17:35:00.000-05:002011-12-17T17:35:01.403-05:00Install software from source on a shared Linux serverIt's a very common task that installing a software from source on a shared Linux server. If you have root privilege, it suffice to install the package by "sudo apt-get install package_name" on Ubuntu or Fedora, or "yum install package_name" on redhat or centOS. However, the common case is that we do not have root privilege and do not have write access to folders other than $HOME. To install a software or package in this situation, we have to build it from source as follows.<br />
<br />
<pre class='prettyprint'>$ mkdir -p ~/tmp # create a temp folder for installation process
$ export TEMP=~/tmp # set it to be the sys tmp folder
$ wget url # get the package source from URL, or you can upload it to the server by sftp
$ unzip file # if the package is .zip
$ tar xf file # if the package is a tar ball
$ cd source # source should be replaced by the folder you just unpacked from .zip or .tar
$ ./configure --prefix=$HOME
$ make
$ make install
$ make clean
</pre>Achilleshttp://www.blogger.com/profile/06941725483326536858noreply@blogger.com1tag:blogger.com,1999:blog-5160328531119397307.post-16661350667059888962011-12-15T14:42:00.005-05:002011-12-15T17:58:14.471-05:00Fibonacci calculation algorithm<b>Iterative version:</b><br />
<pre class='prettyprint'>int fibonacci(int n) {
if (n < 2)
return n;
int prev = 0;
int last = 1;
for (int i=2; i<=n; ++i) {
prev = last;
last = prev + last;
}
return last;
}
</pre>
The time complexity is O(n) while the space complexity is O(3) or constant.<br/><br/>
<b>Recursive version:</b>
<pre class='prettyprint'>int fib(int n) {
if (n < 2)
return n;
return fib(n - 1) + fib(n - 2);
}
</pre>
However, this straightforward solution for Fibonacci is extremely inefficient. Since the computation complexity would be exponential as many text books indicated. An intelligent solution is given below, which is O(n). The origin of this solution is <a href="http://www.cs.northwestern.edu/academics/courses/110/html/fib_rec.html" target='_blank'>here</a>.
<pre class='prettyprint'>int fib(int n, int p0=0, int p1=1) {
if (n==0)
return 0;
else if (n==1)
return p1;
else
return fib(n - 1, p1, p0 + p1);
}
</pre>There are recursive solutions are doing even better than O(n). See this <a href="http://knol.google.com/k/efficient-recursive-computation-of-fibonacci-numbers">link</a>.Achilleshttp://www.blogger.com/profile/06941725483326536858noreply@blogger.com1tag:blogger.com,1999:blog-5160328531119397307.post-76762186870487487112011-12-12T21:35:00.000-05:002011-12-12T21:35:29.175-05:00Find the ith smallest element of the array in O(n)How to find the ith smallest element of the array in O(n) time is a frequently asked question. The straightforward solution is sorting the array and return the ith element. However, for general cases, it's proven that sorting algorithms like merge sorting, is lower bound to O(nlogn). Thus finding the ith smallest element is unfeasible by a merge sorting. Although for some special cases, such as, the possible values or data range of the array is relatively small, counting sorting can give a O(n) solution, it doesn't work for general cases. The solution below works generally and the expected running time is O(n).<br />
<pre class="prettyprint">int find_ith_smallest_elem(int* A, int left, int right, int i) {
if (left==right) //only one element left, then it's the answer
return A[left];
pivot = random_partition(A,left,right);
left_partition_length = pivot - left + 1; //1 counts for the pivot itself
if (i==left_partition_length)
return A[pivot]; //pivot is the answer
else if (i<left_partition_length)
return find_ith_smallest_elem(A,left,pivot-1,i);
else
return find_ith_smallest_elem(A,pivot+1,right,i-left_partition_length);
}
//randomly choose a pivot and partition the array into two parts, where the left part only
//contains elements smaller than the pivot while the right part only contains larger ones
int random_partition(int* A, int left, int right) {
int pivot = rand(left,right);
swap(A,pivot,right); // swap the pivot and the right-most element
int pivot_value = A[right];
int i = left - 1;
for(int j=left; j<right; ++j) {
if (A[j]<=pivot_value) {
i++;
swap(A,i,j);
}
}
swap(A,i+1,right);
return i+1;
}
</pre>Achilleshttp://www.blogger.com/profile/06941725483326536858noreply@blogger.com1tag:blogger.com,1999:blog-5160328531119397307.post-78579009387983931482011-12-08T11:00:00.017-05:002011-12-08T23:44:05.577-05:00JSON Parsing in java with JacksonWhen it comes to API data and results (like google APIs), json is of great favor to java developers. There are more than 50 libraries for json processing in different languages. To get an overview of json structure and access the libraries, please refer to <a href="http://www.json.org/">http://www.json.org/</a>.<br />
<br />
Jackson offers three alternative methods for processing JSON:<br />
<ul><li><span class="Apple-style-span" style="color: #3d85c6;">Streaming API</span> reads and writes JSON content as discrete events.</li>
<li><span class="Apple-style-span" style="color: #3d85c6;">Tree Model</span> provides a mutable in-memory tree representation of a JSON document.</li>
<li><span class="Apple-style-span" style="color: #3d85c6;">Data Binding</span> converts JSON to and from POJOs based either on property accessor conventions or annotations.</li>
</ul><br />
For a known structure of API return and the requirement to reconstruct similar data skeleton, it would be effortless if <span class="Apple-style-span" style="color: #3d85c6;">Tree Model</span> is used for the recovery.<br />
<br />
To download the latest version of Jackson, please refer to:<a href="http://wiki.fasterxml.com/JacksonDownload">http://wiki.fasterxml.com/JacksonDownload</a>. Both jackson.core and jackson.mapper will be needed for <span class="font">Tree Model</span> processing.<br />
<br />
To add the dependencies from internal jars in Eclipse, refer to:<a href="http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java)">How to Add JARs to Project Build Paths in Eclipse</a><br />
<br />
<a href="http://wiki.fasterxml.com/JacksonTreeModel">Tree Model Processing Step by Step</a>Josiehttp://www.blogger.com/profile/01837005103915645854noreply@blogger.com1tag:blogger.com,1999:blog-5160328531119397307.post-44652545751343593152011-12-07T15:46:00.000-05:002011-12-07T15:46:51.471-05:00Facebook JavaScript SDK Call after FB.init()When developing Facebook Apps, the JavaScript SDK is usually loaded asynchronously to not block loading other elements of your page. However, it leaves us a problem: every JavaScript SDK call must be made after the SDK loading. Otherwise, a "FB not defined" error will be thrown. There are two common ways to handle this situation.<br />
<br />
<b>Put the codes inside the callback function of fbAsyncInit process</b><br />
<pre class="prettyprint"><div id="fb-root">
</div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR_APP_ID',
channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html',
status : true,
cookie : true,
oauth : true,
xfbml : true
});
// Put Your Codes Here
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
</pre><br />
<b>Add delay to your SDK call</b><br />
<pre class='prettyprint'>/**
* do something when user leaves a new comment
*/
function fb_event_subscribe() {
try {
FB.Event.subscribe('comment.create', function(response) {
// do something here
});
}
catch(err) {
setTimeout("fb_event_subscribe()",1000); // try again later
}
};
/**
* subscribe the Facebook event "comment.create" after a one second delay, which
* makes sure FB JavaScript SDK asynchronous initialization FB.init() is finished.
*/
setTimeout("fb_event_subscribe()",1000);
</pre>Achilleshttp://www.blogger.com/profile/06941725483326536858noreply@blogger.com1tag:blogger.com,1999:blog-5160328531119397307.post-25855674418109377882011-11-29T16:58:00.008-05:002011-12-01T13:14:49.035-05:00Iterate over a Map(HashMap) in JavaHere are three methods of iterating through a Map(HashMap) in Java and their pros and cons. They all works for any map implementation in Java, e.g. HashMap, TreeMap, HashTable,LinkedHashMap.<br /><br /><b>Method #1</b><br /><span class="Apple-style-span" ><span class="Apple-style-span" >Iterating over Entries</span><br /></span><div><b><span class="Apple-style-span" style="font-weight: normal; "><pre class="lang-java prettyprint" style="font-size: 12px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: rgb(187, 187, 187); border-right-color: rgb(187, 187, 187); border-bottom-color: rgb(187, 187, 187); border-left-color: rgb(187, 187, 187); padding-top: 10px; padding-right: 0px; padding-bottom: 10px; padding-left: 15px; max-height: 600px; overflow-x: auto; overflow-y: auto; background-color: rgb(253, 253, 253); width: 645px; margin-top: 5px; margin-right: auto; margin-bottom: 10px; margin-left: auto; "><code style="font-size: 14px; color: rgb(0, 0, 192); "><span class="typ" style="color: rgb(0, 0, 0); ">Map<</span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">,</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">>;</span><span class="pln" style="color: rgb(0, 0, 0); "> map </span><span class="pun" style="color: rgb(0, 0, 0); ">=</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold; ">new</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="typ" style="color: rgb(0, 0, 0); ">HashMap<</span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">,</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">>();</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold; ">for</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">(</span><span class="typ" style="color: rgb(0, 0, 0); ">Map</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="typ" style="color: rgb(0, 0, 0); ">Entry</span><span class="pun" style="color: rgb(0, 0, 0); "><</span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">,</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">></span><span class="pln" style="color: rgb(0, 0, 0); "> entry </span><span class="pun" style="color: rgb(0, 0, 0); ">:</span><span class="pln" style="color: rgb(0, 0, 0); "> map</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">entrySet</span><span class="pun" style="color: rgb(0, 0, 0); ">())</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">{</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="typ" style="color: rgb(0, 0, 0); ">System</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">out</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">println</span><span class="pun" style="color: rgb(0, 0, 0); ">(</span><span class="str" style="color: rgb(42, 0, 255); ">"Key = "</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">+</span><span class="pln" style="color: rgb(0, 0, 0); "> entry</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">getKey</span><span class="pun" style="color: rgb(0, 0, 0); ">()</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">+</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="str" style="color: rgb(42, 0, 255); ">", Value = "</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">+</span><span class="pln" style="color: rgb(0, 0, 0); "> entry</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">getValue</span><span class="pun" style="color: rgb(0, 0, 0); ">());</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="pun" style="color: rgb(0, 0, 0); ">}</span></code></pre></span></b><integer, integer=""><integer, integer=""><integer, integer="">pros : You can get both keys and values via entrySet().<br />cons: NullPointerException will be thrown if you try to iterate over a null map. You should always check for null references before iterating.<br /><br /><b>Method #2</b><br /><span class="Apple-style-span" >Iterating over either Key or Value Sets<br /></span><span class="Apple-style-span"><pre class="lang-java prettyprint" style="font-size: 12px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: rgb(187, 187, 187); border-right-color: rgb(187, 187, 187); border-bottom-color: rgb(187, 187, 187); border-left-color: rgb(187, 187, 187); padding-top: 10px; padding-right: 0px; padding-bottom: 10px; padding-left: 15px; max-height: 600px; overflow-x: auto; overflow-y: auto; background-color: rgb(253, 253, 253); width: 645px; margin-top: 5px; margin-right: auto; margin-bottom: 10px; margin-left: auto; "><code style="font-size: 14px; color: rgb(0, 0, 192); "><span class="typ" style="color: rgb(0, 0, 0); ">Map</span><span class="pun" style="color: rgb(0, 0, 0); "><</span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">,</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">></span><span class="pln" style="color: rgb(0, 0, 0); "> map </span><span class="pun" style="color: rgb(0, 0, 0); ">=</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold; ">new</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="typ" style="color: rgb(0, 0, 0); ">HashMap</span><span class="pun" style="color: rgb(0, 0, 0); "><</span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">,</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">>();</span><span class="pln" style="color: rgb(0, 0, 0); "><br /><br /></span><span class="com" style="color: rgb(63, 127, 95); ">//iterating over keys only</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold; ">for</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">(</span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pln" style="color: rgb(0, 0, 0); "> key </span><span class="pun" style="color: rgb(0, 0, 0); ">:</span><span class="pln" style="color: rgb(0, 0, 0); "> map</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">keySet</span><span class="pun" style="color: rgb(0, 0, 0); ">())</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">{</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="typ" style="color: rgb(0, 0, 0); ">System</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">out</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">println</span><span class="pun" style="color: rgb(0, 0, 0); ">(</span><span class="str" style="color: rgb(42, 0, 255); ">"Key = "</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">+</span><span class="pln" style="color: rgb(0, 0, 0); "> key</span><span class="pun" style="color: rgb(0, 0, 0); ">);</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="pun" style="color: rgb(0, 0, 0); ">}</span><span class="pln" style="color: rgb(0, 0, 0); "><br /><br /></span><span class="com" style="color: rgb(63, 127, 95); ">//iterating over values only</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold; ">for</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">(</span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pln" style="color: rgb(0, 0, 0); "> value </span><span class="pun" style="color: rgb(0, 0, 0); ">:</span><span class="pln" style="color: rgb(0, 0, 0); "> map</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">values</span><span class="pun" style="color: rgb(0, 0, 0); ">())</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">{</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="typ" style="color: rgb(0, 0, 0); ">System</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">out</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">println</span><span class="pun" style="color: rgb(0, 0, 0); ">(</span><span class="str" style="color: rgb(42, 0, 255); ">"Value = "</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">+</span><span class="pln" style="color: rgb(0, 0, 0); "> value</span><span class="pun" style="color: rgb(0, 0, 0); ">);</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="pun" style="color: rgb(0, 0, 0); ">}</span></code></pre></span>pros: If all you need is either key sets or value sets, this method is about 10% faster than iteration over entrySet(), and also cleaner.</integer,></integer,></integer,></div><div><integer, integer=""><integer, integer=""><integer, integer="">cons: The same as above<br /><br /><b>Method #3</b><br /><span class="Apple-style-span" >Iterating using Iterator<br /></span></integer,></integer,></integer,></div><div><span class="Apple-style-span" >--With Generics</span></div><div><integer, integer=""><integer, integer=""><integer, integer=""><span class="Apple-style-span" style="color: rgb(51, 51, 51); "><pre class="lang-java prettyprint" style="font-size: 12px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: rgb(187, 187, 187); border-right-color: rgb(187, 187, 187); border-bottom-color: rgb(187, 187, 187); border-left-color: rgb(187, 187, 187); padding-top: 10px; padding-right: 0px; padding-bottom: 10px; padding-left: 15px; max-height: 600px; overflow-x: auto; overflow-y: auto; background-color: rgb(253, 253, 253); width: 645px; margin-top: 5px; margin-right: auto; margin-bottom: 10px; margin-left: auto; "><code style="font-size: 14px; color: rgb(0, 0, 192); "><span class="typ" style="color: rgb(0, 0, 0); ">Map</span><span class="pun" style="color: rgb(0, 0, 0); "><</span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">,</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">></span><span class="pln" style="color: rgb(0, 0, 0); "> map </span><span class="pun" style="color: rgb(0, 0, 0); ">=</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold; ">new</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="typ" style="color: rgb(0, 0, 0); ">HashMap</span><span class="pun" style="color: rgb(0, 0, 0); "><</span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">,</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">>();</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="typ" style="color: rgb(0, 0, 0); ">Iterator</span><span class="pun" style="color: rgb(0, 0, 0); "><</span><span class="typ" style="color: rgb(0, 0, 0); ">Map</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="typ" style="color: rgb(0, 0, 0); ">Entry</span><span class="pun" style="color: rgb(0, 0, 0); "><</span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">,</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">>></span><span class="pln" style="color: rgb(0, 0, 0); "> entries </span><span class="pun" style="color: rgb(0, 0, 0); ">=</span><span class="pln" style="color: rgb(0, 0, 0); "> map</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">entrySet</span><span class="pun" style="color: rgb(0, 0, 0); ">().</span><span class="pln" style="color: rgb(0, 0, 0); ">iterator</span><span class="pun" style="color: rgb(0, 0, 0); ">();</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold; ">while</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">(</span><span class="pln" style="color: rgb(0, 0, 0); ">entries</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">hasNext</span><span class="pun" style="color: rgb(0, 0, 0); ">())</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">{</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="typ" style="color: rgb(0, 0, 0); ">Map</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="typ" style="color: rgb(0, 0, 0); ">Entry</span><span class="pun" style="color: rgb(0, 0, 0); "><</span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">,</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">></span><span class="pln" style="color: rgb(0, 0, 0); "> entry </span><span class="pun" style="color: rgb(0, 0, 0); ">=</span><span class="pln" style="color: rgb(0, 0, 0); "> entries</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">next</span><span class="pun" style="color: rgb(0, 0, 0); ">();</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="typ" style="color: rgb(0, 0, 0); ">System</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">out</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">println</span><span class="pun" style="color: rgb(0, 0, 0); ">(</span><span class="str" style="color: rgb(42, 0, 255); ">"Key = "</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">+</span><span class="pln" style="color: rgb(0, 0, 0); "> entry</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">getKey</span><span class="pun" style="color: rgb(0, 0, 0); ">()</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">+</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="str" style="color: rgb(42, 0, 255); ">", Value = "</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">+</span><span class="pln" style="color: rgb(0, 0, 0); "> entry</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">getValue</span><span class="pun" style="color: rgb(0, 0, 0); ">());</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="pun" style="color: rgb(0, 0, 0); ">}</span></code></pre></span></integer,></integer,></integer,></div><div><integer, integer=""><integer, integer=""><integer, integer=""><span class="Apple-style-span" >--Without Generics</span></integer,></integer,></integer,></div><div><integer, integer=""><integer, integer=""><integer, integer=""><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: georgia; "><pre class="lang-java prettyprint" style="font-size: 12px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: rgb(187, 187, 187); border-right-color: rgb(187, 187, 187); border-bottom-color: rgb(187, 187, 187); border-left-color: rgb(187, 187, 187); padding-top: 10px; padding-right: 0px; padding-bottom: 10px; padding-left: 15px; max-height: 600px; overflow-x: auto; overflow-y: auto; background-color: rgb(253, 253, 253); width: 645px; margin-top: 5px; margin-right: auto; margin-bottom: 10px; margin-left: auto; "><code style="font-size: 14px; color: rgb(0, 0, 192); "><span class="typ" style="color: rgb(0, 0, 0); ">Map</span><span class="pln" style="color: rgb(0, 0, 0); "> map </span><span class="pun" style="color: rgb(0, 0, 0); ">=</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold; ">new</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="typ" style="color: rgb(0, 0, 0); ">HashMap</span><span class="pun" style="color: rgb(0, 0, 0); ">();</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="typ" style="color: rgb(0, 0, 0); ">Iterator</span><span class="pln" style="color: rgb(0, 0, 0); "> entries </span><span class="pun" style="color: rgb(0, 0, 0); ">=</span><span class="pln" style="color: rgb(0, 0, 0); "> map</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">entrySet</span><span class="pun" style="color: rgb(0, 0, 0); ">().</span><span class="pln" style="color: rgb(0, 0, 0); ">iterator</span><span class="pun" style="color: rgb(0, 0, 0); ">();</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold; ">while</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">(</span><span class="pln" style="color: rgb(0, 0, 0); ">entries</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">hasNext</span><span class="pun" style="color: rgb(0, 0, 0); ">())</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">{</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="typ" style="color: rgb(0, 0, 0); ">Map</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="typ" style="color: rgb(0, 0, 0); ">Entry</span><span class="pln" style="color: rgb(0, 0, 0); "> entry </span><span class="pun" style="color: rgb(0, 0, 0); ">=</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">(</span><span class="typ" style="color: rgb(0, 0, 0); ">Map</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="typ" style="color: rgb(0, 0, 0); ">Entry</span><span class="pun" style="color: rgb(0, 0, 0); ">)</span><span class="pln" style="color: rgb(0, 0, 0); "> entries</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">next</span><span class="pun" style="color: rgb(0, 0, 0); ">();</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pln" style="color: rgb(0, 0, 0); "> key </span><span class="pun" style="color: rgb(0, 0, 0); ">=</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">(</span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">)</span><span class="pln" style="color: rgb(0, 0, 0); ">entry</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">getKey</span><span class="pun" style="color: rgb(0, 0, 0); ">();</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pln" style="color: rgb(0, 0, 0); "> value </span><span class="pun" style="color: rgb(0, 0, 0); ">=</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">(</span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">)</span><span class="pln" style="color: rgb(0, 0, 0); ">entry</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">getValue</span><span class="pun" style="color: rgb(0, 0, 0); ">();</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="typ" style="color: rgb(0, 0, 0); ">System</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">out</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">println</span><span class="pun" style="color: rgb(0, 0, 0); ">(</span><span class="str" style="color: rgb(42, 0, 255); ">"Key = "</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">+</span><span class="pln" style="color: rgb(0, 0, 0); "> key </span><span class="pun" style="color: rgb(0, 0, 0); ">+</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="str" style="color: rgb(42, 0, 255); ">", Value = "</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">+</span><span class="pln" style="color: rgb(0, 0, 0); "> value</span><span class="pun" style="color: rgb(0, 0, 0); ">);</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="pun" style="color: rgb(0, 0, 0); ">}</span></code></pre></span></integer,></integer,></integer,></div><div>The same method can be used for obtaining the key and value sets respectively. Its performance is equal to for-each loop.</div><div>pros: It is the only way if you want to remove entries during iteration by implementing iterator.remove(). If you do this in for-each loop, you will get unpredictable results.</div><div>cons: Look a little redundant than other methods.</div><div><b><br /></b></div><b>Method #4</b><br /><span class="Apple-style-span" style="color: rgb(51, 102, 255); font-family: arial; ">Iterating over key sets and search values </span><span class="Apple-style-span" ><span class="Apple-style-span" style="font-family: arial; ">(</span><span class="Apple-style-span" style="font-family: arial; ">inefficient</span><span class="Apple-style-span" style="font-family: arial; ">)</span></span><br /><div><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: georgia; "><pre class="lang-java prettyprint" style="font-size: 12px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: rgb(187, 187, 187); border-right-color: rgb(187, 187, 187); border-bottom-color: rgb(187, 187, 187); border-left-color: rgb(187, 187, 187); padding-top: 10px; padding-right: 0px; padding-bottom: 10px; padding-left: 15px; max-height: 600px; overflow-x: auto; overflow-y: auto; background-color: rgb(253, 253, 253); width: 645px; margin-top: 5px; margin-right: auto; margin-bottom: 10px; margin-left: auto; "><code style="font-size: 14px; color: rgb(0, 0, 192); "><span class="typ" style="color: rgb(0, 0, 0); ">Map</span><span class="pun" style="color: rgb(0, 0, 0); "><</span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">,</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">></span><span class="pln" style="color: rgb(0, 0, 0); "> map </span><span class="pun" style="color: rgb(0, 0, 0); ">=</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold; ">new</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="typ" style="color: rgb(0, 0, 0); ">HashMap</span><span class="pun" style="color: rgb(0, 0, 0); "><</span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">,</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pun" style="color: rgb(0, 0, 0); ">>();</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold; ">for</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">(</span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pln" style="color: rgb(0, 0, 0); "> key </span><span class="pun" style="color: rgb(0, 0, 0); ">:</span><span class="pln" style="color: rgb(0, 0, 0); "> map</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">keySet</span><span class="pun" style="color: rgb(0, 0, 0); ">())</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">{</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="typ" style="color: rgb(0, 0, 0); ">Integer</span><span class="pln" style="color: rgb(0, 0, 0); "> value </span><span class="pun" style="color: rgb(0, 0, 0); ">=</span><span class="pln" style="color: rgb(0, 0, 0); "> map</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">get</span><span class="pun" style="color: rgb(0, 0, 0); ">(</span><span class="pln" style="color: rgb(0, 0, 0); ">key</span><span class="pun" style="color: rgb(0, 0, 0); ">);</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="typ" style="color: rgb(0, 0, 0); ">System</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">out</span><span class="pun" style="color: rgb(0, 0, 0); ">.</span><span class="pln" style="color: rgb(0, 0, 0); ">println</span><span class="pun" style="color: rgb(0, 0, 0); ">(</span><span class="str" style="color: rgb(42, 0, 255); ">"Key = "</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">+</span><span class="pln" style="color: rgb(0, 0, 0); "> key </span><span class="pun" style="color: rgb(0, 0, 0); ">+</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="str" style="color: rgb(42, 0, 255); ">", Value = "</span><span class="pln" style="color: rgb(0, 0, 0); "> </span><span class="pun" style="color: rgb(0, 0, 0); ">+</span><span class="pln" style="color: rgb(0, 0, 0); "> value</span><span class="pun" style="color: rgb(0, 0, 0); ">);</span><span class="pln" style="color: rgb(0, 0, 0); "><br /></span><span class="pun" style="color: rgb(0, 0, 0); ">}</span></code></pre></span></div>Getting values by keys is inefficient and time-consuming, in different Maps it can be 20% to 200% slower than <b>Method #1</b>.Josiehttp://www.blogger.com/profile/01837005103915645854noreply@blogger.com1tag:blogger.com,1999:blog-5160328531119397307.post-79067540790225044292011-11-29T14:02:00.000-05:002011-11-29T14:02:46.843-05:00Arrow keys not working in Vim on CygwinInstalled Cygwin today on my machine (ThinkPad, XP SP3). The arrow keys in Vim always bring me out from insert mode. And there is no syntax highlight at all.<br />
<br />
The quick fix is copy /usr/share/vim/vim73/vimrc_example.vim to ~/.vimrc. Then everything works perfectly now.Achilleshttp://www.blogger.com/profile/06941725483326536858noreply@blogger.com0tag:blogger.com,1999:blog-5160328531119397307.post-28339753019423390402011-11-27T14:01:00.000-05:002011-11-27T14:01:35.790-05:00Read and write cookies in mod_wsgi application<b>Read a cookie from HTTP header:</b><br />
<pre class='prettyprint'>def application(environ, start_response):
import Cookie
cookie = Cookie.SimpleCookie()
cookie.load(environ['HTTP_COOKIE'])
access_token = cookie['access_token'].value
</pre><b>Write a cookie to HTTP header:</b><br />
<pre class='prettyprint'>def application(environ, start_response):
import Cookie
cookie = Cookie.SimpleCookie()
cookie['access_token'] = '1xRD3WXsdfsw23rjfsj9L'
access_token = cookie['access_token'].value
start_response('200 OK', ('Set-Cookie',cookie["access_token"].OutputString())])
</pre>Achilleshttp://www.blogger.com/profile/06941725483326536858noreply@blogger.com2tag:blogger.com,1999:blog-5160328531119397307.post-37560633428650672412011-11-23T01:12:00.000-05:002011-11-23T01:12:17.830-05:00The envrion dictionary in mod_wsgi applicationsThe following codes will show the content of the environ dictionary for quick debugging.<br />
<pre class='prettyprint'>def application(environ, start_response):
output = ['%s: %s' % (key, value) for key, value in sorted(environ.items())]
output = '\n'.join(output)
response_headers = [
('Content-Length', str(len(output))),
('Content-Type', 'text/plain'),
]
start_response('200 OK', response_headers)
return [output]
</pre>Achilleshttp://www.blogger.com/profile/06941725483326536858noreply@blogger.com0tag:blogger.com,1999:blog-5160328531119397307.post-51595942673403419392011-11-23T00:50:00.001-05:002011-11-23T00:56:26.818-05:00URI redirection in mod_wsgi scriptsSee this simple example:<br />
<pre class='prettyprint'>def application(environ, start_response):
if environ['REQUEST_METHOD']!='GET':
start_response('301 Redirect', [('Location', http://example.com'),])
return []
response_headers = [
('Content-Length', str(len(output))),
('Content-Type', 'text/plain'),
]
start_response('200 OK', response_headers)
return ["Hello World"]
</pre>Achilleshttp://www.blogger.com/profile/06941725483326536858noreply@blogger.com0tag:blogger.com,1999:blog-5160328531119397307.post-38086385189250040162011-11-23T00:09:00.000-05:002011-11-23T00:09:54.230-05:00VIM highlight file with certain extensionsFor example, to force VIM highlight *.wsgi files with Python syntax, add folllowing lines to your .vimrc file.<br />
<pre class='prettyprint'>syntax on
filetype on
au BufNewFile,BufRead *.wsgi set filetype=python
</pre>Achilleshttp://www.blogger.com/profile/06941725483326536858noreply@blogger.com0tag:blogger.com,1999:blog-5160328531119397307.post-7577903710144583382011-11-22T14:42:00.000-05:002011-11-22T14:43:31.710-05:00Start-Up SpiritAngelList<div><br /></div><div><a href="http://angel.co/about_angellist">http://angel.co/about_angellist</a></div>Josiehttp://www.blogger.com/profile/01837005103915645854noreply@blogger.com0tag:blogger.com,1999:blog-5160328531119397307.post-45217932802560297302011-11-21T18:23:00.002-05:002011-11-21T18:23:45.104-05:00Add all unversioned files to SVN<pre class='prettyprint'>svn st | grep "^\?" | awk "{print \$2}" | xargs svn add $1</pre>Achilleshttp://www.blogger.com/profile/06941725483326536858noreply@blogger.com0tag:blogger.com,1999:blog-5160328531119397307.post-45116152845445037022011-11-15T19:11:00.000-05:002011-11-15T19:11:25.783-05:00Difference between GET and POST<ul><li>Use GET for safe actions and POST for unsafe actions.</li>
<li>Use POST when dealing with sensitive data.</li>
<li>Use POST when dealing with long requests.</li>
<li>Use GET in AJAX environments.</li>
</ul>Achilleshttp://www.blogger.com/profile/06941725483326536858noreply@blogger.com1tag:blogger.com,1999:blog-5160328531119397307.post-31913296549652912952011-11-15T17:54:00.000-05:002011-11-15T17:54:39.770-05:00SQL subquery examples<b>Join a subquery</b><br />
<pre class='prettyprint'>SELECT DISTINCT Email FROM Accounts JOIN
(
SELECT CouponId, UserId, AccountId
FROM Coupons JOIN Users USING(UserId)
WHERE CouponType=123
) AS tbl
USING(AccountId);
</pre>Achilleshttp://www.blogger.com/profile/06941725483326536858noreply@blogger.com0tag:blogger.com,1999:blog-5160328531119397307.post-54794149566568255812011-11-13T15:59:00.002-05:002011-11-14T09:52:36.089-05:00Quick start on latex beamer [Ubuntu]Beamer is a Tex class to make slide presentations. The advantages of Beamer over PowerPoint or OpenOffice:<br />
<ul><li>If you are creating slides from a larger document, you can simply re-use your LaTeX source material from that document.</li>
<li>If you need mathematical content in your slides, you have the wealth of mathematical constructs in LaTeX to draw upon.</li>
<li>The slides you create are multi-platform.</li>
</ul><b>Installation on Ubuntu</b><br />
$ sudo apt-get install latex-beamer<br />
<br />
<b>Create your own slides from a template</b><br />
A good template can be found <a href="http://www.math.zju.edu.cn/ligangliu/LaTeXForum/Tools/slides/dsecbeamer.tar.gz">here</a>. Note: compile it by PdfLatex, or press F6 in TexMaker.Achilleshttp://www.blogger.com/profile/06941725483326536858noreply@blogger.com0tag:blogger.com,1999:blog-5160328531119397307.post-75074578321397498592011-11-08T17:32:00.000-05:002011-11-08T17:32:02.647-05:00The order of Python dictionaryDictionary is one of the most useful datatype in Python. It provides quick access to the value stored with the key. For example, <br />
<pre class='prettyprint'>>>>author={'Harry Potter':'JK Rowling'}
>>>print author['Harry Potter']
JK Rowling
</pre>The advantage of dictionary is quick access to the value associated with the key. The time complexity of searching and insertion are both O(1), which is much faster than array (i.e., list in Python). However, the disadvantage is that dictionary is implemented by hash table, which doesn't keep the original order in which the keys and values are inserted. For example,<br />
<pre class='prettyprint'>>>>author={'A Tale of Two Cities':'Charles Dickens','Harry Potter':'JK Rowling'}
>>>author.items()
[('Harry Potter', 'JK Rowling'), ('A Tale of Two Cities', 'Charles Dickens')]
</pre>Where the order of the printed out results is not same as the original order in the initialization. To solve this problem, use following codes to display the dictionary contents by the order of keys.<br />
<pre class='prettyprint'>keys = author.keys()
keys.sort()
for key in keys:
print "key=%s, value=%s" % (key,author[key])
</pre>Achilleshttp://www.blogger.com/profile/06941725483326536858noreply@blogger.com0tag:blogger.com,1999:blog-5160328531119397307.post-90170587462570439952011-11-07T00:14:00.005-05:002011-11-21T03:19:27.532-05:00JavaScript: waiting 5 seconds and auto redirects<pre class="prettyprint"><div>Redirect to the home page in <span id="countdown">5</span> secondes...</div>
<script type="text/javascript">
function countDown(){
var count = document.getElementById('countdown').innerHTML-1;
document.getElementById('countdown').innerHTML = count;
if (count<=0) {
location.href = "http://networkplot.tk";
}
else {
t=setTimeout("countDown()",1000);
}
}
countDown();
</script>
</pre>Achilleshttp://www.blogger.com/profile/06941725483326536858noreply@blogger.com1