Thursday, March 7, 2019

SQL Joins with On or Using


In a nutshell, you use ON for most things, but USING is a handy shorthand for the situation where the column names are the same.

Consider this example dataset:

mysql> select * from pets;
+---------+---------+--------+-----------+
| pets_id | animal  | name   | owners_id |
+---------+---------+--------+-----------+
|       1 | fox     | Rusty  |         2 |
|       2 | cat     | Fluffy |         2 |
|       3 | cat     | Smudge |         3 |
|       4 | cat     | Toffee |         3 |
|       5 | dog     | Pig    |         3 |
|       6 | hamster | Henry  |         1 |
|       7 | dog     | Honey  |         1 |
+---------+---------+--------+-----------+
7 rows in set (0.00 sec)

mysql> select * from owners;
+-----------+-------+
| owners_id | name  |
+-----------+-------+
|         1 | Susie |
|         2 | Sally |
|         3 | Sarah |
+-----------+-------+
3 rows in set (0.00 sec)

To find out who has which pets, we would join the two tables together like this:
mysql> select owners.name as owner, pets.name as pet, pets.animal
    -> from owners join pets on (pets.owners_id = owners.owners_id);
+-------+--------+---------+
| owner | pet    | animal  |
+-------+--------+---------+
| Sally | Rusty  | fox     |
| Sally | Fluffy | cat     |
| Sarah | Smudge | cat     |
| Sarah | Toffee | cat     |
| Sarah | Pig    | dog     |
| Susie | Henry  | hamster |
| Susie | Honey  | dog     |
+-------+--------+---------+
7 rows in set (0.00 sec)

The example above uses the ON keyword, but since the columns we use to join are called owners_id in both tables, then we can instead put in USING as a shorthand.

mysql> select owners.name as owner, pets.name as pet, pets.animal
    -> from owners join pets using (owners_id);

Here are the different types of the JOINs in SQL:
  • (INNER) JOIN: Returns records that have matching values in both tables
  • LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from the right table
  • RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from the left table
  • FULL (OUTER) JOIN: Return all records when there is a match in either left or right table

Tuesday, March 5, 2019

practice libsvm

practice the guide.pdf saved in C:\Temp\learn_libsvm\test1_research_the_GUIDE

cd /media/sf_Temp/learn_libsvm/test3_test_libsvm/libsvm-3.23 && make

===========================================================================
this test is for demo purpose(use easy.py in one step):

use the data of project Astroparticle under https://www.csie.ntu.edu.tw/~cjlin/papers/guide/data/ ,
it has training dat as train.1 and test dat as test.1 :
save train.1 into svmguide1
save test.1 into  svmguide1.t

//feature|attr value range [-1, 1], 
./svm-scale -l -1 -u 1 -s range1 svmguide1 > svmguide1.scale
/*
1 1:2.617300e+01 2:5.886700e+01 3:-1.894697e-01 4:1.251225e+02
...
into
1 1:-0.823781 2:-0.783405 3:-0.233795 4:0.361305 
...
0 1:-0.885045 2:-0.949015 3:0.182549 4:-0.519316 

has 2 classes,  class label Infected or Clean
*/

./svm-scale -r range1 svmguide1.t > svmguide1.t.scale

./svm-train svmguide1.scale
/*
*
optimization finished, #iter = 496
nu = 0.202599
obj = -507.307046, rho = 2.627039
nSV = 630, nBSV = 621
Total nSV = 630
*/

./svm-predict svmguide1.t.scale svmguide1.scale.model svmguide1.t.predict
Accuracy = 96.15% (3846/4000) (classification)


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

to achieve better Accuracy, ./svm-train requires cross-validation and specify following args:
-c -g 

the RBF kernel function argument C and galma

//the grid tools is a time consumer
./tools/grid.py svmguide1.scale
2.0 2.0 96.9893

./svm-train -c 2 -g 2 svmguide1.scale
Accuracy = 96.875%
===========================================================================
use easy.py in one step:
apt-get install -y gnuplot
cd tools/ && ./easy.py ../svmguide1 ../svmguide1.t

Scaling training data...
Cross validation...
Best c=2.0, g=2.0 CV rate=96.9893
Training...
Output model: svmguide1.model
Scaling testing data...
Testing...
Accuracy = 96.875% (3875/4000) (classification)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Wednesday, January 16, 2019

在温哥华办理美国签证攻略

在温哥华办理美国签证攻略

非移民签证常见类型

  • CNMI-only(仅限于美国北玛利安纳群岛) transitional worker(CW-1).
  • business visitor商务访问(B-1)
  • Exchange visitor交换访问(J)
  • Intra-company transferee公司内部穿梭(L)
  • Tourism, vacation, pleasure visitor旅游(B-2)

照片场景要求

  • Sized such that the head is between 1 inch and 1 3/8 inches (22 mm and 35 mm) or 50% and 69% of the image's total height from the bottom of the chin to the top of the head.
  • Taken in front of a plain white or off-white background

照片拍摄和处理要求


Dimensions The image dimensions must be in a square aspect ratio (the height must be equal to the width). Minimum acceptable dimensions are 600 x 600 pixels. Maximum acceptable dimensions are 1200 x 1200 pixels.
Color The image must be in color (24 bits per pixel) in sRGB color space which is the common output for most digital cameras.
File Format The image must be in JPEG file format
File Size The image must be less than or equal to 240 kB (kilobytes).
Compression The image may need to be compressed in order for it to be under the maximum file size. The compression ratio should be less than or equal to 20:1.

详细流程

  1. 浏览 领事电子申请中心 以申请非移民签证。
  2. ...

Friday, November 30, 2018

Understanding Events in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class TransactionEventArgs : EventArgs
{
    private int _transactionAmount;
    // "Credited": deposit
    // "Debited" : withdraw
    private string _transactionType;
    public TransactionEventArgs(int amt, string type)
    {
        this._transactionAmount = amt;
        this._transactionType = type;
    }
    public int TranactionAmount
    {
        get
        {
            return _transactionAmount;
        }
    }
    public string TranactionType
    {
        get
        {
            return _transactionType;
        }
    }
}

/*like:
public delegate void EventHandler(object sender, EventArgs e);

*/
public delegate void TransactionHandler(object sender, TransactionEventArgs e);
class Account
{
    //like:
    //public event EventHandler Click;
    //
    //the TransactionMade event and its TransactionHandler delegate(s)
    public event TransactionHandler TransactionMade; // Event Definition
    public int BalanceAmount;
    public Account(int amount)
    {
        this.BalanceAmount = amount;
    }
    //publisher invokes a event
    public void Debit(int debitAmount)
    {
        if (debitAmount < BalanceAmount)
        {
            BalanceAmount = BalanceAmount - debitAmount;
            TransactionEventArgs e = new TransactionEventArgs(debitAmount, "Debited");
            OnTransactionMade(e); // Debit transaction made
        }
    }

    public void Credit(int creditAmount)
    {
        BalanceAmount = BalanceAmount + creditAmount;
        TransactionEventArgs e = new TransactionEventArgs(creditAmount, "Credited");
        OnTransactionMade(e); // Credit transaction made
    }
    protected virtual void OnTransactionMade(TransactionEventArgs e)
    {
        if (TransactionMade != null)
        {
            TransactionMade(this, e); // Raise the event
        }
    }
}

class TestMyEvent
{
    private static void SendNotification(object sender, TransactionEventArgs e)
    {
        Console.WriteLine(
            "Your Account is {0} for ${1} ",
            e.TranactionType,
            e.TranactionAmount);
    }
    private static void Main()
    {
        Account MyAccount = new Account(10000);
        //like: this.button1.Click += new System.EventHandler(this.button1_Click);
        //
        //note: the += operator of event will subscribe the event append a delegate
        MyAccount.TransactionMade += new TransactionHandler(SendNotification);
        MyAccount.Credit(500);
        Console.WriteLine("Your Current Balance is : " + MyAccount.BalanceAmount);
        Console.ReadLine();
    }
}

Tuesday, July 3, 2018

_NT_SYMBOL_PATH and extra

_NT_SYMBOL_PATH cache*C:\Temp\Newfolder\_NT_SYMBOL_PATH;srv*https://msdl.microsoft.com/download/symbols

.sympath to view current setting

.sympath+ to add path:
.sympath+ C:\Chromium\debug_v8_02\v8\out.gn\ia32.debug\

// .srcpath C:\Chromium\debug_v8_02\v8\src

// bp d8_exe!main

db poi(poi(argv)+4)
00685050  43 3a 5c 43 68 72 6f 6d-69 75 6d 5c 64 65 62 75  C:\Chromium\debu
00685060  67 5f 76 38 5f 30 32 5c-69 6e 73 74 61 6e 63 65  g_v8_02\instance
00685070  30 31 5c 31 2e 6a 73 00-fd fd fd fd ab ab ab ab  01\1.js.........

Tuesday, June 19, 2018

build_and_debug_v8_on_windows

This artical refers to https://medium.com/dailyjs/how-to-build-v8-on-windows-and-not-go-mad-6347c69aacd4

Setting up environment

/*
VS2013 Requirements:
  • at least 8.5 GB disk space;If your windows has been updated to October 2013, you can delete some not used files in winsxs to free up disk space: apply "Clean up system files" button from windows disk cleanup utility(to run it with system adiminister, view its path through windows task mgr, then right click it and select run as admin).
  • for the express installer, select the installer for Windows Desktop instead of Windows(the later can be used to create windows store app).
  • To install vs2013 express, I make my win7 genenuine first by input the product key W4TGB-8HFJV-QB794-P6MDG-XWGF6 found in my disk packer and machine sticker
  • VS 2013 will use dnet 4.5.1 first, so better remove your old install(to check version of installed dnet, check HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP)
*/
VS2017 Requirements:
  • at least 30 GB disk space

Install git(if has no installation in your system)

From https://git-scm.com/downloads download and install git.

Install VS /*2015|*/2017(if has no installation in your system)

Depending on the system, may needing to install dnet 4.6 first.
Install VS(do not change default install path for VS), making sure the installer has installed Debug Interface Access (DIA) SDK(msdia*.dll), vcvarsall.bat, (and the Universal CRT?).

Install and config depot_tools

V8 uses part of a toolchain named depot_tools for Chromium project, Download depot_tools.zip and extract it(to C:\Chromium\depot_tools), then adds it to PATH. At the same time, add two new env var into your system:
DEPOT_TOOLS_WIN_TOOLCHAIN=0 //If you are a non-googler you need to set DEPOT_TOOLS_WIN_TOOLCHAIN=0
GYP_MSVS_VERSION=2017 //windows is not using GN but GYP for Ninja?
From a cmd.exe shell, run the command `gclient`, it will install all the bits needed to work with the code, including msysgit and python; After running gclient, open a new command prompt and type "where python" and confirm that python.bat comes ahead of python.exe (adjust PATH to let python.bat being searched first).

Install windows sdk

Windows sdk must be installed at default dir; and Debugging Tools For Windows must be selected(building v8 requires windows sdk, so if you only installed `Debugging Tools For Windows`, remove it)
After installing, windbg may be found at following locations:
C:\Program Files (ia32)\Windows Kits // C:\Program Files (x86)\Windows Kits
C:\Program Files (ia32)\Microsoft SDKs\Windows Kits

Building and use V8

Get source code

Go into the directory where you want to download the V8 source into and execute the following in your terminal/shell:
  • cd /d C:\Chromium\debug_v8_02
  • fetch v8
    Running: 'C:\depot_tools\win_tools-2_7_6_bin\python\bin\python.exe' 'C:\depot_tools\gclient.py' root
    Running: 'C:\depot_tools\win_tools-2_7_6_bin\python\bin\python.exe' 'C:\depot_tools\gclient.py' config --spec 'solutions = [
      {
        "url": "https://chromium.googlesource.com/v8/v8.git",
        "managed": False,
        "name": "v8",
        "deps_file": "DEPS",
        "custom_deps": {},
      },
    ]
    '
    C:\Chromium\depot_tools\win_tools-2_7_6_bin\python\bin\python.exe C:\Chromium\depot_tools\gclient.py sync --with_branch_heads
    ...

build debug_v8_02

prepare build file for ninja: cd /d C:\Chromium\debug_v8_02\v8 && python tools/dev/v8gen.py ia32.debug
modify config file C:\Chromium\debug_v8_02\v8\out.gn\ia32.debug\args.gn :
#ia32.debug x64.release ...
is_debug = true
target_cpu = "x86"
v8_enable_backtrace = true
v8_enable_slow_dchecks = true
v8_optimized_debug = false
" ninja -C out.gn/ia32.debug " and produce the product d8.
check if v8 works: C:\Chromium\debug_v8_02\v8\out.gn\ia32.debug\d8.exe C:\Chromium\debug_v8_02\instance01\1.js
debug d8:
  • windbg with C:\Chromium\debug_v8_02\v8\out.gn\ia32.debug\d8.exe C:\Chromium\debug_v8_02\instance01\1.js
  • set symbols: .sympath+ C:\Chromium\debug_v8_02\v8\out.gn\ia32.debug\
  • set srcpath: .srcpath C:\Chromium\debug_v8_02\v8\src
  • bp d8_exe!main
  • let windbg run and break
  • db poi(poi(argv))
    0068501c  43 3a 5c 43 68 72 6f 6d-69 75 6d 5c 64 65 62 75  C:\Chromium\debu
    0068502c  67 5f 76 38 5f 30 32 5c-76 38 5c 6f 75 74 2e 67  g_v8_02\v8\out.g
    0068503c  6e 5c 69 61 33 32 2e 64-65 62 75 67 5c 64 38 2e  n\ia32.debug\d8.
    
  • db poi(poi(argv)+4)
    00685050  43 3a 5c 43 68 72 6f 6d-69 75 6d 5c 64 65 62 75  C:\Chromium\debu
    00685060  67 5f 76 38 5f 30 32 5c-69 6e 73 74 61 6e 63 65  g_v8_02\instance
    00685070  30 31 5c 31 2e 6a 73 00-fd fd fd fd ab ab ab ab  01\1.js.........
    

















Friday, June 15, 2018

googletest

Locate GTEST_ROOT

after you (git) clone google test from master, your dir contains both google test and google mock. googletest is google's suggestion.

GTEST_ROOT may looks like ..\googletest-master\googletest

Locate gtest.sln or makefile to work on

googletest provides build files for some popular build systems: msvc/ for Visual Studio, xcode/ for Mac Xcode, make/ for GNU make, codegear/ for Borland C++ Builder, and the autotools script (deprecated) and CMakeLists.txt for CMake (recommended). In my case, it's under msvc\2010 .

test a project or build target

Created ConsoleApplication1 under the sln, its dir is msvc\ConsoleApplication1.

copy code for the target's int main() func form the project gtest_main.

config the tgt's runtime to "Multi-threaded Debug (/MTd)" //same as gtest

add dependency to gtest(note: in vs2017, add it from project properties menu), and add C:\Temp\Tasks\googletest\test2\googletest-master\googletest\msvc\2010\gtest\Win32-Debug as additional lib dir.

add testing code:

TEST(dummy_case, dummy_test)
{
 //use EXPECT_* when you want the test to continue to reveal more errors after the assertion failure,
 //and use ASSERT_* when continuing after failure doesn't make sense. 
 EXPECT_EQ(3, 3);
 ASSERT_EQ(3, 3);

}

For a c++ class tester

we can create c++ class tester(fixture) public on ::testing::Test; the fixture's tests can use TEST_F.

For each test defined with TEST_F(cls_name,test_name), Google Test will:

Create a fresh test fixture named cls_name at runtime
Immediately initialize it via SetUp()
Run the test
Clean up by calling TearDown()
Delete the test fixture. Note that different tests in the same test case have different test fixture objects, and Google Test always deletes a test fixture before it creates the next one.
Google Test does not reuse the same test fixture for multiple tests. Any changes one test makes to the fixture do not affect other tests.

further readings

https://github.com/google/googletest/blob/master/googletest/docs/primer.md