Sunday, June 8, 2014

改变affinity - CPU利用数

由于Android emulator存在bug - 只能单cpu运行,所以我们必须在emulator启动后,改变其affinity,使其可以多CPU运行

PowShell "Get-Process"

PowerShell "Get-Process calc | Select-Object ProcessorAffinity"
PowerShell "$Process = Get-Process calc; $Process.ProcessorAffinity=255"


PowerShell "Get-Process | where-Object {$_.mainWindowTitle} | format-table id,name,mainwindowtitle -AutoSize"

PowerShell "Get-Process | where-Object {$_.mainWindowTitle -like 'calculator'} | format-table id,name,mainwindowtitle -AutoSize"

Id Name MainWindowTitle
-- ---- ---------------
40312 calc Calculator


PowerShell "$Process = Get-Process | where-Object {$_.mainWindowTitle -like 'calculator'}; $Process.ProcessorAffinity=255"



PowerShell "$Process = Get-Process | where-Object {$_.mainWindowTitle -like '5554:test'}; $Process.ProcessorAffinity=255"


Wednesday, May 28, 2014

Android emulator加速和代理

软件加速:
avd设置中,选use host gpu

硬件加速:
1. 在bios设置, enable visualization technology.
2. cmd输入sc query intelhaxm见到尽管硬件设置了,但软件未安装
3. SDK manager下载intel accelarate








4. Go to android SDK folder,  sdk\extras\intel\Hardware_Accelerated_Execution_Manager\intelhaxm.exe, 安装时候,按需要提高内存上限(默认2G)
5. 在Android SDK manager安装 google APIs (x86 system image) and google APIs ( ARM system image) and Intel x86 Emulator Accelerated(HAXM installer)
这时候重复步骤2可见intelhaxm服务开始了
6. Avd设置中,target选google APIs(x86 system image), skin WXga800-7in
或者cmd: emulator -avd nexus5 -gpu on

代理proxy:
在安装apk到虚拟器emulator一文提到用cmd设置proxy,但笔者用后觉得emulator容易暂停或死机,这里用emulator中设置方法达到:
1. 打开emualotor
2. Settings->more->mobile networks->check data enabled option and click 
access point name->us t-mobile
设置Proxy, port, 置空username password
右上角Save

如果要创建文件:
1. 在创建avd时候设置sd card

Reference:
Android using hardware acceleration 
http://developer.android.com/tools/devices/emulator.html#acceleration

Friday, May 9, 2014

UiAutomator简介(4) - 输入中文

这一节主要讲怎么settext中输入中文,方法主要是用一个外部包

步骤主要如下:
1. 安装Utf7ImeHelper.apk
2. 在你的代码中,加入Utf7ImeHelper.java,并且调用setText(Utf7ImeHelper.e(“我们women”))

当你运行UiAutomator时候,输入文字,不会出现键盘



详细可以参考:
http://testerhome.com/topics/678
http://testerhome.com/topics/408

Wednesday, April 30, 2014

Java之OCR

Java的OCR有一个简单的lib是ocr4j.jar,他可以识别背景不复杂的文字

              OCR ocr = null;
              try {
                     ocr = new OCR();
              } catch (IOException e2) {
                     // TODO Auto-generated catch block
                     e2.printStackTrace();
              }
              File img = new File("C:\\Users\\KK\\Documents\\Coding\\Java\\IMG_2915.PNG");
              try {
                     String text = ocr.recognise(ImageIO.read(img));
                     System.out.print(text);
              } catch (IOException e1) {
                     // TODO Auto-generated catch block
                     e1.printStackTrace();

              }

图片如下:


References:
[1] http://ocr4j.sourceforge.net/









Saturday, April 26, 2014

KB: Java之反射

import java.lang.reflect.*;

1.得到类名:Class c = Class.forName(this.getClass().getName().toString()); this为instance,如果这句在基类,类,getClass会得到给子类。class name是包括包名的,比如com.vtasters.entity.Car Car是类名
2. 得到某类(this)的某属性:c.getField("location").get(this),如果该属性是static,就如下c.getField("location").get(null)
3. 设置某类(this)的某属性:c.getField("location").set(this, "Jersey city");
4. 获得某类某属性的类型:
Field fields[] = c.getDeclaredFields();
for(Field f : fields)
{
            f.getType()
5. 调用方法:
    Class cls=null;
    cls=Class.forName("com.vtasters.Dish");
    Object obj=cls.newInstance();
    Method method=cls.getMethod("Test",null);
    method.invoke(obj);
    这里调用一个没有参数叫Test的方法

     Class cls=null;
  cls = Class.forName("com.framework.Action");

  Object obj = cls.newInstance();

  Class[] cArg = new Class[2];
  cArg[0] = int.class;
  cArg[1] = String.class;
  Method method2 = cls.getMethod("bbb", cArg);
  Object returnType = method2.getReturnType();
  Object value = method2.invoke(obj,5,"6");
  System.out.println(returnType.toString()+":"+value.toString());

    这里调用一个带参数和返回值叫bbb的方法,定义如下
    int bbb(int a, String b){}

    注意,有时一些内部类带的属性或方法,不能在被调用的方法(e.g. bbb)中使用,要作为参数输入,否则会出现Reflection异常

如果参数个数是动态,就这样写:
       String[] mainArgs = new String[5];
      method.invoke(null, (Object)mainArgs);
getDeclaredMethod可以得到protected的方法,但getMethod只能得到公共方法

6. 返回值
Object value = method.invoke(this, x);
                d = (Double) value;

7. 调用构造函数
            cls = Class.forName("com.itg.generic.Events");
            Class[] constructorArg = new Class[1];
            constructorArg[0] = Operator.class;
            Object obj = cls.getDeclaredConstructor(constructorArg).newInstance(this);

8. 判断一个数据是否实例
    Num param instanceof Double

a=="mystr"不对,要用a.equals("mystr")

Wednesday, April 23, 2014

KB: c#之反射

有时为了使相似度较高的代码智能化,我们会使用到反射。
这是Reflexion的命名空间using System.Reflection;

前面的blog说到C#之Entity framework,提到这段代码:

using (ModelEntities context = new ModelEntities())
{
     context.stocks_EF.Add(new stocks_EF() { Symbol = "CAT", Price = 98.42, PricePct = 2.5, Volume = 29389, AvgVolume = 39392, MktCap = "56.5B", Yield = 2.2, PE = 10.2, TradeDate = Convert.ToDateTime("2013-06-17 00:00:00.000") });
     context.SaveChanges();
}


    public partial class ModelEntities : DbContext
    {
        public DbSet<stocks_EF> stocks_EF { get; set; }

    }

下面我说说怎么用反射写这段代码。
1.已知道类型的Type:Type contextType = typeof(ModelEntities);如果是某instance的type是    
  obj.GetType()
2. 创建某类型的Instance:object context = Activator.CreateInstance(contextType);
3. 获得某Instance的某属性的值:
  object dbSetInstance = contextType.GetProperty(propName).GetValue(context);
4. 获得某Instance的某属性的类型:
    Type dbSetType = contextType.GetProperty(propName).PropertyType;
5.获得某类型的某方法:MethodInfo objAddMethod = dbSetType.GetMethod("Add");
6.Call某方法: objAddMethod.Invoke(dbSetInstance, new object[]{obj});
(eg2)7.获得泛型的参数类型
                if (propType.IsGenericType)
                {
                    Type[] paraList = propType.GetGenericArguments();
                    propertyName = paraList[0].Name;

                }
8.设置某属性的值:t.GetProperty(name).SetValue(obj, "abd")


Type contextType = typeof(ModelEntities);
object context = Activator.CreateInstance(contextType);
Type objType = obj.GetType();

var propNames = from p in contextType.GetProperties() select p.Name;

foreach (var propName in propNames)
{
    if (objType.ToString() == contextType.Namespace+"."+propName)
    {
         object dbSetInstance = contextType.GetProperty(propName).GetValue(context);
         Type dbSetType = contextType.GetProperty(propName).PropertyType;

         MethodInfo objAddMethod = dbSetType.GetMethod("Add");
         objAddMethod.Invoke(dbSetInstance, new object[] { obj });

         MethodInfo objSaveMethod = contextType.GetMethod("SaveChanges");
         objSaveMethod.Invoke(context, null);
     }
}

再训练一个:
    public partial class stocks_EF
    {
        public int ID { get; set; }
        public string Symbol { get; set; }
        public Nullable<double> Price { get; set; }
        public Nullable<double> PricePct { get; set; }
        public Nullable<int> Volume { get; set; }
        public Nullable<int> AvgVolume { get; set; }
        public string MktCap { get; set; }
        public Nullable<double> Yield { get; set; }
        public Nullable<double> PE { get; set; }
        public Nullable<System.DateTime> TradeDate { get; set; }
        public Nullable<System.DateTime> InsertTime { get; set; }

    }


code如下:
public static void Fill(string[] words, object obj)
{
    Type t = obj.GetType();
    var propNames = from p in t.GetProperties() select p.Name;
    int i=0;
    foreach (var name in propNames)
    {
        Type propType = t.GetProperty(name).PropertyType;
        string propertyName = propType.Name;

        if (propType.IsGenericType)
        {
            Type[] paraList = propType.GetGenericArguments();
            propertyName = paraList[0].Name;
        }
        if (propertyName == "DateTime")
            t.GetProperty(name).SetValue(obj, Convert.ToDateTime(words[i]));
        else if (propertyName == "Int32")
            t.GetProperty(name).SetValue(obj, Convert.ToInt32(words[i]));
        else if (propertyName == "Double")
            t.GetProperty(name).SetValue(obj, Convert.ToDouble(words[i]));
        else t.GetProperty(name).SetValue(obj, words[i]);
        i++;
    }


}

main函数:
string[] words = { "1", "CAT", "98.43", "2.5", "29389", "39392", "56.5B", "2.2", "10.2", "2013-06-17 00:00:00.000", "2014-04-23 00:00:00.000" };
 stocks_EF s = new stocks_EF();
 Fill(words, s);


KB: Batch的简介

Batch其实就是CMD编程

1. 传入参数
假设我们有一个test.bat,代码如下:

test.exe "abc.txt"

我们希望在cmd中传入自定义文件名称,batch file的代码可以改成

test.exe %1

然后cmd中可以输入test.bat abc.txt或者test.bat me.data,即可作为参数传递给test.exe

2.Call
如果调用非exe,要加上call,比如call abc.bat,否则如果有如下命令:
abc.bat
dd.exe
没有call的话,dd.exe不会执行,所以 应该这样:
call abc.bat
dd.exe
[1] http://stackoverflow.com/questions/4825746/cmd-exe-when-to-use-call-to-run-external-programs

3.常用设置
@rem表示注释行还可以用::
@echo off表示不输出cmd自带结果,除非你显示调用echo函数
@setlocal enableextensions enabledelayedexpansion设置环境变量可见性
endlocal结束设置环境变量可见性

4. 局部变量
Set Filename="abc.txt"
set aaa = %Filename%
for 循环的变量可以用%%x详见7

5. 循环、if and goto以及动态赋值
for:
for /l %%x in (1,1,100) do (
    echo %%x
    copy %%x.txt z\whatever\tect
)
左括号要在第一行,不能在新行
in cmd use %x instead of %%x
不能用::作注释只能用@rem
set newest=%a%
echo !newest!由于for是循环,赋值大部分都是动态,所以用动态取值(execution time rather than at parse time)!newest!而不是%newest% 还例如findstr /m /c:"my" !newest!连%errorlevel%也要改成!errorlevel!
要打印%today%要加引号"%today%"而!newest!无需引号

if:
if %aa%==5 echo 6
if %aa%==5 (
     echo 6
)
没有else,要用多重if来实现

goto:
@echo off
setlocal enabledelayedexpansion
IF 5==5 IF 4==3 (
    GOTO END
)
echo 2
:END
ECHO DONE
这里goto相当于break或者return

6. 等效and/or操作符的代码
AND: 
if %age% geq 2 if %age% leq 12 set class=child
OR: 
set res=false
if %hour% leq 6 set res=true
if %hour% geq 22 set res=true
if "%res%"=="true" (
    set state=asleep
)
NOT: 
if not a==5 echo 6




9. 文件和路径
执行非当前路径的命令---执行后返回到之前目录
pushd D:\a
for /f "tokens=*" %%a in ('dir /b /od') do set newest=%%a
copy "%newest%" D:\b
popd
复制最新文件到D:\b然后回到原来目录

移动所有文件
move "*.txt" c:\

判断文件夹是否未空或者不含某类文件:
@echo off
for /F %%i in ('dir /b "c:\test directory\*.*"') do (
   echo Folder is NON empty
   goto :EOF
)
echo Folder is empty or does not exist


10. 字符串操作
可以参考
http://scripts.dragon-it.co.uk/scripts.nsf/docs/batch-search-replace-substitute!OpenDocument&ExpandSection=3&BaseTarget=East&AutoFramed
http://www.dostips.com/DtTipsStringManipulation.php#Snippets.Replace

10.1 在文档中找给定的字符串
findstr /c:"Job finished" abc.txt
     if %errorlevel%==0 (
     echo Given string is found
)

10.2 是否含有给定字符串
@setlocal enableextensions enabledelayedexpansion
@echo off
set str1=%1
if not %str1:bcd=%==%str1% echo It contains bcd
endlocal
str1是否含有bcd,用的方法是replace给定字符串再比较原来的,%str1:bcd=%是在str1中用空字符替换bcd如果给定字符串是变量,参见10.3

10.3 替代变量字符串
@echo off
setlocal enabledelayedexpansion
set string=This is my string to work with.
set find=my string
set replace=your replacement
call set string=%%string:!find!=!replace!%%
echo %string%
这里用了延迟赋值来替换
所以10.2如果bcd是变量可以变成if not %string%==%str1%

11. 睡眠sleep 30秒,ctrl+c中断
timeout /t 30 /nobreak

12. taskkill
taskkill /im calc.exe 终止所有计算机(一个或多个)
taskkill /f /fi "Windowtitle eq 5554:test" /im *(终止某一个指定程序)

13. Forfiles
http://ss64.com/nt/forfiles.html


--get files whose last modified date is older than 90 days
forfiles -p "C:\Users\KK\Documents\Test" /d -90


--loop in a dir and echo file name ("cmd /c echo @file" is default cmd)
forfiles -p "C:\Users\KK\Documents\Test" /d -90 /c "cmd /c echo @file"


--loop in a dir and see if any file exists in it(if not, error occurs)
forfiles /s -p "D:\ETLSourceFiles\Carmax\DEAR"

--loop in a dir and delete each directory whose last modified date is older than 90 days
forfiles -p "C:\Users\KK\Documents\Test" /d -90 /c "cmd /c IF @isdir == TRUE rd /s /q @path & echo @path >> abc.txt"