`
jxqc_job
  • 浏览: 529 次
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
jython jython
导包:jython-2.7-b1.jar
-----------------------------------------
-----------------------------------------
java类文件:

package com.newbee;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.python.core.PyDictionary;
import org.python.core.PyException;
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyList;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.PyStringMap;

public class SimpleEmbedded {
	public static void main(String[] args) throws PyException, IOException { 
        
        // Java 
        EmployeeType eType = 
        		(EmployeeType) JythonFactory.getInstance().getJavaObjectFromJythonFile("com.newbee.EmployeeType", "Employee.py"); 
        System.out.println("Employee Name: " + eType.getEmployeeFirst() + " " + eType.getEmployeeLast()); 
        System.out.println("Employee ID: " + eType.getEmployeeId()); 
         
        // Jython 
        PyObject pyObject = JythonFactory.getInstance().getPyObjectFromJythonFile("Employee", "Employee.py"); 
        System.out.println("+++="+pyObject.invoke("getEmployeeId")); 
        pyObject.invoke("setEmployeeId",new PyString("1999")); 
        System.out.println("+++="+pyObject.invoke("getEmployeeId")); 
         
        // Jython Function 
        PyFunction pyFunction = JythonFactory.getInstance().getPyFunctionFromJythonFile("getNunmberValue", "Employee.py"); 
        System.out.println("***="+pyFunction.__call__(new PyInteger(10))); 
        
        PyFunction py = JythonFactory.getInstance().getPyFunctionFromJythonFile("adder", "my_utils.py"); 
        System.out.println("***="+py);
        System.out.println("***="+py.__call__(new PyInteger(10),new PyInteger(10))); 
        System.out.println("**************************************************************");
        
//        PyFunction py_2 = JythonFactory.getInstance().getPyFunctionFromJythonFile("getPersonNew", "my_class.py"); 
//        System.out.println("***="+py_2.__call__(new PyString("孙悟空"))); 
        /***对应demo01.py***/
        PyFunction py_2 = JythonFactory.getInstance().getPyFunctionFromJythonFile("handCollection", "demo01.py"); 
        List list = new ArrayList();
        list.add(1);
        list.add(2);
        List newList = (List)py_2.__call__(new PyList(list));
        for(int i = 0; i < newList.size(); i++){
        	Object obj = newList.get(i);
        	if(obj instanceof String){
        		System.out.println("字符串的值 = "+newList.get(i).toString());
        	}else if(obj instanceof Integer){
        		int temp = (Integer)obj;
        		System.out.println("字符串的值 = "+temp);
        	}
        	
        }
//        System.out.println("***="+py_2.__call__(new PyList(list))); 
        
        System.out.println("----------------西游记----------------");
        /***对应demo02.py***/
        PyFunction py_3 = JythonFactory.getInstance().getPyFunctionFromJythonFile("getUserList", "demo02.py"); 
        List<UserVo> list_3 = new ArrayList<UserVo>();
        UserVo u = new UserVo();
        u.setId(1);
        u.setUsername("唐僧");
        u.setUserpass("tangseng");
        UserVo u2 = new UserVo();
        u2.setId(2);
        u2.setUsername("八戒");
        u2.setUserpass("bajie");
        
        list_3.add(u);
        list_3.add(u2);
        
        List<UserVo> newList_3 = (List<UserVo>)py_3.__call__(new PyList(list_3));
        for(int i = 0; i < newList_3.size(); i++){
        	Object obj = newList_3.get(i);
        	if(obj instanceof String){
        		System.out.println("字符串的值 = "+newList_3.get(i).toString());
        	}else if(obj instanceof Integer){
        		int temp = (Integer)obj;
        		System.out.println("字符串的值 = "+temp);
        	}else if(obj instanceof UserVo){
        		UserVo uv = (UserVo)obj;
        		System.out.println("名称 = "+uv.getUsername()+", 密码 = "+uv.getUserpass());
        	}
        }
//        System.out.println("***="+py_2.__call__(new PyList(list))); 
        
        System.out.println("+++++++++++++++++读取文件+++++++++++++++++++++++");
        PyFunction py_4 = JythonFactory.getInstance().getPyFunctionFromJythonFile("handler", "queryFileContent.py"); 
        Map map = new HashMap();
        map = (Map)py_4.__call__(new PyString("D:\\Java 开发工具包\\jython\\demo\\project_13F\\a.txt"));
        System.out.println("传说你这边有问题没解决");
        for(Object key : map.keySet()){
        	System.out.println("(key,value) = ("+key+","+map.get(key)+")");
        }
        System.out.println("传说你这边有问题没解决");
        System.out.println("size = "+map.size());
        
        System.out.println("+++++++++++++++++map方式+++++++++++++++++++++++");
        PyFunction py_5 = JythonFactory.getInstance().getPyFunctionFromJythonFile("handler", "queryFileContent2.py"); 
        Map map2 = new HashMap();
        map2.put("DHY:TD-M01-010-001", 11);
        map2.put("DHY:TD-M01-010-002", 22);
        map2.put("DHY:TD-M01-010-003", 33);
        map2.put("DHY:TD-M01-010-004", 44);
        map2.put("DHY:TD-M01-010-005", 55);
        map2.put("DHY:TD-M01-010-006", 66);
        FileInputStream fis = new FileInputStream(new File("D:\\Java 开发工具包\\jython\\demo\\project_13F\\a.txt"));
        File tempFile = new File("D:\\Java 开发工具包\\jython\\demo\\project_13F");
        if(!tempFile.exists()){
        	tempFile.mkdir();
        }
        FileOutputStream fos = new FileOutputStream(new File(tempFile,"b.txt"));
        for(Object key : map2.keySet()){
        	byte[] b = new byte[2048];
        	int len = 0;
        	String content = key +"="+map2.get(key)+"\r\n";
        	b = content.getBytes();
        	fos.write(b, 0, content.length());
        }
        fos.flush();
        fos.close();
        
        System.out.println("###################999999999999######################");
        py_5.__call__(new PyString("D:\\Java 开发工具包\\jython\\demo\\project_13F\\b.txt"));
        
        System.out.println("###################66666######################");
        Map<PyObject , PyObject> map3 = new HashMap<PyObject , PyObject>();
        map3.put(new PyString("DHY:TD-M01-010-001"), new PyInteger(11));
        map3.put(new PyString("DHY:TD-M01-010-002"), new PyInteger(22));
        map3.put(new PyString("DHY:TD-M01-010-003"), new PyInteger(33));
        map3.put(new PyString("DHY:TD-M01-010-004"), new PyInteger(44));
       
        PyFunction py_6 = JythonFactory.getInstance().getPyFunctionFromJythonFile("handler", "queryFileContent3.py"); 
        PyDictionary pb = new PyDictionary(map3);
        PyDictionary pp = (PyDictionary)py_6.__call__(pb);
        Set<String> mapSet = pp.keySet(); 
        for(String key : mapSet)
        {
        	System.out.println("打印map集合key = "+key+", value = "+pp.get(key));
        }
//        System.out.println("打印map集合"+pp);
    } 
}



--------------------------------------------
--------------------------------------------
jython脚本:
1. queryFileContent.py
def handler(filePath):
    result = ""
    f = open(filePath,'r')
    ##lineContent = f.readline()
    ##while(f.readline()):
    ##    result.append(lineContent)
    result = f.read()
    content = result.split(",\n")
    key_value = {}
    for i in range(len(content)):
##        print(content[i])
        splitRes = content[i].split('=')
        key = splitRes[0]
        value = splitRes[1]
        key_value[key] = value
    print key_value
    return key_value
##for i in range(len(content)):
    

2. queryFileContent2.py
def handler(filePath):
    result = ""
    f = open(filePath,'r')
    ##lineContent = f.readline()
    ##while(f.readline()):
    ##    result.append(lineContent)
    result = f.read()
    content = result.split(",\n")
    key_value = {}
    for i in range(len(content)):
##        print(content[i])
        splitRes = content[i].split('=')
        key = splitRes[0]
	if(key == "DHY:TD-M01-010-001"):
            key = "DHY:TD-P01-010-001"
        elif(key == "DHY:TD-M01-010-002"):
            key = "DHY:TD-P01-010-002"
        elif(key == "DHY:TD-M01-010-003"):
            key = "DHY:TD-P01-010-003"
        elif(key == "DHY:TD-M01-010-004"):
            key = "DHY:TD-P01-010-004"
        elif(key == "DHY:TD-M01-010-005"):
            key = "DHY:TD-P01-010-005"
        elif(key == "DHY:TD-M01-010-006"):
            key = "DHY:TD-P01-010-006"
        value = splitRes[1]
        key_value[key] = value
        print key +" = "+ value
##    print key_value
    return key_value
        

    
3. queryFileContent3.py
def handler(mp):
    key_obj = mp.keys()
    key_val = mp.values()
#    print key_obj , key_val
    for (k, v) in mp.items():
        print k, v
    return mp
Global site tag (gtag.js) - Google Analytics