Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Thursday, March 22, 2012

The difference between Scripting Languages: PHP, Perl, Python, Ruby

The difference between Scripting Languages: PHP, Perl, Python, Ruby.
http://hyperpolyglot.org/scripting?utm_source

Wednesday, December 7, 2011

Linq calculate average of a list C#

Only one line of code will do it:

List<float> f = new List<float>();
for (int i = 0; i < 10; i++)
{
    f.Add((float)i);
}
float ave = f.Aggregate((acc, cur) => acc + cur) / f.Count;
Console.WriteLine(ave);

Tuesday, September 20, 2011

How special var is in C#, var can't be the type of property of class C#


var is static typed - the compiler and runtime know the type - they just save you some typing... the following are 100% identical:
var s = "abc";
Console.WriteLine(s.Length);
and
string s = "abc";
Console.WriteLine(s.Length);

Monday, August 8, 2011

OpenCV material

http://en.wikipedia.org/wiki/Blob_extraction
http://www.neuroforge.co.uk/index.php/binary-images-with-opencv
http://opencv.willowgarage.com/documentation/python/histograms.html#createhist

PYTHON iterate all files in a directory


To iterate through all the files within the specified directory (folder), with ability to use wildcards (*, ?, and [ ]-style ranges), use the following code snippet:
PYTHON:
  1. import os
  2. import glob
  3.  
  4. path = 'sequences/'
  5. for infile in glob.glob( os.path.join(path, '*.fasta') ):
  6.     print "current file is: " + infile
If you do not need wildcards, then there is a simpler way to list all items in a directory:
PYTHON:
  1. import os
  2.  
  3. path = 'sequences/'
  4. listing = os.listdir(path)
  5. for infile in listing:
  6.     print "current file is: " + infile

PYTHON RuntimeError: maximum recursion depth exceeded in cmp


if your python code produces the following error: “RuntimeError: maximum recursion depth exceeded in cmp,” try adding this line to the beginning of your code:
sys.setrecursionlimit(1500)
note that python’s default limit is 1000.

Saturday, August 6, 2011

opencv

g++ hello-world.cpp -o hello-world \
    -I /usr/local/include/opencv -L /usr/local/lib  \
    -lm -lcv -lhighgui -lcvaux
c++ find_obj.cpp -o o -I /usr/local/include -L /usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann

Monday, June 13, 2011

VIM increase number, increase alpha

Press Ctrl+A increase the number under cursor.

:set nrformats=alpha

can help you increase character.

Sunday, June 5, 2011

Tuesday, May 31, 2011

eclipse comments short cut


ShortcutCommandDescription
Ctrl+/Toggle CommentAdd/remove line comments (//…) from the current line. The position of the cursor can beanywhere on the line. Works with multiple selected lines as well.
Ctrl+Shift+/Add Block CommentWrap the selected lines in a block comment (/*… */).
Ctrl+Shift+\Remove Block CommentRemove a block comment (/*… */) surrounding the selected lines.
Alt+Shift+JAdd Javadoc CommentAdd a Javadoc comment to the active field/method/class. See the notes below for more details on where to position the cursor.

Monday, May 23, 2011

perl send email cgi

Lots of tutorials about sending email with perl depend on some uncommon libs.
There is a common way to do so:

use Net::SMTP::SSL;


sub send_mail {
    my $to      = $_[0];
    my $subject = $_[1];
    my $body    = $_[2];

perl session cgi

http://search.cpan.org/dist/CGI-Session/lib/CGI/Session/Tutorial.pm


$session = CGI::Session->load() or die CGI::Session->errstr;
if ( $session->is_expired ) {
    die "Your session expired. Please refresh your browser to re-start your session";
}
if ( $session->is_empty ) {
    $session = $session->new();
}


print $session->header();

#save into session
$session->param('username',$username);

#read from session
$variable = $session->param('username');

Saturday, May 14, 2011

Tuesday, May 10, 2011

Java sort list -- Collections.sort sample




public static void Sort(List<Movies> items) {
Collections.sort(items, SENIORITY_ORDER);
}
static final Comparator<Movies> SENIORITY_ORDER =
        new Comparator<Movies>() {
public int compare(Movies e1, Movies e2) {
if (e1.getReleaseDate().after(e2.getReleaseDate()))
return 1;
else
return -1;
}
};

Tuesday, March 22, 2011

Conditional Operations


Let's look at the if/then/else construct in a Bash shell script and see how to control the flow of a script with conditional logic. The general form of if/then/else is shown here, with the actual syntax shown in boldface and the parts you must supply in normal type:
if [ condition is true ]
then

execute these commands

else

execute those commands

Thursday, January 13, 2011

Use the "using" C# key word to build sql connection

While reviewing some C# code written by a newbie programmer and buddy of mine, I noticed a lack of calling Dispose() on SqlConnection and SqlCommand objects. And in all cases, the database code was not placed in try / finally blocks. This is typical newbie style of development that everyone, including myself, attempted in the very beginning.

SqlConnection cn = new SqlConnection(connectionString);
SqlCommand cm = new SqlCommand(commandString, cn);

Thursday, December 30, 2010

Set "Auto" to GridView Colum Width when re-binding data to the GridView

The GridView Colum Width is a headache.

detailView.Columns.OfType<System.Windows.Controls.GridViewColumn>().ToList().ForEach(c => c.Width = c.ActualWidth);
detailView.Columns.OfType<System.Windows.Controls.GridViewColumn>().ToList().ForEach(c => c.Width = Double.NaN);


http://msdn.microsoft.com/en-us/library/system.windows.controls.gridviewcolumn.width.aspx
Double.NaN means auto-size

Wednesday, December 29, 2010

WPF Make a Button invoked by Enter Key, set it as IsDefault=true

In the window, I try to use focus manager to set the button focused to invoke Enter Key, but the focus may lost.

By setting the IsDefault property, problem solved.


public void EnableFinish()
{
    btnNext.Visibility = Visibility.Hidden;
    btnFinish.Visibility = Visibility.Visible;
    btnFinish.IsDefault = true;
}

Tuesday, December 28, 2010

Define width of GridViewColumn by parent window

<GridView ColumnHeaderContainerStyle="{StaticResource CustomHeaderStyle}">
      <GridViewColumn Header="Available Roles"
         Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource FindAncestor, cestorType={x:Type local:ListViewEx}}, Converter={StaticResource gridViewWidthConverter}, ConverterParameter=0.9}">
          

Wednesday, December 15, 2010

Timer vs. DispatcherTimer in WPF

Have you ever wondered what is the difference between Timer and DispatcherTimer in WPF / Silverlight? In this post I will try to explain you the little difference. Timer generates recurring events in an application. DispatcherTimer is a timer that is integrated into theDispatcher queue which is processed at a specified interval of time and at a specified priority. Timers are not guaranteed to execute exactly when the time interval occurs, but are guaranteed not to execute before the time interval occurs. This is becauseDispatcherTimer operations are placed on the Dispatcher queue like other operations. When the DispatcherTimer operation executes, it is dependent of the other jobs in the queue and their priorities.